GCDAsyncSocket 不会抛出错误
GCDAsyncSocket doesn't throw error
我正在 Swift 上为 Mac OS 编写简单的 Telnet 客户端。我正在连接到 localhost
上的端口 9052 并发送三个简单的命令。那是我的代码:
import Cocoa
import CocoaAsyncSocket
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var socket : GCDAsyncSocket!
func applicationDidFinishLaunching(aNotification: NSNotification) {
requestIdentity()
}
func requestIdentity() -> Bool {
socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
let port : UInt16 = 9052
do {
try socket.connectToHost("127.0.0.1", onPort: port, withTimeout: 2.0)
} catch let e {
print("Error \(e) accured while connecting to \(port) on 127.0.0.1")
return false
}
print("connection working")
return true
}
func applicationWillTerminate(aNotification: NSNotification) {
}
func socket(socket : GCDAsyncSocket, didReadData data : NSData, withTag : UInt16) {
let response = String(data: data, encoding: NSUTF8StringEncoding)
print("Got response : \(response)")
}
func socket(socket : GCDAsyncSocket, didConnectToHost host : String, port p: UInt16) {
print("Connected to host \(host) on port \(p)")
var request = "AUTHENTICATE \"1728390\"\n"
var data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
socket.readDataWithTimeout(-1.0, tag: 0)
request = "SIGNAL NEWNYM\n"
data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
socket.readDataWithTimeout(-1.0, tag: 0)
request = "QUIT\n"
data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
}
}
我故意关闭端口 9052,但套接字始终 "connected" - 它不会抛出任何错误,也不会提供任何输出 - 它只是什么都不做。如何解决这个问题?
找到解决方法:当连接超时时,
func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!)
被调用。
我正在 Swift 上为 Mac OS 编写简单的 Telnet 客户端。我正在连接到 localhost
上的端口 9052 并发送三个简单的命令。那是我的代码:
import Cocoa
import CocoaAsyncSocket
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var socket : GCDAsyncSocket!
func applicationDidFinishLaunching(aNotification: NSNotification) {
requestIdentity()
}
func requestIdentity() -> Bool {
socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
let port : UInt16 = 9052
do {
try socket.connectToHost("127.0.0.1", onPort: port, withTimeout: 2.0)
} catch let e {
print("Error \(e) accured while connecting to \(port) on 127.0.0.1")
return false
}
print("connection working")
return true
}
func applicationWillTerminate(aNotification: NSNotification) {
}
func socket(socket : GCDAsyncSocket, didReadData data : NSData, withTag : UInt16) {
let response = String(data: data, encoding: NSUTF8StringEncoding)
print("Got response : \(response)")
}
func socket(socket : GCDAsyncSocket, didConnectToHost host : String, port p: UInt16) {
print("Connected to host \(host) on port \(p)")
var request = "AUTHENTICATE \"1728390\"\n"
var data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
socket.readDataWithTimeout(-1.0, tag: 0)
request = "SIGNAL NEWNYM\n"
data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
socket.readDataWithTimeout(-1.0, tag: 0)
request = "QUIT\n"
data = request.dataUsingEncoding(NSUTF8StringEncoding)
socket.writeData(data, withTimeout: -1.0, tag: 0)
}
}
我故意关闭端口 9052,但套接字始终 "connected" - 它不会抛出任何错误,也不会提供任何输出 - 它只是什么都不做。如何解决这个问题?
找到解决方法:当连接超时时,
func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!)
被调用。