bugfixed !( stuff
[my-code/ivac.git] / src / network.c
index 3e9fe39..66314f9 100644 (file)
@@ -53,6 +53,12 @@ int network_init(t_net *net) {
 
 int network_shutdown(t_net *net) {
 
+  int channel;
+
+  for(channel=0;channel<MAX_CONNECTIONS;channel++)
+    if(net->connection[channel].status&C_SOCKET)
+      close(net->connection[channel].fd);
+
   if(close(net->l_fd)==-1) {
     perror("[network] close call");
     return N_ERROR;
@@ -90,14 +96,16 @@ int network_manage_connection(t_net *net) {
 
       if(net->connection[i].status&C_INFO_A) {
 
-        if(!net->connection[i].status&C_SOCKET) {
+        if(!(net->connection[i].status&C_SOCKET)) {
           if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
             perror("[network] socket call");
             return N_ERROR;
           }
+          net->connection[i].status|=C_SOCKET;
         }
 
-        if(!net->connection[i].status&C_ESTABL) {
+        if((!(net->connection[i].status&C_ESTABL))&&
+           (net->connection[i].status&C_SOCKET)) {
 
           memset(&addr,0,sizeof(struct sockaddr));
           addr.sin_family=AF_INET;
@@ -115,6 +123,7 @@ int network_manage_connection(t_net *net) {
 
           printf("[network] established connection to %s port %d on channel %d\n",
                  net->connection[i].ip,net->connection[i].port,i);
+          net->connection[i].status|=C_ESTABL;
 
         }
 
@@ -134,7 +143,7 @@ int network_manage_incoming(t_net *net) {
   int len;
 
   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
-    if(!net->connection[channel].status&C_IN_USE) {
+    if(!(net->connection[channel].status&C_IN_USE)) {
       if((net->connection[channel].fd=accept(net->l_fd,
                                        (struct sockaddr *)&addr,
                                        &len))==-1) {