added some other network builtins
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
|
||||
port = 8080;
|
||||
addr = "127.0.0.1";
|
||||
|
||||
fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0);
|
||||
|
||||
if fd < 0:
|
||||
{
|
||||
print('Failed to create socket.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
sin_addr = {};
|
||||
if net.inet_aton(addr, sin_addr) == 0:
|
||||
{
|
||||
print('Bad address string format.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
if net.connect(fd, { sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: sin_addr }) != 0:
|
||||
{
|
||||
print('Failed to connect.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
print('Connected!\n');
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
port = 8080;
|
||||
backlog = 5;
|
||||
|
||||
fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0);
|
||||
|
||||
if fd < 0:
|
||||
{
|
||||
print('Failed to create socket.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
if net.bind(fd, {sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: {s_addr: net.htonl(net.INADDR_ANY)}}) != 0:
|
||||
{
|
||||
print('Failed to bind.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
if net.listen(fd, backlog) != 0:
|
||||
{
|
||||
print('Failed to listen.\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
while true:
|
||||
{
|
||||
client_addr = {};
|
||||
client_fd = net.accept(fd, client_addr);
|
||||
|
||||
if client_fd < 0:
|
||||
print('Warning! Failed to accept!\n');
|
||||
|
||||
print('Got a connection!\n');
|
||||
print('client_fd = ', client_fd, '\n');
|
||||
print('client_addr = {', client_addr, '}\n');
|
||||
|
||||
# ..continue..
|
||||
}
|
||||
+24
-5
@@ -1,9 +1,28 @@
|
||||
|
||||
print('test 0\n');
|
||||
addr = "216.58.208.142";
|
||||
port = 80;
|
||||
|
||||
print('SOCK_STREAM = ', network.SOCK_STREAM, ';\n');
|
||||
fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0);
|
||||
|
||||
print('test 1\n');
|
||||
if fd < 0:
|
||||
{
|
||||
print('Failed to create socket\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
fd = network.socket(network.AF_INET, network.SOCK_STREAM, 0);
|
||||
print(fd);
|
||||
in_addr = {};
|
||||
if net.inet_aton(addr, in_addr) == 0:
|
||||
{
|
||||
print('Bad address string format\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
r = net.connect(fd, {sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: in_addr});
|
||||
|
||||
if r < 0:
|
||||
{
|
||||
print('Failed to connect\n');
|
||||
return none;
|
||||
}
|
||||
|
||||
print('Connected!\n');
|
||||
Reference in New Issue
Block a user