Skip to content

TCPClient

Evan Swick edited this page Feb 19, 2016 · 3 revisions

TCPClient is used to initiate a connection with other servers.

It has one method, connect(), which initiates a connection with a server and returns the Connection associated with it.

Examples

var client = try TCPClient(host: "google.com", port: 80)
do {
    var stream = try client.connect()
    defer { try stream.close() }

    try stream.write("GET / HTTP/1.1\nUser-Agent: SocketKit/0.8.0\n\n")
    
    while true {
        var bytesRead = try stream.read(1024)
        
        print(bytesRead)
    }
} catch let e as TCPClientError {
    switch e {
    case .ConnectFailed(let errCode):
        fatalError(String.fromCString(strerror(errCode))!)
    default:
        fatalError(String(e))
    }
}
Clone this wiki locally