2019-09-23 23:17:06 +03:00
|
|
|
import net
|
2019-06-30 17:11:55 +03:00
|
|
|
|
2019-07-01 15:55:45 +03:00
|
|
|
fn test_socket() {
|
2019-09-20 17:04:39 +03:00
|
|
|
mut server := net.listen(0) or {
|
|
|
|
println(err)
|
|
|
|
return
|
2019-09-23 23:17:06 +03:00
|
|
|
}
|
2019-09-20 17:04:39 +03:00
|
|
|
server_port := server.get_port()
|
|
|
|
mut client := net.dial('127.0.0.1', server_port) or {
|
|
|
|
println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut socket := server.accept() or {
|
|
|
|
println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
message := 'Hello World'
|
|
|
|
socket.send(message.str, message.len)
|
2019-09-23 19:48:18 +03:00
|
|
|
$if debug { println('message send: $message') }
|
|
|
|
$if debug { println('send socket: $socket.sockfd') }
|
2019-09-20 17:04:39 +03:00
|
|
|
|
2019-09-23 23:17:06 +03:00
|
|
|
bytes, blen := client.recv(1024)
|
2019-09-23 19:48:18 +03:00
|
|
|
received := tos(bytes, blen)
|
|
|
|
$if debug { println('message received: $received') }
|
|
|
|
$if debug { println('client: $client.sockfd') }
|
2019-09-20 17:04:39 +03:00
|
|
|
|
|
|
|
assert message == received
|
|
|
|
|
|
|
|
server.close()
|
|
|
|
client.close()
|
|
|
|
socket.close()
|
2019-09-23 23:17:06 +03:00
|
|
|
}
|