HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 162
define SOCKET_PATH
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 162 highlights
Using UNIX Domain Datagram Sockets Example Using UNIX Domain Datagram Sockets * */ #include #include #include #include #include #include #include #define SOCKET_PATH "/tmp/myserver" #define SOCKET_PATHCLNT "/tmp/my_af_unix_client" #define bzero(ptr, len) memset((ptr), NULL, (len)) int timeout(); main() { int sock; int j, slen, rlen; unsigned char sdata[2000];/* send data */ unsigned char rdata[2000];/* receive data */ struct sockaddr_un servaddr;/* address of server */ struct sockaddr_un clntaddr;/* address of client */ struct sockaddr_un from; int fromlen; /* Stop the program if not done in 2 minutes */ signal(SIGALRM, timeout); alarm((unsigned long) 120); /* Fork the server process to receive data from client */ printf("Client : Forking server\n"); if (fork() == 0 ) { execl("./server", "server", 0 ); printf("Cannot exec ./server.\n"); exit(1); } /* Initialize the send data */ for (j = 0; j < sizeof(sdata); j++) sdata[j] = (char) j; /* Create a UNIX datagram socket for client */ if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { perror("client: socket"); exit(2); } /* Client will bind to an address so the server will * get an address in its recvfrom call and use it to * send data back to the client. */ bzero(&clntaddr, sizeof(clntaddr)); clntaddr.sun_family = AF_UNIX; strcpy(clntaddr.sun_path, SOCKET_PATHCLNT); if (bind(sock, &clntaddr, sizeof(clntaddr)) < 0) { close(sock); 162 Chapter 7