1、swift socket編程怎麼弄
swift代碼:需要導入GCDAsyncSocket庫文件,可去github下載
import UIKit
class ViewController:
UIViewController,
GCDAsyncSocketDelegate {
@IBOutlet var serveripInput:
UITextField!
@IBOutlet var msgInput:
UITextField!
@IBOutlet var conBtn:
UIButton!
@IBOutlet var sendBtn:
UIButton!
@IBOutlet var msgView:
UITextView!
let serverPort: UInt16 =
9050
var clientSocket:
GCDAsyncSocket!
var mainQueue =
dispatch_get_main_queue()
override func viewDidLoad() {
super.viewDidLoad()
}
//連接伺服器按鈕事件
@IBAction func conBtnClick(sender:
AnyObject) {
do {
clientSocket = GCDAsyncSocket()
clientSocket.delegate =
self
clientSocket.delegateQueue =
dispatch_get_global_queue(0,
0)
try
clientSocket.connectToHost(serveripInput.text!,
onPort: serverPort)
conBtn.backgroundColor =
UIColor.blueColor()
}
catch {
print("error")
conBtn.backgroundColor =
UIColor.redColor()
}
}
func socket(sock:
GCDAsyncSocket!, didConnectToHost host: String!, port:
UInt16) {
print("與伺服器連接成功!")
clientSocket.readDataWithTimeout(-1, tag:
0)
}
func socketDidDisconnect(sock:
GCDAsyncSocket!, withError err: NSError!) {
print("與伺服器斷開連接")
}
func socket(sock:
GCDAsyncSocket!, didReadData data: NSData!, withTag tag:
Int) {
// 1
獲取客戶的發來的數據 ,把 NSData
轉 NSString
let readClientDataString:
NSString? = NSString(data: data, encoding:
NSUTF8StringEncoding)
print(readClientDataString!)
// 2
主界面ui 顯示數據
dispatch_async(mainQueue, {
let showStr:
NSMutableString = NSMutableString()
showStr.appendString(self.msgView.text)
showStr.appendString(readClientDataString!
as String)
showStr.appendString("\n")
self.msgView.text = showStr
as String
})
// 3.處理請求,返回數據給客戶端 ok
let serviceStr:
NSMutableString = NSMutableString()
serviceStr.appendString("ok\n")
clientSocket.writeData(serviceStr.dataUsingEncoding(NSUTF8StringEncoding), withTimeout: -1,
tag: 0)
// 4
每次讀完數據後,都要調用一次監聽數據的方法
clientSocket.readDataWithTimeout(-1, tag:
0)
}
//發送消息按鈕事件
@IBAction func sendBtnClick(sender:
AnyObject) {
// 1.處理請求,返回數據給客戶端 ok
let serviceStr:
NSMutableString = NSMutableString()
serviceStr.appendString(self.msgInput.text!)
serviceStr.appendString("\n")
clientSocket.writeData(serviceStr.dataUsingEncoding(NSUTF8StringEncoding), withTimeout: -1,
tag: 0)
}
}
2、SWIFT L/C主要條款的域名號是什麼
SWIFT MT700的域名一共有37個,從27開始,到72結束,主要有
45A貨物描述
46A單據要求
47A附加條款
等等。
3、swift tcp的連接用什麼庫
使用SQLiteDatabase操作SQLite資料庫 Android提供了一個名為SQLiteDatabase的類,該類封裝了一些操作資料庫的API,使用該類可以完成對數據進行添加(Create)、查詢(Retrieve)、更新(Update)和刪除(Delete)操作(這些操作簡稱為CRUD)。對SQLiteDa...
4、swift3.0 gcdasyncsocket 客戶端怎麼接收數據
swift代碼:需要導入GCDAsyncSocket庫文件,可去github下載
import UIKit
class ViewController:
UIViewController,
GCDAsyncSocketDelegate {
@IBOutlet var serveripInput:
UITextField!
@IBOutlet var msgInput:
UITextField!
@IBOutlet var conBtn:
UIButton!
@IBOutlet var sendBtn:
UIButton!
@IBOutlet var msgView:
UITextView!
let serverPort: UInt16 =
9050
var clientSocket:
GCDAsyncSocket!
var mainQueue =
dispatch_get_main_queue()
override func viewDidLoad() {
super.viewDidLoad()
}
//連接伺服器按鈕事件
@IBAction func conBtnClick(sender:
AnyObject) {
do {
clientSocket = GCDAsyncSocket()
clientSocket.delegate =
self
clientSocket.delegateQueue =
dispatch_get_global_queue(0,
0)
try
clientSocket.connectToHost(serveripInput.text!,
onPort: serverPort)
conBtn.backgroundColor =
UIColor.blueColor()
}
catch {
print("error")
conBtn.backgroundColor =
UIColor.redColor()
}
}
func socket(sock:
GCDAsyncSocket!, didConnectToHost host: String!, port:
UInt16) {
print("與伺服器連接成功!")
clientSocket.readDataWithTimeout(-1, tag:
0)
}
func socketDidDisconnect(sock:
GCDAsyncSocket!, withError err: NSError!) {
print("與伺服器斷開連接")
}
func socket(sock:
GCDAsyncSocket!, didReadData data: NSData!, withTag tag:
Int) {
// 1
獲取客戶的發來的數據 ,把 NSData
轉 NSString
let readClientDataString:
NSString? = NSString(data: data, encoding:
NSUTF8StringEncoding)
print(readClientDataString!)
// 2
主界面ui 顯示數據
dispatch_async(mainQueue, {
let showStr:
NSMutableString = NSMutableString()
showStr.appendString(self.msgView.text)
showStr.appendString(readClientDataString!
as String)
showStr.appendString("\n")
self.msgView.text = showStr
as String
})
// 3.處理請求,返回數據給客戶端 ok
let serviceStr:
NSMutableString = NSMutableString()
serviceStr.appendString("ok\n")
clientSocket.writeData(serviceStr.dataUsingEncoding(NSUTF8StringEncoding), withTimeout: -1,
tag: 0)
// 4
每次讀完數據後,都要調用一次監聽數據的方法
clientSocket.readDataWithTimeout(-1, tag:
0)
}
//發送消息按鈕事件
@IBAction func sendBtnClick(sender:
AnyObject) {
// 1.處理請求,返回數據給客戶端 ok
let serviceStr:
NSMutableString = NSMutableString()
serviceStr.appendString(self.msgInput.text!)
serviceStr.appendString("\n")
clientSocket.writeData(serviceStr.dataUsingEncoding(NSUTF8StringEncoding), withTimeout: -1,
tag: 0)
}
}
5、swift socket.io怎麼用
socket.io封裝了websocket,同時包含了其它的連接方式,比如Ajax。原因在於不是所有的瀏覽器都支持websocket,通過socket.io的封裝,你不用關心裏面用了什麼連接方式。你在任何瀏覽器里都可以使用socket.io來建立非同步的連接。
6、ios8 swift CFSocket怎麼使用
swiftsocket
https://github.com/xyyc/SwiftSocket
例子
https://github.com/xyyc/SwiftChatServer
https://github.com/xyyc/SwiftChatClient