mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net: socket send and recv
This commit is contained in:
parent
cd4fe63355
commit
859c8ffdb8
@ -154,6 +154,25 @@ pub fn dial(address string, port int) Socket {
|
|||||||
return s
|
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
|
// shutdown and close socket
|
||||||
pub fn (s Socket) close() int {
|
pub fn (s Socket) close() int {
|
||||||
shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)
|
shutdown_res := C.shutdown(s.sockfd, SHUT_RDWR)
|
||||||
|
@ -7,7 +7,14 @@ fn test_socket() {
|
|||||||
// println(client)
|
// println(client)
|
||||||
// socket := server.accept()
|
// socket := server.accept()
|
||||||
// println(socket)
|
// 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()
|
// server.close()
|
||||||
// client.close()
|
// client.close()
|
||||||
// socket.close()
|
// socket.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user