一、服务器
注意点:
1、- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket;
这里方法里面一定要把这个newSocket,作为一个属性持有
后面的- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data
withTag:(long)tag;- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;方法里面需要用到监听方法sock(这里要用newSocket).readData(withTimeout: -1, tag: 0)
读写操作都可以直接用这个newSocket,页可以直接用delegata方法里面的参数socket
2、需要开通TCP Proxy
3、在开启服务之前要写个URLSession:否则就是报错拒绝连接,或者连接超时(GCDAsyncSocket 没有触发网络连接)
let sess = URLSession.shared;
let urls:NSURL=NSURL.init(string: "www.baidu.com")!
let request:URLRequest=NSURLRequest.init(url: urls as URL) as URLRequest
let task = sess.dataTask(with: request) { (data, res, error) in
if(error == nil){
//.解析服务器返回的数据
//说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)
do {
let dict = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
print(dict)
} catch {
}
}
}
task.resume()//开始执行