net: socket send and recv
parent
cd4fe63355
commit
859c8ffdb8
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue