]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
Added test for unix sockets - not finished
authorMartin Molnar <molnam1@fel.cvut.cz>
Tue, 15 Jan 2008 23:56:39 +0000 (00:56 +0100)
committerMartin Molnar <molnam1@fel.cvut.cz>
Tue, 15 Jan 2008 23:56:39 +0000 (00:56 +0100)
fwp/libfwp/include/fwp_msgq.h
fwp/libfwp/src/Makefile.omk
fwp/tests/unixclient.c [new file with mode: 0644]
fwp/tests/unixserver.c [new file with mode: 0644]

index 55b23c27673e710cfbc0edbfa361911cc970f97b..d9f11fc19a94bfa48b85ea90e8f2331813573f18 100644 (file)
@@ -14,8 +14,8 @@ struct fwp_msgq {
        
        /* For simplicity now */
        struct fwp_msgb* queue[FWP_MSGQ_SIZE];
-       unsigned int in;   /* add at offset (in % size) */ 
-       unsigned int out;  /* extracted from offset (out % size) */ 
+       unsigned int in;   /**< add at offset (in % size) */ 
+       unsigned int out;  /**< extracted from offset (out % size) */ 
        sem_t lock;
 };
 
index 6e0d649cd986c5a46bef6862e48a700f8a2fb86f..c11f2007acc5bfd28c80eb19fad131b0ae97e52f 100644 (file)
@@ -1,3 +1,7 @@
 lib_LIBRARIES = fwp
+CFLAGS = -Wall -D_REENTRANT -g
+LDFLAGS = -lpthread  -lrt
 fwp_SOURCES = fwp_util.c fwp_ac.c fwp_vres.c fwp_msgb.c fwp_msgq.c fwp_msg.c\
              fwp_proto.c fwp_ctable.c
+
+#lib_LOADLIBES+= pthread rt
diff --git a/fwp/tests/unixclient.c b/fwp/tests/unixclient.c
new file mode 100644 (file)
index 0000000..d8c1dad
--- /dev/null
@@ -0,0 +1,46 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int sockfd;
+       struct sockaddr_un address;
+       int result;
+       char msg[20];
+       
+       msglen = strlen(argv[1]) + 1;
+       if (msglen > sizeof(*msg))
+               msglen = sizeof(*msg);
+
+       strncpy(msg, argv[1], sizeof(*msg));
+
+       /*  Create a socket for the client.  */
+
+       sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+       /*  Name the socket, as agreed with the server.  */
+
+       addr.sun_family = AF_UNIX;
+       strcpy(addr.sun_path, "server_socket");
+
+       /*  Now connect our socket to the server's socket.  */
+
+       result = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
+
+       if(result == -1) {
+               perror("oops: client1");
+               return -1;
+       }
+
+       /*  We can now read/write via sockfd.  */
+       while (1) {
+               write(sockfd, msg, msglen);
+               read(sockfd, msg, msglen);
+               printf("%s\n", msg);
+       }       
+       close(sockfd);
+       return 0;
+}
diff --git a/fwp/tests/unixserver.c b/fwp/tests/unixserver.c
new file mode 100644 (file)
index 0000000..8a6ee70
--- /dev/null
@@ -0,0 +1,50 @@
+/*  Make the necessary includes and set up the variables.  */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int main()
+{
+    int server_sockfd, client_sockfd;
+    int server_len, client_len;
+    struct sockaddr_un server_address;
+    struct sockaddr_un client_address;
+
+/*  Remove any old socket and create an unnamed socket for the server.  */
+
+    unlink("server_socket");
+    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+/*  Name the socket.  */
+
+    server_address.sun_family = AF_UNIX;
+    strcpy(server_address.sun_path, "server_socket");
+    server_len = sizeof(server_address);
+    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
+
+/*  Create a connection queue and wait for clients.  */
+
+    listen(server_sockfd, 5);
+    while(1) {
+        char ch;
+
+        printf("server waiting\n");
+
+/*  Accept a connection.  */
+
+        client_len = sizeof(client_address);
+        client_sockfd = accept(server_sockfd, 
+            (struct sockaddr *)&client_address, &client_len);
+
+/*  We can now read/write to client on client_sockfd.  */
+
+        read(client_sockfd, &ch, 1);
+        ch++;
+        write(client_sockfd, &ch, 1);
+        close(client_sockfd);
+    }
+}
+