aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorjacopo grandi <jak.sk8@hotmail.it>2021-02-20 10:26:22 +0100
committerjacopo grandi <jak.sk8@hotmail.it>2021-02-20 10:26:22 +0100
commitd1dc6ddc035193ab6e2570f58b7e3a79fc335bce (patch)
tree6d2f1ab0c2e2d11fa7faa4060624585e870b567f /net
parent2eef87c8970db643c4ef09e0fd9e8110c8193043 (diff)
sock work
Diffstat (limited to 'net')
-rw-r--r--net/net.c22
-rw-r--r--net/net.h2
2 files changed, 12 insertions, 12 deletions
diff --git a/net/net.c b/net/net.c
index 7e1dbf4..4dc79f2 100644
--- a/net/net.c
+++ b/net/net.c
@@ -21,14 +21,14 @@ void net_init () {
p_libsys_init ();
}
-int net_open_socket(PSocket **socket, int port) {
+int net_open_socket(PSocket **socket, char ip[], int port) {
*socket = p_socket_new (P_SOCKET_FAMILY_INET,
P_SOCKET_TYPE_STREAM,
P_SOCKET_PROTOCOL_TCP,
NULL);
p_socket_set_blocking (*socket, FALSE);
p_socket_set_timeout (*socket, 10);
- PSocketAddress *sock_addr = p_socket_address_new("127.0.0.1", port);
+ PSocketAddress *sock_addr = p_socket_address_new(ip, port);
int sender_port = 0;
if (p_socket_bind (*socket, sock_addr, FALSE, NULL) == FALSE) {
p_socket_free (*socket);
@@ -58,7 +58,7 @@ int net_recv (PSocket *socket, char buffer[]) {
printf("received (%d)\n", len);
return len;
}
- printf("error server recv\n");
+ printf("error recv\n");
return -1;
} else {
len += recv_now;
@@ -74,7 +74,7 @@ int net_recv (PSocket *socket, char buffer[]) {
void net_client_open(net_client *c) {
- int err = net_open_socket(&c->socket, 0);
+ int err = net_open_socket(&c->socket, "127.0.0.1", 0);
if (err > 0) printf("error opening client socket: %d\n", err);
}
@@ -89,8 +89,8 @@ int net_client_connect (net_client *c, char ip[], int port) {
{
p_socket_address_free (c->addr_server);
p_socket_free (c->socket);
- p_uthread_exit (-1);
printf("error client socket freed\n");
+ return 1;
}
}
@@ -116,15 +116,15 @@ int net_client_recv (net_client *c, char buffer[]) {
}
void net_client_close (net_client *c) {
- p_socket_close (c->socket, NULL);
+ if (c->socket != NULL) p_socket_close (c->socket, NULL);
p_socket_address_free (c->addr_server);
p_socket_free (c->socket);
}
-void net_server_open(net_server *s, int port) {
- int err = net_open_socket(&s->socket, port);
+void net_server_open(net_server *s, char ip[], int port) {
+ int err = net_open_socket(&s->socket, ip, port);
if (err > 0) printf("error opening server socket: %d\n", err);
if (p_socket_listen (s->socket, NULL) == FALSE) {
printf("error server socket listen, nobody in.\n");
@@ -162,7 +162,7 @@ void net_server_send (net_server *s, char data[], int sizeofdata) {
}
void net_server_close (net_server *s) {
- p_socket_close (s->socket, NULL);
- p_socket_free (s->sock_client);
- p_socket_free(s->socket);
+ if (s->socket != NULL) p_socket_close (s->socket, NULL);
+ if (s->sock_client != NULL) p_socket_free (s->sock_client);
+ if (s->socket != NULL) p_socket_free(s->socket);
}
diff --git a/net/net.h b/net/net.h
index 056114b..7ec782f 100644
--- a/net/net.h
+++ b/net/net.h
@@ -26,7 +26,7 @@ typedef struct {
PSocket *sock_client;
} net_server;
-void net_server_open(net_server *s, int port);
+void net_server_open(net_server *s, char ip[], int port);
void net_server_accept(net_server *s);
int net_server_recv(net_server *s, char buffer[]);
void net_server_send (net_server *s, char data[], int sizeofdata);