Network Programming Lab Manual
Network Programming Lab Manual
Network Programming Lab Manual
Prepared By:
G.Sampath Kumar. M.Tech, M.I.S.T.E, M.C.S.I.
Asst. Professor
1
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY
HYDERABAD
IV Year B.Tech. CSE -I Sem T P C
0 3 2
NETWORK PROGRAMMING LAB
Objectives:
To teach students various forms of IPC through Unix and socket Programming
Week1.
Implement the following forms of IPC.
a)Pipes
b)FIFO
Week2.
Implement file transfer using Message Queue form of IPC
Week3.
Write a program to create an integer variable using shared memory concept and increment the variable
simultaneously by two processes. Use semaphores to avoid race conditions
Week4.
Design TCP iterative Client and server application to reverse the given input sentence
Week5.
Design TCP iterative Client and server application to reverse the given input sentence
Week6.
Design TCP client and server application to transfer file
Week7.
Design a TCP concurrent server to convert a given text into upper case using multiplexing system call “select”
Week8.
Design a TCP concurrent server to echo given set of sentences using poll functions
Week9.
Design UDP Client and server application to reverse the given input sentence
Week10
Design UDP Client server to transfer a file
Week11
Design using poll client server application to multiplex TCP and UDP requests for converting a given text into
upper case.
Week12
Design a RPC application to add and subtract a given pair of integers
Reference Book:
1. Advance UNIX Programming Richard Stevens, Second Edition Pearson Education
2. Advance UNIX Programming, N.B. Venkateswarlu, BS Publication.
2
Week1.
Implement the following forms of IPC.
a) Pipes b) FIFO
a) Named Pipes
Half Duplex
---------------------------------------------------------------------------------------------------------------------
half Duplex.h
#define HALF_DUPLEX "/tmp/halfduplex"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "half_duplex.h" /* For name of the named-pipe */
#include <stdlib.h>
3
buf[numread] = '0';
---------------------------------------------------------------------------------------------------------------------
halfduplex1.h
#define HALF_DUPLEX "/tmp/halfduplex1"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include "half_duplex1.h" /* For name of the named-pipe */
#include <stdlib.h>
if (argc != 2) {
printf("Usage : %s <string to be sent to the server>n", argv[0]);
exit (1);
}
4
/* Open the pipe for writing */
fd = open(HALF_DUPLEX, O_WRONLY);
Execution Steps:
1. Named Pipes:
a) Half Duplex.
1. Run the server:
% cc hd_server.c
% mv a.out hd_server
%./hd_server &
The server program will block here, and the shell will return control to the
command line.
2. Run the client:
% cc hd_client
% mv a.out hd_client
%./hd_client hello
3. The server prints the string read and terminates:
Output:-
Half Duplex Server : Read From the pipe : hello
Half Duplex Server : Converted String : HELLO
---------------------------------------------------------------------------------------------------------------------
5
b) Named Pipe:
Full Duplex:
full duplex.h
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
fd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
6
/* Open the second named pipe for writing */
wrfd = open(NP2, O_WRONLY);
buf[numread] = '0';
/*
* * Write the converted string back to the second
* * pipe
* */
write(wrfd, buf, strlen(buf));
}
---------------------------------------------------------------------------------------------------------------------
7
fd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
if (argc != 2) {
printf("Usage : %s <string to be sent to the server>n", argv[0]);
exit (1);
}
rdbuf[numread] = '0';
8
Execution Steps:
b) Full Duplex.
1. Run the server:
% cc fd_server.c
% mv a.out fd_server
%./fd_server &
The server program will block here, and the shell will return control to the
command line.
2. Run the client:
% cc fd_client
% mv a.out fd_client
%./fd_client hello
3. The client program will send the string to server and block on the read
to await the server's response.
9
Week2.
Implement file transfer using Message Queue form of IPC
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
/*
* Declare the message structure.
*/
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
/*
* We'll send message type 1
*/
sbuf.mtype = 1;
buf_length = strlen(sbuf.mtext) + 1 ;
/*
* Send a message.
*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
exit(0);
}
---------------------------------------------------------------------------------------------------------------------
11
message_rec.c -- receiving the above message
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
/*
* Declare the message structure.
*/
main()
{
int msqid;
key_t key;
message_buf rbuf;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
/*
* Receive an answer of message type 1.
*/
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
12
exit(1);
}
/*
* Print the answer.
*/
printf("%s\n", rbuf.mtext);
exit(0);
}
13
Execution Steps:
14
Week3.
Write a program to create an integer variable using shared memory concept and increment
the variable simultaneously by two processes. Use semaphores to avoid race conditions
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void) {
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
} else { /* Parent */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}
15
Execution steps:
[sampath@localhost ipc]$cc shared_mem.c
[sampath@localhost ipc]$ ./a.out
Shared Memory ID=65537Child pointer 3086680064
Child value=1
Shared Memory ID=65537Parent pointer 3086680064
Parent value=1
Parent value=42
Child value=42
---------------------------------------------------------------------------------------------------------------------
16
Week4.
Design TCP iterative Client and server application to reverse the given input sentence
Week5.
Design TCP iterative Client and server application to reverse the given input sentence
Week6.
Design TCP client and server application to transfer file
tcpserver.c
/***************************************************************************
* FILENAME :demoserver.c
* DESCRIPTION:Contains Code for a server,that will accept
* a string from a client process , prints the string and the
* IP Address of the client .(Shows a typical ITERATIVE SERVER )
* Invoke the Executable as a.out
* Copyright 2007 Aricent
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
17
listensocket = socket(AF_INET, SOCK_STREAM, 0 );
if (listensocket < 0 )
{
perror("socket" );
exit(1);
}
memset(&serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);/*PORT NO*/
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);/*ADDRESS*/
retbind=bind(listensocket,(struct sockaddr*)&serveraddress,
sizeof(serveraddress));
/*Check the return value of bind for error*/
if(-1==retbind)
{
perror("BIND ERROR\n");
exit(1);
}
listen(listensocket,5);
/*Beginning of the Main Server Processing Loop*/
for (;;)
{
printf("Server:I am waiting-----Start of Main Loop\n");
len=sizeof(cliaddr);
connectionsocket=accept(listensocket,
(struct sockaddr*)&cliaddr,&len);
if (connectionsocket < 0)
{
if (errno == EINTR)
printf("Interrupted system call ??");
continue;
}
printf("Connection from %s\n",
inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)));
readstring(connectionsocket , databuf);
close(connectionsocket);
printf("Finished Serving One Client\n");
}
18
/********************************************************************
* FUNCTION NAME:readstring
* DESCRIPTION: Reads the string sent by the client over the
* socket and stores it in the array fname .
* NOTES : No Error Checking is done .
* RETURNS :void
*********************************************************************/
void readstring(
int connectionsocket, /*Socket Descriptor*/
char *fname) /*Array , to be populated by the string from client*/
/********************************************************************/
{
int pointer=0,n;
int len=0,a,b;
char rev[50],temp[50],temp1[50];
int k,i;
while ((n=read(connectionsocket,(fname + pointer),1024))>0)
{
pointer=pointer+n;
}
fname[pointer]='\0';
//strcpy(temp,fname);
k=strlen(fname);
// for(k=0;temp[k]!=0;k++);
// len=k;
a=0;
for(i=k-1;i>=0;i--)
temp[a++]=fname[i];
temp[a]='\0';
}
/**********************************************************************/
---------------------------------------------------------------------------------------------------------------------
19
tcpclient.c
/***************************************************************************
* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */
/*************************************************************************/
{ int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
21
}
printf("String : %s sent to server \n",buffer);
}
/****************************************************************************/
22
Execution Steps:
2. TCP
a) Client Server Application.
1.Compiling and running server.
[user@localhost week9]$ cc tcpserver.c
[user@localhost week9]$ mv a.out tcpserver
[user@localhost week9]$ ./tcpserver
Server:I am waiting-----Start of Main Loop
Connection from 127.0.0.1
enter the string
Server :Received Network Programming
23
Week7.
Design a TCP concurrent server to convert a given text into upper case using multiplexing
system call “select”
tcpservselect01.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXLINE 100
#define SERV_PORT 13153
/* include fig02 */
for ( ; ; ) {
printf("Server:I am waiting-----Start of Main Loop\n");
rset = allset; /* structure assignment */
nready = select(maxfd+1, &rset, NULL, NULL, NULL);
#ifdef NOTDEF
printf("new client: %s, port %d\n",
inet_ntop(AF_INET, &cliaddr.sin_addr, buf, NULL),
ntohs(cliaddr.sin_port));
#endif
if (--nready <= 0)
25
continue; /* no more readable descriptors
*/
}
{
printf("\n output at server\n");
for(k=0;line[k]!='\0';k++)
printf("%c",toupper(line[k]));
---------------------------------------------------------------------------------------------------------------------
26
tcpclient.c
/***************************************************************************
* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
sendstring(sd,text);
close(sd);
return 0;
}
/************************************************************************
* FUNCTION NAME:sendstring
* DESCRIPTION: sends a string over the socket .
* NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */
/*************************************************************************/
{ int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
28
}
printf("String : %s sent to server \n",buffer);
}
/****************************************************************************/
Execution Steps:
b) Concurrent Server Application Using Select.
Compiling and running server.
output at server
A B C DServer:I am waiting-----Start of Main Loop
output at server
A B C DServer:I am waiting-----Start of Main Loop
Server:I am waiting-----Start of Main Loop
29
Week8.
Design a TCP concurrent server to echo given set of sentences using poll functions
tcpservpoll01.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <limits.h> /* for OPEN_MAX */
#include <poll.h>
#include <errno.h>
#define MAXLINE 100
#define SERV_PORT 13154
#define POLLRDNORM 5
#define INFTIM 5
#define OPEN_MAX 5
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
listen(listenfd, 5);
30
client[0].fd = listenfd;
client[0].events = POLLRDNORM;
for (i = 1; i < OPEN_MAX; i++)
client[i].fd = -1; /* -1 indicates available entry */
maxi = 0; /* max index into client[] array */
/* end fig01 */
/* include fig02 */
for ( ; ; ) {
client[i].events = POLLRDNORM;
if (i > maxi)
maxi = i; /* max index in client[] array */
if (--nready <= 0)
continue; /* no more readable descriptors
*/
}
strcpy(line," ");
}
if (--nready <= 0)
break; /* no more readable descriptors
*/
}
}
}
}
/* end fig02 */
---------------------------------------------------------------------------------------------------------------------------
-----------------
32
democlient.c
/***************************************************************************
* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
sendstring(sd,text);
close(sd);
return 0;
}
/************************************************************************
* FUNCTION NAME:sendstring
* DESCRIPTION: sends a string over the socket .
* NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */
/*************************************************************************/
{ int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd , buffer+byteswritten,(n-byteswritten));
34
byteswritten+=written;
}
printf("String : %s sent to server \n",buffer);
}
/****************************************************************************/
---------------------------------------------------------------------------------------------------------------------
35
c) Concurrent Server Application Using Poll.
36
Week9.
Design UDP Client and server application to reverse the given input sentence
Week10
Design UDP Client server to transfer a file
Week11
Design using poll client server application to multiplex TCP and UDP requests for
converting a given text into upper case.
udp_server.c
/******************************************************************************
* FILENAME : uechos.c
* DESCRIPTION:Contains Code for a echo server , that will accept data
* from a client process and sends that data back to client, using UDP
* Invoke the Executable as a.out
* Copyright 2007 Aricent
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
{
int sd,n,ret;
struct sockaddr_in
37
serveraddress,cliaddr;
socklen_t length;
char clientname[MAXNAME],datareceived[BUFSIZE];
for(;;)
{
printf("I am waiting\n");
/*Received a datagram*/
length=sizeof(cliaddr);
n=recvfrom(sd,datareceived,BUFSIZE,0,
(struct sockaddr*)&cliaddr , &length);
---------------------------------------------------------------------------------------------------------------------
38
udp_client.c
/***************************************************************************
* FILENAME : uechoc.c
* DESCRIPTION:Contains Code for a echo client , that will accept data
* from the user(keyboard) and sens that data to a echo server process
* and prints the received data back on the screen .(UDP)
* Invoke the Executable as a.out ServerIP ServerPort
* Copyright 2007 Aricent
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#define BUFSIZE 512
static void sig_usr(int);
void str_cli(FILE *fp , int sockfd , struct sockaddr *server , socklen_t len);
/************************************************************************
* FUNCTION NAME:sig_usr
* DESCRIPTION: Signal Handler for Trappinf SIGPIPE
* NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
static void sig_usr(
int signo) /*Signal Number*/
/************************************************************************/
{
char *strpipe="RECEIVED SIGPIPE - ERROR";
char *strctrl="RECEIVED CTRL-C FROM YOU";
if(signo==SIGPIPE)
{
write(1,strpipe,strlen(strpipe));
exit(1);
}
else if(signo==SIGINT)
{
write(1,strctrl,strlen(strctrl));
exit(1);
}
40
}
/************************************************************************
* FUNCTION NAME:str_cli
* DESCRIPTION: Main Client Processing (Select waits for readiness of
* connection socket or stdin
* NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
int maxdes,n;
fd_set rset;
char sendbuf[BUFSIZE] , recvbuf[BUFSIZE] ,servername[100];
struct sockaddr_in serveraddr;
socklen_t slen;
FD_ZERO(&rset);
maxdes=(sockfd>fileno(fp)?sockfd+1:fileno(fp)+1);
for(;;){
FD_SET(fileno(fp) , &rset);
FD_SET(sockfd , &rset);
select(maxdes,&rset,NULL,NULL,NULL);
if(FD_ISSET(sockfd , & rset))
{
slen=sizeof(serveraddr);
n=recvfrom(sockfd,recvbuf,BUFSIZE,0,
(struct sockaddr*)&serveraddr,&slen);
printf("Data Received from server %s:\n",
inet_ntop(AF_INET,&serveraddr.sin_addr,
servername,sizeof(servername)));
write(1,recvbuf,n);
printf("Enter Data For the server\n");
}
if(FD_ISSET(fileno(fp) , & rset))
{
41
/*Reading data from the keyboard*/
fgets(sendbuf,BUFSIZE,fp);
n = strlen (sendbuf);
/*Sending the read data over socket*/
sendto(sockfd,sendbuf,n,0,to,length);
printf("Data Sent To Server\n");
}
}
}
/**************************************************************************/
---------------------------------------------------------------------------------------------------------------------
42
5. UDP Client Server Application.
Compiling and running server.
[user@localhost week9]$ cc udp_server.c
[user@localhost week9]$ mv a.out udp_server
[user@localhost week9]$ ./ udp_server
I am waiting
Data Received from 127.0.0.1
I have received abcd efgh
rev is
hgfe dcba
I am waiting
Compiling and running client.
user@localhost week9]$ cc udp_client.c
[user@localhost week9]$ mv a.out udp_client
[user@localhost week9]$ ./ udp_client 127.0.0.1 11710
Client Starting service
Enter Data For the server
abcd efgh
Data Sent To Server
Data Received from server 127.0.0.1:
abcd efgh
Enter Data For the server
43
Week12
Design a RPC application to add and subtract a given pair of integers
rpctime.x
/*****************************************************************************/
/*** rpctime.x ***/
/*** ***/
/*** SPECIFICATION FILE FOR RPC TO DEFINE SERVER PROCEDURE AND
ARGUMENTS ***/
/*****************************************************************************/
program RPCTIME
{
version RPCTIMEVERSION
{
long GETTIME() = 1;
} = 1;
} = 2000001;
---------------------------------------------------------------------------------------------------------------------
44
rpctime.h
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _RPCTIME_H_RPCGEN
#define _RPCTIME_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
#else /* K&R C */
#define GETTIME 1
extern long * gettime_1();
extern long * gettime_1_svc();
extern int rpctime_1_freeresult ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_RPCTIME_H_RPCGEN */
---------------------------------------------------------------------------------------------------------------------
45
rpctime_client
/* rpctime_client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** rpctime_client.c ***/
/*** ***/
/*****************************************************************************/
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "rpctime.h"
void
rpctime_1(char *host)
{
CLIENT *clnt;
46
long *result_1;
char *gettime_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, RPCTIME, RPCTIMEVERSION, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
rpctime_1 (host);
exit (0);
}
rpctime_cntl.c
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
long *
gettime_1(void *argp, CLIENT *clnt)
{
static long clnt_res;
---------------------------------------------------------------------------------------------------------------------
48
rpctime_server.c
/* rpctime_server.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** rpctime_server.c ***/
/*** ***/
/*****************************************************************************/
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "rpctime.h"
long *
gettime_1_svc(void *argp, struct svc_req *rqstp)
{
static long result;
49
time(&result);
return &result;
}
---------------------------------------------------------------------------------------------------------------------
50
rpctime_svc.c
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#include "rpctime.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif
static void
rpctime_1(struct svc_req *rqstp, register SVCXPRT *transp)
{
union {
int fill;
} argument;
char *result;
xdrproc_t _xdr_argument, _xdr_result;
char *(*local)(char *, struct svc_req *);
switch (rqstp->rq_proc) {
case NULLPROC:
(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
return;
case GETTIME:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_long;
local = (char *(*)(char *, struct svc_req *)) gettime_1_svc;
break;
default:
svcerr_noproc (transp);
return;
}
memset ((char *)&argument, 0, sizeof (argument));
51
if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
svcerr_decode (transp);
return;
}
result = (*local)((char *)&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
svcerr_systemerr (transp);
}
if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
fprintf (stderr, "%s", "unable to free arguments");
exit (1);
}
return;
}
int
main (int argc, char **argv)
{
register SVCXPRT *transp;
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL) {
fprintf (stderr, "%s", "cannot create udp service.");
exit(1);
}
if (!svc_register(transp, RPCTIME, RPCTIMEVERSION, rpctime_1, IPPROTO_UDP)) {
fprintf (stderr, "%s", "unable to register (RPCTIME, RPCTIMEVERSION,
udp).");
exit(1);
}
svc_run ();
fprintf (stderr, "%s", "svc_run returned");
52
exit (1);
/* NOTREACHED */
}
---------------------------------------------------------------------------------------------------------------------
53
6. RPC Application.
Step 1:
[user@localhost $]$ rpcgen –C rpctime.x.
This creates rpctime.h, rpctime_clnt.c, rpctime_svc.c files in the folder
Step 2:
[user@localhost $]$cc –c rpctime_client.c –o rpctime_clien.o
Step 3:
[user@localhost $]$cc –o client rpctime_client.o rpctime_clnt.o -lnsl
Step 4:
[user@localhost $]$cc –c rpctime_server.c –o rpctime_server.o
Step 5:
[user@localhost $]$cc –o server rpctime_server.o rpctime_svc.o -lnsl
[root@localhost $]$./server &
[1] 7610
[root@localhost $]$./client 127.0.0.1
1277628700 |Sun Jun 27 14:21:40 2010
[root@localhost $]$./client 127.0.0.1
1277628718 |Sun Jun 27 14:21:58 2010
54