🐛 fix bug in recvfrom
This commit is contained in:
parent
e8c8a6446b
commit
2ea9e03522
|
@ -76,8 +76,9 @@ var socket=func(){
|
||||||
MSG_OOB:0x1,
|
MSG_OOB:0x1,
|
||||||
MSG_PEEK:0x2,
|
MSG_PEEK:0x2,
|
||||||
MSG_DONTROUTE:0x4,
|
MSG_DONTROUTE:0x4,
|
||||||
|
MSG_DONTWAIT:0x40,
|
||||||
|
|
||||||
socket:func(af,type,proto){
|
socket:func(af,type,proto = 0){
|
||||||
return invoke_iii(sock,af,type,proto);
|
return invoke_iii(sock,af,type,proto);
|
||||||
},
|
},
|
||||||
closesocket:func(sd){
|
closesocket:func(sd){
|
||||||
|
|
|
@ -231,6 +231,7 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
|
||||||
hash["str"] = ngc->newstr(buf);
|
hash["str"] = ngc->newstr(buf);
|
||||||
delete[] buf;
|
delete[] buf;
|
||||||
hash["fromip"] = ngc->newstr(inet_ntoa(addr.sin_addr));
|
hash["fromip"] = ngc->newstr(inet_ntoa(addr.sin_addr));
|
||||||
|
hash["port"] = var::num(ntohs(addr.sin_port));
|
||||||
ngc->temp = nil;
|
ngc->temp = nil;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
use module.libsock;
|
||||||
|
|
||||||
|
var udp_server = func(hostname, port, retry_delay = 5) {
|
||||||
|
var socket = libsock.socket;
|
||||||
|
var server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
|
||||||
|
while(socket.bind(server, hostname, port) < 0){
|
||||||
|
println("[", os.time(), "] failed to bind socket "~server~" at ", hostname, ":", port, ".");
|
||||||
|
unix.sleep(retry_delay);
|
||||||
|
println("[", os.time(), "] retrying...");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sendto: func(ip, port, message) {
|
||||||
|
var res = socket.sendto(server, ip, port, message);
|
||||||
|
println("[", os.time(), "] send message to ", ip, ":", port, " ", res, " byte(s)");
|
||||||
|
return res;
|
||||||
|
},
|
||||||
|
recvfrom: func(length = 1024) {
|
||||||
|
var message = socket.recvfrom(server, length);
|
||||||
|
println("[", os.time(), "] get message \"", message.str, "\" from ", message.fromip, ":", message.port);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var udp_client = func(hostname = "", port = -1, retry_delay = 5) {
|
||||||
|
var socket = libsock.socket;
|
||||||
|
var client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
|
||||||
|
if (port > 0 and size(hostname) != 0) {
|
||||||
|
while(socket.bind(client, hostname, port)<0){
|
||||||
|
println("[",os.time(),"] failed to bind socket "~client~" at ", hostname, ":", port, ".");
|
||||||
|
unix.sleep(retry_delay);
|
||||||
|
println("[",os.time(),"] retrying...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sendto: func(ip, port, message) {
|
||||||
|
var res = socket.sendto(client, ip, port, message);
|
||||||
|
println("[", os.time(), "] send message to ", ip, ":", port, " ", res, " byte(s)");
|
||||||
|
return res;
|
||||||
|
},
|
||||||
|
recvfrom: func(length = 1024) {
|
||||||
|
var message = socket.recvfrom(client, length);
|
||||||
|
println("[", os.time(), "] get message \"", message.str, "\" from ", message.fromip, ":", message.port);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
use std.udp;
|
||||||
|
|
||||||
|
var argument = arg[0];
|
||||||
|
if (argument=="server") {
|
||||||
|
var server = udp.udp_server("127.0.0.1", 5506);
|
||||||
|
while(1) {
|
||||||
|
var message = server.recvfrom();
|
||||||
|
server.sendto(message.fromip, message.port, "from server");
|
||||||
|
}
|
||||||
|
} elsif (argument=="client") {
|
||||||
|
var client = udp.udp_client();
|
||||||
|
while(1) {
|
||||||
|
client.sendto("127.0.0.1", 5506, "hello");
|
||||||
|
var message = client.recvfrom();
|
||||||
|
println(message);
|
||||||
|
unix.sleep(1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue