-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from danshevluk/master
Breaking improvements
- Loading branch information
Showing
32 changed files
with
1,486 additions
and
1,507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,91 @@ | ||
# a simple socket library for apple swift lang | ||
# usage | ||
> drag ysocket.c and ysocket.swift to your project | ||
> just use apis in YSocket class | ||
# SwiftSocket | ||
SwiftSocket library provides as easy to use interface for socket based connections on server or client side. Supports both TCP and UDP sockets. | ||
|
||
# api usage | ||
## create client socket | ||
``` swift | ||
//create a socket connect to www.apple.com and port at 80 | ||
var client:TCPClient = TCPClient(addr: "www.apple.com", port: 80) | ||
``` | ||
## connect with timeout | ||
``` swift | ||
var (success, errmsg) = client.connect(timeout: 10) | ||
# Installation | ||
## Cocoapods | ||
Add this to your `Podfile`: | ||
```ruby | ||
pod 'SwiftSocket' | ||
``` | ||
And run then `pod install` | ||
|
||
## send data | ||
``` swift | ||
var (success, errmsg) = client.send(str:"GET / HTTP/1.0\n\n") | ||
//or you can send binnary data | ||
//socket.send(data:[Int8]) | ||
``` | ||
# Code examples | ||
|
||
## read data | ||
## Create client socket | ||
``` swift | ||
var data = client.read(1024*10) //return optional [Int8] | ||
// Create a socket connect to www.apple.com and port at 80 | ||
let client = TCPClient(address: "www.apple.com", port: 80) | ||
``` | ||
|
||
## close socket | ||
## Connect with timeout | ||
You can also set timeout to `-1` or leave parameters empty (`client.connect()`) to turn off connection timeout. | ||
``` swift | ||
var (success, errormsg) = client.close() | ||
switch client.connect(timeout: 10) { | ||
case .success: | ||
// Connection successful 🎉 | ||
case .failure(let error): | ||
// 💩 | ||
} | ||
``` | ||
|
||
## create servert socket | ||
|
||
## Send data | ||
``` swift | ||
var server:TCPServer = TCPServer(addr: "127.0.0.1", port: 8080) | ||
let data: Data = // ... Bytes you want to send | ||
let result = client.send(data: data) | ||
``` | ||
|
||
## listen | ||
|
||
## Read data | ||
``` swift | ||
var (success, msg) = server.listen() | ||
var data = client.read(1024*10) //return optional [Int8] | ||
``` | ||
### accept | ||
|
||
## Close socket | ||
``` swift | ||
var client = server.accept() //now you can use client socket api to read and write | ||
client.close() | ||
``` | ||
|
||
# client socket example | ||
## Client socket example | ||
``` swift | ||
//创建socket | ||
var client:TCPClient = TCPClient(addr: "www.apple.com", port: 80) | ||
//连接 | ||
var (success, errmsg) = client.connect(timeout: 1) | ||
if success { | ||
//发送数据 | ||
var (success, errmsg) = client.send(str:"GET / HTTP/1.0\n\n" ) | ||
if success { | ||
//读取数据 | ||
var data = client.read(1024*10) | ||
if let d = data { | ||
if let str = String.stringWithBytes(d, length: d.count, encoding: NSUTF8StringEncoding){ | ||
println(str) | ||
} | ||
let client = TCPClient(address: "www.apple.com", port: 80) | ||
switch client.connect(timeout: 1) { | ||
case .success: | ||
switch client.send(string: "GET / HTTP/1.0\n\n" ) { | ||
case .success: | ||
guard let data = client.read(1024*10) else { return } | ||
|
||
if let response = String(bytes: data, encoding: .utf8) { | ||
print(response) | ||
} | ||
}else { | ||
println(errmsg) | ||
case .failure(let error): | ||
print(error) | ||
} | ||
} else { | ||
println(errmsg) | ||
case .failure(let error): | ||
print(error) | ||
} | ||
|
||
``` | ||
|
||
# server socket example (echo server) | ||
## Server socket example (echo server) | ||
``` swift | ||
func echoService(client c:TCPClient) { | ||
println("newclient from:\(c.addr)[\(c.port)]") | ||
func echoService(client: TCPClient) { | ||
print("Newclient from:\(c.address)[\(c.port)]") | ||
var d = c.read(1024*10) | ||
c.send(data: d!) | ||
c.close() | ||
} | ||
func testserver(){ | ||
var server:TCPServer = TCPServer(addr: "127.0.0.1", port: 8080) | ||
var (success, msg) = server.listen() | ||
if success { | ||
|
||
func testServer() { | ||
let server = TCPServer(address: "127.0.0.1", port: 8080) | ||
switch server.listen() { | ||
case .success: | ||
while true { | ||
if var client = server.accept() { | ||
echoService(client: client) | ||
} else { | ||
println("accept error") | ||
print("accept error") | ||
} | ||
} | ||
} else { | ||
println(msg) | ||
case .failure(let error): | ||
print(error) | ||
} | ||
} | ||
``` | ||
|
||
# Copyright and License | ||
Code released under the BSD license. | ||
|
||
# QQ group | ||
275935304 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
|
||
public enum Result { | ||
case success | ||
case failure(Error) | ||
|
||
public var isSuccess: Bool { | ||
switch self { | ||
case .success: | ||
return true | ||
case .failure: | ||
return false | ||
} | ||
} | ||
|
||
public var isFailure: Bool { | ||
return !isSuccess | ||
} | ||
|
||
public var error: Error? { | ||
switch self { | ||
case .success: | ||
return nil | ||
case .failure(let error): | ||
return error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Copyright (c) <2014>, skysent | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// 1. Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// 2. Redistributions in binary form must reproduce the above copyright | ||
// notice, this list of conditions and the following disclaimer in the | ||
// documentation and/or other materials provided with the distribution. | ||
// 3. All advertising materials mentioning features or use of this software | ||
// must display the following acknowledgement: | ||
// This product includes software developed by skysent. | ||
// 4. Neither the name of the skysent nor the | ||
// names of its contributors may be used to endorse or promote products | ||
// derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY skysent ''AS IS'' AND ANY | ||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL skysent BE LIABLE FOR ANY | ||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias Byte = UInt8 | ||
|
||
open class Socket { | ||
|
||
public let address: String | ||
public let port: Int32 | ||
public var fd: Int32? | ||
|
||
public init(address: String, port: Int32) { | ||
self.address = address | ||
self.port = port | ||
} | ||
|
||
} | ||
|
||
public enum SocketError: Error { | ||
case queryFailed | ||
case connectionClosed | ||
case connectionTimeout | ||
case unknownError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
//! Project version number for SwiftSocket | ||
FOUNDATION_EXPORT double SwiftSocketVersionNumber; | ||
|
||
//! Project version string for SwiftSocket | ||
FOUNDATION_EXPORT const unsigned char SwiftSocketVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <SwiftSocket_iOS/PublicHeader.h> |
Oops, something went wrong.