Skip to content

Commit

Permalink
Merge pull request #40 from danshevluk/master
Browse files Browse the repository at this point in the history
Breaking improvements
  • Loading branch information
pengyunchou authored Nov 25, 2016
2 parents 484cf9f + 752c9d8 commit 888cf18
Show file tree
Hide file tree
Showing 32 changed files with 1,486 additions and 1,507 deletions.
42 changes: 30 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
*.xcuserdatad
*.DS_Store
*.pbxproj
*.xcuserstate
*.xccheckout
# Xcode
.DS_Store
build/
*.pbxuser
!default.pbxuser
Expand All @@ -14,12 +8,36 @@ build/
!default.mode2v3
*.perspectivev3
!default.perspectivev3
*.xcworkspace
!default.xcworkspace
xcuserdata
profile
*.xccheckout
*.moved-aside
DerivedData
.idea/
# Pods - for those of you who use CocoaPods
Pods
*.hmap
*.ipa
*.xcuserstate

#OS X Stuff
.localized
.DS_Store
*.zip

# Pods
Pods/

# Editors
.idea
*.swp

# Fastlane
fastlane/report.xml
*.mobileprovision
*.certSigningRequest
*.cer

# Keys
*.pem
*.pkey
*.p12

# Rbenv
.ruby-version
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
121 changes: 54 additions & 67 deletions README.md
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
4 changes: 1 addition & 3 deletions ysocket-ios/Info.plist → Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<string>1.1</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
28 changes: 28 additions & 0 deletions Sources/Result.swift
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
}
}
}
53 changes: 53 additions & 0 deletions Sources/Socket.swift
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
}
9 changes: 9 additions & 0 deletions Sources/SwiftSocket.h
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>
Loading

0 comments on commit 888cf18

Please sign in to comment.