net.socket: windows fixes and test
* 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 conflictspull/2052/head
parent
f1bb25bfaf
commit
a42b4e219c
|
@ -46,8 +46,9 @@ mut:
|
||||||
ai_flags int
|
ai_flags int
|
||||||
ai_protocol int
|
ai_protocol int
|
||||||
ai_addrlen int
|
ai_addrlen int
|
||||||
ai_next voidptr
|
|
||||||
ai_addr voidptr
|
ai_addr voidptr
|
||||||
|
ai_canonname voidptr
|
||||||
|
ai_next voidptr
|
||||||
}
|
}
|
||||||
|
|
||||||
struct C.sockaddr_storage {}
|
struct C.sockaddr_storage {}
|
||||||
|
@ -175,6 +176,10 @@ pub fn (s Socket) connect(address string, port int) ?int {
|
||||||
hints.ai_family = C.AF_UNSPEC
|
hints.ai_family = C.AF_UNSPEC
|
||||||
hints.ai_socktype = C.SOCK_STREAM
|
hints.ai_socktype = C.SOCK_STREAM
|
||||||
hints.ai_flags = C.AI_PASSIVE
|
hints.ai_flags = C.AI_PASSIVE
|
||||||
|
hints.ai_addrlen = 0
|
||||||
|
hints.ai_canonname = C.NULL
|
||||||
|
hints.ai_addr = C.NULL
|
||||||
|
|
||||||
|
|
||||||
info := &C.addrinfo{!}
|
info := &C.addrinfo{!}
|
||||||
sport := '$port'
|
sport := '$port'
|
||||||
|
@ -263,7 +268,7 @@ const (
|
||||||
)
|
)
|
||||||
pub fn (s Socket) write(str string) {
|
pub fn (s Socket) write(str string) {
|
||||||
line := '$str\r\n'
|
line := '$str\r\n'
|
||||||
C.write(s.sockfd, line.str, line.len)
|
C.send(s.sockfd, line.str, line.len, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (s Socket) read_line() string {
|
pub fn (s Socket) read_line() string {
|
||||||
|
@ -306,4 +311,11 @@ pub fn (s Socket) read_line() string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (s Socket) get_port() int {
|
||||||
|
mut addr := C.sockaddr_in {}
|
||||||
|
size := 16 // sizeof(sockaddr_in)
|
||||||
|
sockname_res := C.getsockname(s.sockfd, &addr, &size)
|
||||||
|
return int(C.ntohs(addr.sin_port))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
import net
|
import net
|
||||||
|
|
||||||
fn test_socket() {
|
fn test_socket() {
|
||||||
// server := net.listen(8080) or {
|
mut server := net.listen(0) or {
|
||||||
// println(err)
|
println(err)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// println(server)
|
server_port := server.get_port()
|
||||||
// client := net.dial('127.0.0.1', 8080) or {
|
mut client := net.dial('127.0.0.1', server_port) or {
|
||||||
// println(err)
|
println(err)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// println(client)
|
mut socket := server.accept() or {
|
||||||
// socket := server.accept() or {
|
println(err)
|
||||||
// println(err)
|
return
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
// println(socket)
|
message := 'Hello World'
|
||||||
//
|
socket.send(message.str, message.len)
|
||||||
// message := 'Hello World'
|
|
||||||
// socket.send(message.str, message.len)
|
bytes := client.recv(1024)
|
||||||
// println('Sent: ' + message)
|
received := tos(bytes, message.len)
|
||||||
//
|
|
||||||
// bytes := client.recv(1024)
|
assert message == received
|
||||||
// println('Received: ' + tos(bytes, message.len))
|
|
||||||
//
|
server.close()
|
||||||
// server.close()
|
client.close()
|
||||||
// client.close()
|
socket.close()
|
||||||
// socket.close()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue