Better client.
authorviric@llimona
Fri, 14 Sep 2007 21:19:46 +0200
changeset 5 5ed1654fe407
parent 4 b5b369483ec0
child 6 4fe857c0b12a
Better client.
main.c
--- a/main.c	Fri Sep 14 21:08:13 2007 +0200
+++ b/main.c	Fri Sep 14 21:19:46 2007 +0200
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "main.h"
 
@@ -73,6 +74,7 @@
     int maxfd;
     int opened_socket;
     int stdin_opened;
+    int res;
 
     child_read = child_pipe[0];
     child_write = child_pipe[1];
@@ -101,18 +103,23 @@
         }
 
         /* Will block */
-        select(maxfd + 1, &read_set, 0, 0, 0);
+        res = select(maxfd + 1, &read_set, 0, 0, 0);
+        if (res == -1)
+        {
+            if (errno == EINTR)
+                continue;
+            else
+                error("Error in select()");
+        }
 
         if (FD_ISSET(child_read, &read_set))
         {
-            int res;
             res = forward_app_data(child_read, 1);
             if (res == 0)
                 break;
         }
         if (FD_ISSET(0, &read_set))
         {
-            int res;
             res = forward_app_data(0, child_write);
             if (res == 0)
             {
@@ -122,7 +129,6 @@
         }
         if (opened_socket >= 0 && FD_ISSET(opened_socket, &read_set))
         {
-            int res;
             res = forward_app_data(opened_socket, child_write);
             if (res == 0)
             {
@@ -157,12 +163,42 @@
 {
     int cs;
     int res;
+    fd_set read_set;
+    int maxfd;
+
     cs = connect_socket();
 
     do
     {
-        res = forward_app_data(0, cs);
-    } while (res > 0);
+        FD_ZERO(&read_set);
+
+        FD_SET(cs, &read_set); /* For reading other side's close() */
+        maxfd = cs;
+        FD_SET(0, &read_set); /* stdin */
+        maxfd = max(maxfd, cs);
+
+        res = select(maxfd + 1, &read_set, 0, 0, 0);
+        if (res == -1)
+        {
+            if (errno == EINTR)
+                continue;
+            else
+                error("Error in select()");
+        }
+
+        if (FD_ISSET(cs, &read_set))
+        {
+            /* assuming close() on the other side, even
+             * without read(). */
+            break;
+        }
+        if (FD_ISSET(0, &read_set))
+        {
+            res = forward_app_data(0, cs);
+            if (res == 0) /* EOF */
+                break;
+        }
+    } while (1);
 
     close(cs);
     return 0;