Hello!
I would like to create a simple client server application in swift (for MacOS). The client have to send a string to server, and the server have, once received the string, send another string to client.
I have that examples, but I can compile it, but the string won't send or receive:
Server side:
func echoService(client: TCPClient) {
TextBox1.stringValue = ("Newclient from:\(client.address)[\(client.port)]")
let d = client.read(1024*10)
client.send(data: d!)
client.close()
}
func testServer() {
let server = TCPServer(address: "127.0.0.1", port: 8080)
switch server.listen() {
case .success:
while true {
if let client = server.accept() {
echoService(client: client)
} else {
TextBox2.stringValue = ("accept error")
}
}
case .failure(let error):
TextBox2.stringValue = ("error")
}
}
client side:
let client = TCPClient(address: "127.0.0.1", port: 8080)
switch client.connect(timeout: 4) {
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)
}
case .failure(let error):
print(error)
}
case .failure(let error):
print(error)
}
For do that I'm using the library SwiftSocket, found here:
I know swift don't have (for now I hope) an internal library for the socket connection, but any ideas is welcome...