mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
a42b4e219c
* added missing ai_canonname in addrinfo structure * ai_canonname, ai_addr and ai_addrlen must be zeroed before getaddrinfo call * write() must not be used on windows sockets * added (Socket).get_port() function which is useful when socket was initialized with 0 (random) port * tests is fixed, test server started listening on random port to avoid conflicts
30 lines
491 B
V
30 lines
491 B
V
import net
|
|
|
|
fn test_socket() {
|
|
mut server := net.listen(0) or {
|
|
println(err)
|
|
return
|
|
}
|
|
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)
|
|
|
|
bytes := client.recv(1024)
|
|
received := tos(bytes, message.len)
|
|
|
|
assert message == received
|
|
|
|
server.close()
|
|
client.close()
|
|
socket.close()
|
|
}
|