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