]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/fwp/fwp/tests/unixsocktest/unixserver.c
69584fc325029126a275f1568a8b6928670e4948
[frescor/frsh-forb.git] / src / fwp / fwp / tests / unixsocktest / unixserver.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <sys/un.h>
5 #include <unistd.h>
6 #include <errno.h>
7
8 int main()
9 {
10         int server_sockfd, client_sockfd;
11         socklen_t client_len;
12         struct sockaddr_un server_addr;
13         struct sockaddr_un client_addr;
14         int mlen;
15
16         char buf[20];
17
18         /*  Remove any old socket and create 
19          *  an unnamed socket for the server.  */
20         
21         unlink("server_socket");
22         server_sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
23
24         /*  Name the socket.  */
25
26         server_addr.sun_family = AF_UNIX;
27         strcpy(server_addr.sun_path, "server_socket");
28         bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
29
30         /*  Create a connection queue and wait for clients.  */
31
32         listen(server_sockfd, 5);
33         while(1) {
34                 client_sockfd = accept(server_sockfd, 
35                                        (struct sockaddr *)&client_addr, 
36                                        &client_len);
37 /*              if (client_sockfd < 0) {
38                         if (errno==EINTR) continue;
39                         printf("Accept error \n");
40                         goto out_err;
41                 }
42 */
43
44                 switch (fork())
45                 {
46                 case 0:
47                 close(server_sockfd);
48                 while (1) {     
49                         while ((mlen = recv(client_sockfd, buf, sizeof(buf), 
50                                             0)) == -1){
51                                 if (errno == EINTR) continue;
52                                 goto out_err;           
53                         }       
54                         
55                         while (send(client_sockfd, buf, mlen, 0) == -1){
56                                 if (errno == EINTR) continue;
57                                 goto out_err;
58                         }
59                 }
60       
61                 case -1:
62                         return -1;
63                 default:
64                         close(client_sockfd);
65                 }       
66         }
67         
68         close(server_sockfd);
69         close(client_sockfd);
70         return 0;
71
72 out_err:
73         perror("\n");
74         close(server_sockfd);
75         close(client_sockfd);
76         return -1;
77 }
78