Hi,

Have you considered using unix domain sockets for client-server
communications?

When both client and server reside in the same computer unix sockets
should be more efficient than internet sockets.  Especially in the
embedded environments where you might want to conserve memory by removing
tcp/ip support from kernel.  Differences between using internet sockets
and unix sockets are minimal.  Instead of IP numbers and ports, unix
sockets use paths.

Rather than to ponder it's usability on a theoretical level, I made a
small patch that switches cli_c and pgserver to unix domain sockets.  TCP
portions are #ifdef'ed out, uclinux-parts probably broken.  Unified diff
is attached to this email.

If thought usable, unix sockets should be put as a config option and
server address parameter definition should be cleaned out.  (and someone
fix uclinux-portions too.)


        - Riba
diff -u -r cli_c/src/clientlib.h cli_c.my/src/clientlib.h
--- cli_c/src/clientlib.h       Fri Jun  8 02:41:00 2001
+++ cli_c.my/src/clientlib.h    Sun Jul  1 01:18:48 2001
@@ -36,7 +36,11 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef WANT_IN
 #include <netinet/in.h>
+#else
+#include <sys/un.h>
+#endif
 #include <netdb.h>
 #include <stdio.h>    /* for fprintf() */
 #include <malloc.h>
@@ -58,7 +62,11 @@
 //#define DEBUG_EVT
 
 /* Default server */
+#ifdef WANT_IN
 #define PG_REQUEST_SERVER       "127.0.0.1"
+#else
+#define PG_REQUEST_SERVER      "/tmp/.pgui"
+#endif
 
 /* Buffer size. When packets don't need to be sent immediately,
  * they accumulate in this buffer. It doesn't need to be very big
diff -u -r cli_c/src/netcore.c cli_c.my/src/netcore.c
--- cli_c/src/netcore.c Sat Jun 30 11:52:47 2001
+++ cli_c.my/src/netcore.c      Sun Jul  1 01:19:39 2001
@@ -272,7 +272,7 @@
        return;
   }
   else {
-     /* Normal recieve */
+     /* Normal receive */
      
      if (_pg_recv(&_pg_return.type,sizeof(_pg_return.type)))
        return;
@@ -490,8 +490,12 @@
 {
   int  numbytes;
   struct pghello ServerInfo;
+#ifdef WANT_IN
   struct hostent *he;
   struct sockaddr_in server_addr; /* connector's address information */
+#else
+  struct sockaddr_un server_addr; 
+#endif
   const char *hostname;
   int fd,i,j,args_to_shift;
   char *arg;
@@ -571,19 +575,25 @@
     return;
   }
 #else
+#if WANT_IN
   if ((he=gethostbyname(hostname)) == NULL) {  /* get the host info */
     clienterr("Error resolving server hostname");
     return;
   }
 #endif
+#endif
 
 
-
+#ifdef WANT_IN
   if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+#else
+  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+#endif
     clienterr("socket error");
     return;
   }
 
+#ifdef WANT_IN
   /* Try disabling the "Nagle algorithm" or "tinygram prevention" */
   tmp = 1;
   setsockopt(fd,6 /*PROTO_TCP*/,TCP_NODELAY,(void *)&tmp,sizeof(tmp));
@@ -598,6 +608,13 @@
   bzero(&(server_addr.sin_zero), 8);                /* zero the rest of the struct */
 
   if (connect(fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
+#else
+  server_addr.sun_family = AF_UNIX;
+  strcpy(server_addr.sun_path,hostname);
+  i = strlen(server_addr.sun_path) + sizeof(server_addr.sun_family);
+
+  if (connect(fd, (struct sockaddr *)&server_addr, i) == -1) {
+#endif
     clienterr("Error connecting to server");
     return;
   }
diff -u -r pgserver/net/request.c pgserver.my/net/request.c
--- pgserver/net/request.c      Tue Jun 26 14:31:27 2001
+++ pgserver.my/net/request.c   Sun Jul  1 01:36:16 2001
@@ -31,7 +31,13 @@
 #include <pgserver/common.h>
 #include <pgserver/pgnet.h>
 #include <pgserver/input.h>
+#ifdef WANT_IN
 #include <netinet/tcp.h>
+#else
+#include <sys/un.h>
+
+#define PG_REQUEST_SERVER "/tmp/.pgui"
+#endif
 
 #ifndef CONFIG_MAXPACKETSZ
 #define CONFIG_MAXPACKETSZ 6000000
@@ -274,7 +280,11 @@
 
 /* Bind the socket and start listening */
 g_error net_init(void) {
+#ifdef WANT_IN
   struct sockaddr_in server_sockaddr;
+#else
+  struct sockaddr_un server_sockaddr;
+#endif
   volatile int tmp;
 #ifdef WINDOWS
   WSADATA wsad;
@@ -294,9 +304,14 @@
     return mkerror(PG_ERRT_NETWORK,49);
 #endif
 
+#ifdef WANT_IN
   if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
+#else
+  if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+#endif
     return mkerror(PG_ERRT_NETWORK,50);
 
+#ifdef WANT_IN
   /* Try to avoid blocking the port up... */
   tmp = 1;
   setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&tmp,sizeof(tmp));
@@ -311,6 +326,13 @@
 
   if(bind(s, (struct sockaddr *)&server_sockaddr, 
      sizeof(server_sockaddr)) == -1)
+#else
+  server_sockaddr.sun_family = AF_UNIX;
+  strcpy(server_sockaddr.sun_path,PG_REQUEST_SERVER);
+  unlink(server_sockaddr.sun_path);
+  tmp = strlen(server_sockaddr.sun_path) + sizeof(server_sockaddr.sun_family);
+  if(bind(s, (struct sockaddr *)&server_sockaddr, tmp) == -1)
+#endif
     return mkerror(PG_ERRT_NETWORK,52);
 
   if(listen(s, REQUEST_BACKLOG) == -1)

Reply via email to