For example struct sockaddrun socaddr char socname serversoc bzerosocaddr

For example struct sockaddrun socaddr char socname

This preview shows page 20 - 24 out of 72 pages.

Image of page 20
ICT374 Networking and Sockets _____________________________________________________________________________________ Page 21 / 72 14. Example 1 (ser1.c & cli1.c). This example shows the minimum of what is required. Future examples will provide incremental improvements over this one. The client and the server use the stream-like Unix domain protocol to communicate. Since Unix domain protocol is not really a network protocol, the client and the server must run on the same machine. The server provides a very simple service: echo the message back to the client. /* * cli1.c - test for Unix domain stream socket (client part). * This is a very **crude** program. */ #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <string.h> #include <stdio.h> #include <string.h> char serversockname[]="serversocket"; int main() { int sd, n; char buf[256]; struct sockaddr_un server_addr; char mesg[]="hello from client"; // create client socket sd = socket(PF_UNIX, SOCK_STREAM, 0); // construct the server address server_addr.sun_family = AF_UNIX; strcpy(server_addr.sun_path, serversockname); // request connection to serversoc connect(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)); // send a message to server write(sd, mesg, strlen(mesg)+1); // read the result back from server read(sd, buf, sizeof(buf)); // print out the result printf("server echoed '%s'\n", buf); exit(0); }
Image of page 21
ICT374 Networking and Sockets _____________________________________________________________________________________ Page 22 / 72 /* * ser1.c test for Unix domain stream socket (server part) * This is a very **crude** program. */ #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <string.h> #include <errno.h> char serversockname[]="serversocket"; main() { int sd, nsd, n; char buf[256]; struct sockaddr_un server_addr; struct sockaddr_un client_addr; int client_addr_len; // set up listening socket sd sd = socket(PF_UNIX, SOCK_STREAM, 0); // construct the server address server_addr.sun_family = PF_UNIX; strcpy(server_addr.sun_path, serversockname); // bind server address to socket sd bind(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)); // set socket sd to a listening socket listen(sd, 1); // accept connection request client_addr_len = sizeof(client_addr); nsd = accept(sd, (struct sockaddr *)&client_addr, &client_addr_len); // data transfer on connected socket ns n = read(nsd, buf, sizeof(buf)); write(nsd, buf, n); exit(0); }
Image of page 22
ICT374 Networking and Sockets _____________________________________________________________________________________ Page 23 / 72
Image of page 23
Image of page 24

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture