HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 162

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

Using UNIX Domain Datagram Sockets
Example Using UNIX Domain Datagram Sockets
*
*/
#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
#define SOCKET_PATHCLNT "/tmp/my_af_unix_client"
#define bzero(ptr, len)
int
timeout();
main()
{
int
sock;
int
j, slen, rlen;
unsigned char
unsigned char
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
"/tmp/myserver"
memset((ptr), NULL, (len))
sdata[2000];/* send data */
rdata[2000];/* receive data */
*/
Chapter 7

Advertisement

Table of Contents
loading

Table of Contents