HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 7 Using UNIX Domain Datagram Sockets

Example Using UNIX Domain Datagram Sockets

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

NOTE: These programs are provided as examples only of UNIX Domain datagram socket usage and are not Hewlett-Packard supported products.

These programming examples demonstrate how to set up and use UNIX Domain datagram sockets. These sample programs can be found in the /usr/lib/demos/networking/af_unix directory. The client program is intended to run in conjunction with the server program.

This example shows how to create UNIX Domain datagram sockets and how to set up address structures for the sockets. In this example the client process sends 2000 bytes of data to the server (five times). The server process can receive data from any other process and will echo the data back to the sender.

The source code for these two programs follows.

/*
* AF_UNIX datagram server process
*
* This is an example program that demonstrates the use of
* AF_UNIX datagram sockets as a BSD Sockets mechanism. This
* program contains the server and is intended to operate in
* conjunction with the client program.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <stdio.h>
#include <signal.h>
#include <netdb.h>

#define SOCKET_PATH "/tmp/myserver"
#define bzero(ptr, len) memset((ptr), NULL, (len))

int timeout();
main()
{
int sock;
int slen, rlen, expect;
unsigned char sdata[5000];
struct sockaddr_un servaddr; /* address of server */
struct sockaddr_un from;
int fromlen;
/* Escape hatch so blocking calls don't wait forever */
signal(SIGALRM,timeout);
alarm((unsigned long) 120);
/* Create a UNIX datagram socket for server */
if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
perror("server: socket");
exit(1);
}
/* Set up address structure for server socket */

bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, SOCKET_PATH);

if (bind(sock, &servaddr, sizeof(servaddr)) < 0) {
close(sock);
perror("server: bind");
exit(2);
}
/* Receive data from anyone, echo back data to the sender
* Note that fromlen is passed as pointer so recvfrom
* call can return the size of the returned address.
*/
expect = 5 * 2000;
while (expect > 0) {
fromlen = sizeof(from);
rlen = recvfrom(sock, sdata, 2000, 0, &from, &fromlen);
if (rlen == -1) {
perror("server : recv\n");
exit(3);
} else {
expect -= rlen;
printf("server : recv'd %d bytes\n",rlen);
slen = sendto(sock, sdata, rlen, 0, &from,
fromlen);
if (slen <0) {
perror ("server : sendto\n");
exit (4);
}
}
}
/* Use unlink to remove the file (inode) so that the
* name will be available for the next run.
*/
unlink(SOCKET_PATH);
close(sock);
printf("Server done\n");
exit(0);
}
timeout() /* escape hatch so blocking calls don't wait forever */
{
printf( "alarm received — stopping server\n" );
fprintf(stderr, "stopping the server process\n");
exit(5);
}
/*
* AF_UNIX datagram client process
*
* This is an example program that demonstrates the use of
* AF_UNIX datagram sockets as a BSD Sockets mechanism. This
* contains the client, and is intended to operate in
* conjunction with the server program.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <stdio.h>
#include <signal.h>
#include <netdb.h>
#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);
perror("client: bind");
exit(3);
}

/* Set up address structure for server socket */
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, SOCKET_PATH);

for (j = 0; j < 5; j++) {
sleep(1);
slen = sendto(sock, sdata, 2000, 0,
(struct sockaddr *) &servaddr,
sizeof(servaddr));
if (slen<0) {
perror("client: sendto");
exit(4);
}
else {
printf("client : sent %d bytes\n", slen);
fromlen = sizeof(from);
rlen = recvfrom(sock, rdata, 2000, 0, &from,
&fromlen);
if (rlen == -1) {
perror("client: recvfrom\n");
exit(5);
} else
printf("client : received %d bytes\n", rlen);
}
}
/* Use unlink to remove the file (inode) so that the
* name will be available for the next run.
*/
sleep(1);
unlink(SOCKET_PATHCLNT);
close(sock);
printf("Client done\n");
exit(0);
}
timeout() /* escape hatch so blocking calls don't wait forever */
{
printf( "alarm went off — stopping client\n" );
fprintf(stderr, "stopping the client process\n");
exit(6);
}
© 1997 Hewlett-Packard Development Company, L.P.