net: socket send and recv

pull/937/head
Justice Suh 2019-07-02 11:25:21 -04:00 committed by Alexander Medvednikov
parent cd4fe63355
commit 859c8ffdb8
2 changed files with 27 additions and 1 deletions

View File

@ -154,6 +154,25 @@ pub fn dial(address string, port int) Socket {
return s
}
// send string data to socket
pub fn (s Socket) send(buf byteptr, len int) int {
res := C.send(s.sockfd, buf, len, 0)
if res < 0 {
println('socket: send failed')
}
return res
}
// receive string data from socket
pub fn (s Socket) recv(bufsize int) byteptr {
buf := malloc(bufsize)
res := C.recv(s.sockfd, buf, bufsize, 0)
if res < 0 {
println('socket: recv failed')
}
return buf
}
// shutdown and close socket
pub fn (s Socket) close() int {
shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)

View File

@ -7,7 +7,14 @@ fn test_socket() {
// println(client)
// socket := server.accept()
// println(socket)
//
// message := 'Hello World'
// socket.send(message.cstr(), message.len)
// println('Sent: ' + message)
// bytes := client.recv(1024)
// println('Received: ' + tos(bytes, message.len))
// server.close()
// client.close()
// socket.close()