22.07.2013 Views

Download File

Download File

Download File

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

IV YEAR B.Tech<br />

Network Programming<br />

Prepared<br />

By<br />

R.Venkata Subbaiah BE.M.Tech.<br />

Associate Professor<br />

IC RNEC@Development Cell<br />

Department of CSE<br />

RAO & NAIDU ENGINEERING COLLEGE<br />

ONGOLE<br />

NP Lab Manual , RNEC P a g e | 1<br />

For more Details access portals : ravinuthalavs.webs.com<br />

Ravinuthalavs.blogspot.com


Network Programming<br />

Introduction to Inter process Communication :<br />

Pipes<br />

Introduction:<br />

There are two types of pipes for two-way communication: anonymous pipes and named pipes.<br />

Anonymous pipes enable related processes to transfer information to each other. Typically, an anonymous pipe<br />

is used for redirecting the standard input or output of a child process so that it can exchange data with its<br />

parent process. To exchange data in both directions (duplex operation), you must create two anonymous pipes.<br />

The parent process writes data to one pipe using its write handle, while the child process reads the data from<br />

that pipe using its read handle. Similarly, the child process writes data to the other pipe and the parent process<br />

reads from it. Anonymous pipes cannot be used over a network, nor can they be used between unrelated<br />

processes.<br />

Properties of Pipe:<br />

1) Pipes do not have a name. For this reason, the processes must share a parent process. This is the main<br />

drawback to pipes. However, pipes are treated as file descriptors, so the pipes remain open even after fork<br />

and exec.<br />

2) Pipes do not distinguish between messages; they just read a fixed number of bytes. Newline (\n) can be<br />

used to separate messages. A structure with a length field can be used for message containing binary data.<br />

3) Pipes can also be used to get the output of a command or to provide input to a command.<br />

Creation of Pipes<br />

Since A pipe provides a one-way flow of data.<br />

Int pipe (int * filedes);<br />

Int pipefd[2]; /* pipefd[0] is opened for reading;pipefd[1] is opened for writing */<br />

NP Lab Manual , RNEC P a g e | 2<br />

For more Details access portals : ravinuthalavs.webs.com<br />

Ravinuthalavs.blogspot.com


o<br />

after call to fork<br />

o after both calls to dup2<br />

NP Lab Manual , RNEC P a g e | 3<br />

For more Details access portals : ravinuthalavs.webs.com<br />

Ravinuthalavs.blogspot.com<br />

I<br />

I


o after all calls to close<br />

If the parent wants to receive data from the child, it should close fd1, and the child should<br />

close fd0. If the parent wants to send data to the child, it should close fd0, and the child<br />

should close fd1. Since descriptors are shared between the parent and child, we should<br />

always be sure to close the end of pipe we aren't concerned with. On a technical note, the<br />

EOF will never be returned if the unnecessary ends of the pipe are not explicitly closed.<br />

NP Lab Manual , RNEC P a g e | 4<br />

For more Details access portals : ravinuthalavs.webs.com<br />

Ravinuthalavs.blogspot.com


WEEK 1:<br />

Aim: Implement the pipe using fork () /* file name pipe.c */<br />

# include <br />

# include <br />

# include <br />

# define MAXLINE 4096<br />

int main()<br />

{<br />

int n,fd[2];<br />

pid_t pid;<br />

char line[MAXLINE];<br />

if(pipe(fd)


Output:<br />

[student@localhost ~]$ vi pipe.c<br />

[student@localhost ~]$ cc pipe.c -o pipe<br />

[student@localhost ~]$ ./pipe<br />

Hello world<br />

NP Lab Manual , RNEC page: 6


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 2:<br />

Aim: Implement the pipe using popen (), close () /* file name pipeopen.c */<br />

# include <br />

# include <br />

# include <br />

# define MAXLINE 4096<br />

# define PAGER "${PAGER:-more}"<br />

int main(int argc,char*argv[])<br />

{<br />

char line[MAXLINE];<br />

FILE *fpin,*fpout;<br />

if(argc!=2)<br />

{<br />

perror("\n usage :- a.out ");<br />

exit(0);<br />

}<br />

if((fpin=fopen(argv[1],"r"))==NULL)<br />

{<br />

printf("\nUnable to open %s",argv[1]);<br />

}<br />

if((fpout=popen(PAGER,"w"))==NULL)<br />

perror("popen error...");<br />

while(fgets(line,MAXLINE,fpin))<br />

{<br />

if(fputs(line,fpout)==EOF)<br />

perror("\n fput error...");<br />

}<br />

if(ferror(fpin))<br />

perror("fgets error...");<br />

if(pclose(fpout)==-1)<br />

perror("pclose error..");<br />

exit(0);<br />

}<br />

NP Lab Manual , RNEC page: 7


*<br />

Compiling………….<br />

$ cc pipeopen.c – o pipeopen<br />

Execution…………..<br />

$ pipeopen <br />

*/<br />

NP Lab Manual , RNEC page: 8


Output:<br />

[student@localhost ~]$ vi pipeopen.c<br />

[student@localhost ~]$ cc pipeopen.c -o pipeopen<br />

[student@localhost ~]$ ./pipeopen myfile.txt<br />

This is the simple text in myfile<br />

To create a file: $cat > filename<br />

NP Lab Manual , RNEC page: 9


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

Aim: Implement the FIFO form of IPC /* file name makefifo.c */<br />

# include <br />

# include <br />

# include <br />

# include <br />

int main()<br />

{<br />

int fd;<br />

char buff[100];<br />

/*creating FIFO */<br />

if((mkfifo("./MyFifo",S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO))0)<br />

printf("%s",buff);<br />

/*creating write process */<br />

fd=open("./MyFifo",O_WRONLY);<br />

write(fd,"Hello world",12);<br />

close(fd);<br />

}<br />

/*<br />

Compiling………….<br />

$ cc makefifo.c – o makefifo<br />

Execution…………..<br />

$ makefifo<br />

*/<br />

NP Lab Manual , RNEC page: 10


Output:<br />

[student@localhost ~/khan]$ vi makefifo.c<br />

[student@localhost ~/khan]$ cc makefifo.c -o makefifo<br />

[student@localhost ~/khan]$ ./makefifo<br />

[student@localhost ~/khan]$ ./makefifo<br />

mkfifo error...: <strong>File</strong> exists<br />

NP Lab Manual , RNEC page: 11


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 3:<br />

Aim: Implement file transfer using Message Queue form of IPC /* file msgsendQ.c */<br />

# include <br />

# include <br />

# include <br />

# include <br />

# include <br />

# include <br />

struct message<br />

{<br />

long msg_type;<br />

char msg_data[1024];<br />

};<br />

int main(int arc,char*argv[])<br />

{<br />

//message structure<br />

FILE*f;<br />

int x,y,i=0;<br />

char ch;<br />

int key=1234;<br />

int msg_id;<br />

struct message msg_obj1={100,"I"};<br />

//function to put file to message Q<br />

if((f=fopen(argv[1],"r"))==NULL)<br />

perror("\nUnable to open file for reading...");<br />

else<br />

{<br />

while((ch=getc(f))!=EOF)<br />

{<br />

msg_obj1.msg_data[i++]=ch; //writing to msg_data<br />

}<br />

NP Lab Manual , RNEC page: 12


}<br />

fclose(f); //closing file<br />

//creating msg q id<br />

if((msg_id=msgget(key,IPC_CREAT|0644))==-1)<br />

perror("\nUnable to get message id...");<br />

else<br />

printf("\nmsgid=%d",msg_id);<br />

/*writing message to the q */<br />

if((x=msgsnd(msg_id,&msg_obj1,strlen(msg_obj1.msg_data),IPC_NOWAIT))<br />

==-1)<br />

perror("\nUnable to send message...");<br />

else<br />

printf("\nSend Msg Success : return %d",x);<br />

}<br />

return 0;<br />

/*<br />

Compiling………….<br />

$CC msgsndQ.c -o msgsndQ<br />

Execution…………..<br />

$msgsndQ <br />

*/<br />

NP Lab Manual , RNEC page: 13


Output:<br />

[student@localhost ~]$ cc msgsndQ.c -o msgsndQ<br />

[student@localhost ~]$ ./msgsndQ pipe.c<br />

msgid=0<br />

Send Msg Success : return 0[student@localhost ~]$<br />

NP Lab Manual , RNEC page: 14


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/* file msgrecvQ.c */<br />

# include <br />

# include <br />

# include <br />

# include <br />

# include <br />

# include <br />

//message structure<br />

struct message<br />

{<br />

long msg_type;<br />

char msg_data[1024];<br />

};<br />

int main(int argc,char*argv[])<br />

{<br />

FILE*f;<br />

int x,y,i=0;<br />

char ch;<br />

struct message msg_obj2={100,"M"};<br />

int msg_id;<br />

//creating msg q id<br />

if((msg_id=msgget(1234,0644))==-1)<br />

perror("\nUnable to get message id...");<br />

else<br />

printf("\nmsgid=%d",msg_id);<br />

/* rec message from q*/<br />

if((y=msgrcv(msg_id,&msg_obj2,1024,100,MSG_NOERROR))==-1)<br />

perror(":- msgrcv error...");<br />

else<br />

printf("\nRec Bytes : %d",y);<br />

NP Lab Manual , RNEC page: 15


}<br />

if((f=fopen(argv[1],"w"))==NULL)<br />

perror("\nUnable to open file for writing...");<br />

else<br />

{<br />

for(i=0;msg_obj2.msg_data[i]!=0;i++)<br />

{<br />

putc(msg_obj2.msg_data[i],f);<br />

}<br />

fclose(f); //closing file<br />

}<br />

return 0;<br />

/*<br />

Compiling………….<br />

$CC msgrecvQ.c -o msgrecvQ<br />

Execution…………..<br />

$ msgrecvQ <br />

*/<br />

NP Lab Manual , RNEC page: 16


Output:<br />

[student@localhost ~]$ cc msgrecvQ.c -o msgrecvQ<br />

[student@localhost ~]$ ./msgrecv recvpipe.c<br />

msgid=0<br />

Rec Bytes : 415[student@localhost ~]$ cat recvpipe.c<br />

/* file name pipe.c */<br />

# include <br />

# include <br />

# include <br />

# define MAXLINE 4096<br />

int main()<br />

{<br />

int n,fd[2];<br />

pid_t pid;<br />

char line[MAXLINE];<br />

if(pipe(fd)


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 4:<br />

Aim: Write a program to create an integer variable using shared memory concept and<br />

increment the variable simultaniously two processes. Use semaphores to avoid race<br />

conditions. /*file name sem.c */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

# include <br />

#define SHMSZ 4 /* Good for integer */<br />

#define BUF_SZ 16<br />

#define SHM_KEY 1234<br />

#define SEM_KEY 1235<br />

#define CHILD_INCREMENT_COUNT 67787<br />

#define PARENT_INCREMENT_COUNT 84823<br />

union semun<br />

{<br />

int val; /* value for SETVAL */<br />

struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */<br />

unsigned short *array; /* array for GETALL, SETALL */<br />

/* Linux specific part: */<br />

struct seminfo *__buf; /* buffer for IPC_INFO */<br />

};<br />

NP Lab Manual , RNEC page: 18


main()<br />

{<br />

int shmid;<br />

int semid;<br />

key_t key;<br />

int *child_shm, *parent_shm;<br />

union semun arg;<br />

int i;<br />

struct sembuf operations[1];<br />

int status;<br />

int pid;<br />

/* Create the shared segment.& semaphore. */<br />

if ((shmid = shmget(SHM_KEY, SHMSZ, IPC_CREAT | 0666)) < 0)<br />

{<br />

perror("shmget");<br />

exit(1);<br />

}<br />

if ((semid = semget(SEM_KEY, 1, IPC_CREAT | 0666)) < 0)<br />

{<br />

perror("semget");<br />

exit(1);<br />

}<br />

/*<br />

* Initialize the semaphore to value 1. The idea is multiple processes<br />

* do semop() of -1. So only one is allowed in critical section.<br />

* initialize the shm to 0.<br />

*/<br />

arg.val = 1;<br />

if (semctl(semid, 0, SETVAL, arg) < 0)<br />

{<br />

perror("semctl");<br />

exit(1);<br />

}<br />

NP Lab Manual , RNEC page: 19


if ((parent_shm = shmat(shmid, NULL, 0)) == (int *)-1)<br />

{<br />

perror("parent shmat");<br />

exit(1);<br />

}<br />

*parent_shm = 0;<br />

/*<br />

* create a child process. The above opened shm & sem fds get<br />

* copied to child process as a result of fork(). They both attach to the<br />

* shared memory and use the semphore to increment the value in the shm.<br />

*/<br />

if ((pid = fork()) < 0)<br />

{<br />

printf("Child Process Creation Error:%d\n", errno);<br />

return;<br />

}<br />

/*<br />

* Child process attaches to shm. It fill operations to block till<br />

* it gets -1. Since the initial value of semaphore is 1, only one<br />

* process can do -1. The other process will see the value as 0 and<br />

* block till it sees sem value as 1. After semop(), increment the shm<br />

* integer by 1. Then again use the semop to set the sem value to 1, so<br />

* that other process gets the chance to run.<br />

* Repeat the above for a defined number of times. Later similar thing is<br />

* done for parent process also.<br />

*/<br />

if (pid == 0)<br />

{<br />

if ((child_shm = shmat(shmid, NULL, 0)) == (int *)-1)<br />

{<br />

perror("child shmat");<br />

exit(1);<br />

}<br />

NP Lab Manual , RNEC page: 20


}<br />

for (i = 0; i < CHILD_INCREMENT_COUNT; i++)<br />

{<br />

operations[0].sem_num = 0;<br />

operations[0].sem_op = -1;<br />

operations[0].sem_flg = 0;<br />

}<br />

if (semop(semid, operations, 1) < 0)<br />

{<br />

perror("child semop");<br />

exit(1);<br />

}<br />

*child_shm = *child_shm + 1;<br />

if (i%1000 == 0)<br />

{<br />

usleep(1); // sleep 1 us to increase window of critical section<br />

}<br />

operations[0].sem_num = 0;<br />

operations[0].sem_op = 1;<br />

operations[0].sem_flg = 0;<br />

if (semop(semid, operations, 1) < 0)<br />

{<br />

perror("child semop");<br />

exit(1);<br />

}<br />

if (pid != 0)<br />

{<br />

for (i = 0; i < PARENT_INCREMENT_COUNT; i++)<br />

{<br />

operations[0].sem_num = 0;<br />

operations[0].sem_op = -1;<br />

operations[0].sem_flg = 0;<br />

NP Lab Manual , RNEC page: 21


}<br />

if (semop(semid, operations, 1) < 0)<br />

{<br />

perror("parent semop");<br />

exit(1);<br />

}<br />

*parent_shm = *parent_shm + 1;<br />

if (i%1500 == 0)<br />

{<br />

usleep(1); // sleep 1 us to increase window of critical section<br />

}<br />

operations[0].sem_num = 0;<br />

operations[0].sem_op = 1;<br />

operations[0].sem_flg = 0;<br />

if (semop(semid, operations, 1) < 0)<br />

{<br />

perror("parent semop");<br />

exit(1);<br />

}<br />

// wait for child to complete<br />

wait(&status);<br />

/*<br />

* now that parent and child are done incrementing, check the<br />

* consistency of the shm memory.<br />

*/<br />

printf("Child Incremented %d times, Parent %d times. SHM Value %d<br />

\n",CHILD_INCREMENT_COUNT,PARENT_INCREMENT_COUNT,<br />

*parent_shm);<br />

NP Lab Manual , RNEC page: 22


if (*parent_shm == (CHILD_INCREMENT_COUNT +<br />

PARENT_INCREMENT_COUNT))<br />

{<br />

printf("Total of Parent & Child matches SHM value\n");<br />

}<br />

else<br />

{<br />

printf("BUG - Total of Parent & Child DOESNT match SHM<br />

value\n");<br />

}<br />

}<br />

}<br />

exit(0);<br />

/*<br />

Compiling………….<br />

$CC sem.c -o sema<br />

Execution…………..<br />

$ sema<br />

*/<br />

NP Lab Manual , RNEC page: 23


Output:<br />

[student@localhost ~]$ cc sem.c -o sema<br />

[student@localhost ~]$ ./sema<br />

Child Incremented 67787 times, Parent 84823 times. SHM Value 152610<br />

Total of Parent & Child matches SHM value<br />

NP Lab Manual , RNEC page: 24


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 5:<br />

AIM: Design TCP iterative Client and server application to reverse the given input<br />

sentence /*tcp_strrev_ser.c*/<br />

#include "unp.h"<br />

void str_echo(int sockfd)<br />

{<br />

long arg1, arg2;<br />

int x,y;<br />

ssize_t n;<br />

char line[MAXLINE];<br />

char swp;<br />

}<br />

for ( ; ; )<br />

{<br />

if ( (n = Readline(sockfd, line, MAXLINE)) == 0)<br />

return;/* connection closed by other end */<br />

n = strlen(line);<br />

/*strrev(line);*/<br />

for(x=0,y=n-1;x


}<br />

socklen_t clilen;<br />

struct sockaddr_in cliaddr, servaddr;<br />

listenfd = Socket(AF_INET, SOCK_STREAM, 0);<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));<br />

Listen(listenfd, LISTENQ);<br />

printf("Server Running on Port %d\n", SERV_PORT);<br />

for ( ; ; )<br />

{<br />

clilen = sizeof(cliaddr);<br />

connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);<br />

if ( (childpid = Fork()) == 0)<br />

{ /* child process */<br />

Close(listenfd); /* close listening socket */<br />

str_echo(connfd); /* process the request */<br />

exit(0);<br />

}<br />

Close(connfd); /* parent closes connected socket */<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_strrev_ser.c wrapsock.c -o tcp_strrev_ser<br />

Execution…………..<br />

$tcp_strrev_ser<br />

*/<br />

NP Lab Manual , RNEC page: 26


Output:<br />

[student@localhost ~]$ cc tcp_strrev_ser.c wrapsock.c -o tcp_strrev_ser<br />

[student@localhost ~]$ ./ tcp_strrev_ser<br />

Server Running on Port 9877<br />

Req From Client :<br />

iah<br />

Req From Client :<br />

eyb<br />

Req From Client :<br />

Suspended<br />

NP Lab Manual , RNEC page: 27


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/* tcp_strrev_clnt.c */<br />

#include "unp.h"<br />

void str_cli(FILE *fp, int sockfd)<br />

{<br />

char sendline[MAXLINE], recvline[MAXLINE];<br />

while (Fgets(sendline, MAXLINE, fp) != NULL)<br />

{<br />

Writen(sockfd, sendline, strlen(sendline));<br />

if (Readline(sockfd, recvline, MAXLINE) == 0)<br />

err_quit("str_cli: server terminated prematurely");<br />

Fputs(recvline, stdout);<br />

}<br />

}<br />

int main(int argc, char **argv)<br />

{<br />

int sockfd;<br />

struct sockaddr_in servaddr;<br />

if (argc != 2)<br />

err_quit("usage: tcpcli ");<br />

sockfd = Socket(AF_INET, SOCK_STREAM, 0);<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);<br />

Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));<br />

str_cli(stdin, sockfd); /* do it all */<br />

exit(0);<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_strrev_clnt.c wrapsock.c -o tcp_strrev_clnt<br />

Execution…………..<br />

$tcp_strrev_clnt <br />

*/<br />

NP Lab Manual , RNEC page: 28


Output:<br />

[student@localhost ~]$ ./ tcp_strrev_clnt.c wrapsock.c –o tcp_strrev_clnt<br />

[student@localhost ~]$ ./ tcp_strrev_clnt 169.254.17.41<br />

hai<br />

bye<br />

NP Lab Manual , RNEC page: 29


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 6:<br />

AIM: Design TCP client and server application to transfer file /* tcp_fil_ser.c */<br />

#include "unp.h"<br />

void str_echo(int sockfd)<br />

{<br />

long arg1, arg2;<br />

ssize_t n;<br />

char line[MAXLINE];<br />

char fname[100];<br />

char ch;<br />

FILE*f;<br />

int i=0;<br />

for ( ; ; )<br />

{<br />

if ( (n = Readline(sockfd, line, MAXLINE)) == 0)<br />

return;<br />

/* connection closed by other end */<br />

n = strlen(line);<br />

printf("\nReq From Client for file : %s",line);<br />

printf("\n.......%s",fname);<br />

f=fopen(“myfile”,"r");<br />

while((ch=getc(f))!=EOF)<br />

{<br />

line[i++]=ch;<br />

}<br />

line[i]=0;<br />

n=i;<br />

fclose(f);<br />

Writen(sockfd, line, n);<br />

}<br />

}<br />

int main(int argc, char **argv)<br />

{<br />

int listenfd, connfd;<br />

pid_t childpid;<br />

socklen_t clilen;<br />

struct sockaddr_in cliaddr, servaddr;<br />

listenfd = Socket(AF_INET, SOCK_STREAM, 0);<br />

NP Lab Manual , RNEC page: 30


}<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));<br />

Listen(listenfd, LISTENQ);<br />

printf("Server Running on Port %d\n", SERV_PORT);<br />

for ( ; ; )<br />

{<br />

clilen = sizeof(cliaddr);<br />

connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);<br />

if ( (childpid = Fork()) == 0)<br />

{ /* child process */<br />

Close(listenfd); /* close listening socket */<br />

str_echo(connfd); /* process the request */<br />

exit(0);<br />

}<br />

Close(connfd); /* parent closes connected socket */<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_file_ser.c wrapsock.c -o tcpfileser<br />

Execution…………..<br />

$ tcpfileser<br />

*/<br />

NP Lab Manual , RNEC page: 31


Output:<br />

[student@localhost ~]$ ./ tcp_file_ser.c wrapsock.c –o tcpfileser<br />

[student@localhost ~]$ ./ tcpfileser<br />

Server Running on Port 9877<br />

NP Lab Manual , RNEC page: 32


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/* tcp_file_clnt.c*/<br />

#include "unp.h"<br />

void str_cli(FILE *fp, int sockfd)<br />

{<br />

char sendline[MAXLINE], recvline[MAXLINE];<br />

while (Fgets(sendline, MAXLINE, fp) != NULL)<br />

{<br />

Writen(sockfd, sendline, strlen(sendline));<br />

if (Readline(sockfd, recvline, MAXLINE) == 0)<br />

err_quit("str_cli: server terminated prematurely");<br />

Fputs(recvline, stdout);<br />

}<br />

}<br />

int main(int argc, char **argv)<br />

{<br />

int sockfd;<br />

struct sockaddr_in servaddr;<br />

if (argc != 2)<br />

err_quit("usage: tcpcli ");<br />

sockfd = Socket(AF_INET, SOCK_STREAM, 0);<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);<br />

Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));<br />

str_cli(stdin, sockfd); /* do it all */<br />

exit(0);<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_file_clnt.c wrapsock.c -o tcpfileclnt<br />

Execution…………..<br />

$ tcpfileclnt <br />

*/<br />

NP Lab Manual , RNEC page: 33


Output:<br />

[student@localhost ~]$ ./ tcp_file_clnt.c wrapsock.c –o tcpfileclnt<br />

[student@localhost ~]$ ./ tcpfileclnt 127.0.0.1<br />

Hai printing from myfile<br />

NP Lab Manual , RNEC page: 34


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 7:<br />

AIM: Design a TCP concurrent server to echo given text into upper case using<br />

multiplexing system call “select” /* tcp_str_ser.c */<br />

#include "unp.h"<br />

void str_echo(int sockfd)<br />

{<br />

long arg1, arg2;<br />

ssize_t n;<br />

char line[MAXLINE];<br />

for ( ; ; )<br />

{<br />

if ( (n = Readline(sockfd, line, MAXLINE)) == 0)<br />

return; /* connection closed by other end */<br />

n = strlen(line);<br />

printf("\nReq From Client : %s",line);<br />

/* converting into upper case */<br />

for(i=0;i=97 && line[i]


}<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));<br />

Listen(listenfd, LISTENQ);<br />

printf("Server Running on Port %d\n", SERV_PORT);<br />

for ( ; ; )<br />

{<br />

clilen = sizeof(cliaddr);<br />

connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);<br />

if ( (childpid = Fork()) == 0)<br />

{ /* child process */<br />

Close(listenfd); /* close listening socket */<br />

str_echo(connfd); /* process the request */<br />

exit(0);<br />

}<br />

Close(connfd); /* parent closes connected socket */<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_str_ser.c wrapsock.c -o tcpstrser<br />

Execution…………..<br />

$ tcpstrser<br />

*/<br />

NP Lab Manual , RNEC page: 36


Output:<br />

[student@localhost ~]$ cc tcpstrser.c wrapsock.c -o tcpstrser<br />

[student@localhost ~]$ ./tcpstrser<br />

Server Running on Port 9877<br />

Req From Client : khan<br />

Req From Client : ram<br />

Req From Client : karthik<br />

NP Lab Manual , RNEC page: 37


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/* tcp_strrev_clnt.c */<br />

#include "unp.h"<br />

void str_cli(FILE *fp, int sockfd)<br />

{<br />

char sendline[MAXLINE], recvline[MAXLINE];<br />

while (Fgets(sendline, MAXLINE, fp) != NULL)<br />

{<br />

Writen(sockfd, sendline, strlen(sendline));<br />

if (Readline(sockfd, recvline, MAXLINE) == 0)<br />

err_quit("str_cli: server terminated prematurely");<br />

Fputs(recvline, stdout);<br />

}<br />

}<br />

int main(int argc, char **argv)<br />

{<br />

int sockfd;<br />

struct sockaddr_in servaddr;<br />

if (argc != 2)<br />

err_quit("usage: tcpcli ");<br />

sockfd = Socket(AF_INET, SOCK_STREAM, 0);<br />

bzero(&servaddr, sizeof(servaddr));<br />

servaddr.sin_family = AF_INET;<br />

servaddr.sin_port = htons(SERV_PORT);<br />

Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);<br />

Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));<br />

str_cli(stdin, sockfd); /* do it all */<br />

exit(0);<br />

}<br />

/*<br />

Compiling………….<br />

$CC tcp_str_clnt.c wrapsock.c -o tcpstrclnt<br />

Execution…………..<br />

$ tcpstrclnt <br />

*/<br />

NP Lab Manual , RNEC page: 38


Output:<br />

[student@localhost ~]$ cc tcpstrclnt.c wrapsock.c -o tcpstrclnt<br />

[student@localhost ~]$ ./tcpstrclnt 169.254.17.41<br />

khan<br />

KHAN<br />

ram<br />

RAM<br />

karthik<br />

KARTHIK<br />

NP Lab Manual , RNEC page: 39


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 8:<br />

Aim: Design UDP client and server application to reverse the given input sentence<br />

/* udp_strrev_ser.c */<br />

/* UDP echo client/server with select() (timeout option). See udpClient.c */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* close() */<br />

#include /* memset() */<br />

#define LOCAL_SERVER_PORT 1500<br />

#define MAX_MSG 100<br />

int main(int argc, char *argv[])<br />

{<br />

int l,x,y;<br />

char swp;<br />

int sd, rc, n, cliLen, flags;<br />

struct sockaddr_in cliAddr, servAddr;<br />

char msg[MAX_MSG];<br />

/* socket creation */<br />

sd=socket(AF_INET, SOCK_DGRAM, 0);<br />

if(sd


servAddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

servAddr.sin_port = htons(LOCAL_SERVER_PORT);<br />

rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));<br />

if(rc


}<br />

argv[0],inet_ntoa(cliAddr.sin_addr),<br />

ntohs(cliAddr.sin_port),msg);<br />

sleep(1);<br />

sendto(sd,msg,n,flags,(struct sockaddr *)&cliAddr,cliLen);<br />

}/* end of server infinite loop */<br />

return 0;<br />

/*<br />

Compiling………….<br />

$CC udp_strrev_ser.c wrapsock.c -o udpstrrevser<br />

Execution…………..<br />

$udpstrrevser<br />

*/<br />

NP Lab Manual , RNEC page: 42


Output:<br />

[student@localhost ~]$ cc udp_strrev_ser.c wrapsock.c -o udpstrrrevser<br />

[student@localhost ~]$ ./udpstrrevser<br />

./ udpstrrevser: waiting for data on port UDP 1500<br />

./ udpstrrevser: from 169.254.17.41:UDP32782 : iah<br />

./ udpstrrevser: from 169.254.17.41:UDP32782 : woh<br />

./ udpstrrevser: from 169.254.17.41:UDP32782 : era<br />

./ udpstrrevser: from 169.254.17.41:UDP32782 : u<br />

NP Lab Manual , RNEC page: 43


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/*udp_strrev_clnt.c */<br />

#include /* for exit() */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* memset() */<br />

#include /* select() */<br />

#define REMOTE_SERVER_PORT 1500<br />

#define MAX_MSG 100<br />

#define SOCKET_ERROR -1<br />

int isReadable(int sd,int * error,int timeOut)<br />

{ // milliseconds<br />

fd_set socketReadSet;<br />

FD_ZERO(&socketReadSet);<br />

FD_SET(sd,&socketReadSet);<br />

struct timeval tv;<br />

if (timeOut)<br />

{<br />

tv.tv_sec = timeOut / 1000;<br />

tv.tv_usec = (timeOut % 1000) * 1000;<br />

}<br />

else<br />

{<br />

tv.tv_sec = 0;<br />

tv.tv_usec = 0;<br />

} // if<br />

if (select(sd+1,&socketReadSet,0,0,&tv) == SOCKET_ERROR)<br />

{<br />

*error = 1;<br />

return 0;<br />

} // if<br />

NP Lab Manual , RNEC page: 44


*error = 0;<br />

return FD_ISSET(sd,&socketReadSet) != 0;<br />

} /* isReadable */<br />

int main(int argc, char *argv[])<br />

{<br />

int sd, rc, i, n, echoLen, flags, error, timeOut;<br />

struct sockaddr_in cliAddr, remoteServAddr, echoServAddr;<br />

struct hostent *h;<br />

char msg[MAX_MSG];<br />

/* check command line args */<br />

if(argch_name,<br />

inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));<br />

remoteServAddr.sin_family = h->h_addrtype;<br />

memcpy((char *) &remoteServAddr.sin_addr.s_addr,h->h_addr_list[0], h-<br />

>h_length);<br />

remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);<br />

/* socket creation */<br />

sd = socket(AF_INET,SOCK_DGRAM,0);<br />

if(sd


* bind any port */<br />

cliAddr.sin_family = AF_INET;<br />

cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

cliAddr.sin_port = htons(0);<br />

rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));<br />

if(rc


if(n


Output:<br />

[student@localhost ~]$ cc udp_strrev_clnt.c wrapsock.c -o udpstrrevclnt<br />

[student@localhost ~]$ ./ udpstrrevclnt 169.254.17.41 hai how are u<br />

./ udpstrrevclnt: sending data to '169.254.17.41' (IP : 169.254.17.41)<br />

..........<br />

./ udpstrrevclnt: echo from 169.254.17.41:UDP1500 : iah<br />

..........<br />

./ udpstrrevclnt: echo from 169.254.17.41:UDP1500 : woh<br />

..........<br />

./ udpstrrevclnt: echo from 169.254.17.41:UDP1500 : era<br />

..........<br />

./udpstrrevclnt: echo from 169.254.17.41:UDP1500 : u<br />

NP Lab Manual , RNEC page: 48


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 9:<br />

Aim: Design UDP client server to transfer a file<br />

/* server program */<br />

/* udp_file_ser.c */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* close() */<br />

#include /* memset() */<br />

#define LOCAL_SERVER_PORT 1500<br />

#define MAX_MSG 3000<br />

int main(int argc, char *argv[])<br />

{<br />

FILE*f;<br />

int i=0;<br />

char fname[100],ch;<br />

int sd, rc, n, cliLen, flags;<br />

struct sockaddr_in cliAddr, servAddr;<br />

char msg[MAX_MSG];<br />

/* socket creation */<br />

sd=socket(AF_INET, SOCK_DGRAM, 0);<br />

if(sd


c = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));<br />

if(rc


printf("%s: from %s:UDP%u : %s \n" , argv[0],<br />

inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port),msg);<br />

sleep(1);<br />

sendto(sd,msg,n,flags,(struct sockaddr *)&cliAddr,cliLen);<br />

}/* end of server infinite loop */<br />

return 0;<br />

}<br />

/*<br />

Compiling………….<br />

$ cc udp_file_ser.c wrapsock.c -o udp_file_ser<br />

Execution…………..<br />

$ udp_file_ser<br />

*/<br />

NP Lab Manual , RNEC page: 51


Output:<br />

[student@localhost ~]$ cc udp_file_ser.c wrapsock.c -o udp_file_ser<br />

[student@localhost ~]$ ./ udp_file_ser<br />

./udptranser: waiting for data on port UDP 1500<br />

./ udp_file_ser: from 169.254.17.41:UDP32782 : #include<br />

#include<br />

#include<br />

#include<br />

#define MAXLINE 4096<br />

int main(int argc,char*argv[])<br />

{<br />

int fd,n;<br />

char buff[2000];<br />

if(argc=2)<br />

{<br />

printf("\n wrong arg...");<br />

exit(0);<br />

}<br />

if((fd=open(argv[1],O_RDONLY))0)<br />

{<br />

write(1,buff,n);<br />

}<br />

exit(0);<br />

}<br />

NP Lab Manual , RNEC page: 52


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/* udp_file_clnt.c */<br />

#include /* for exit() */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* memset() */<br />

#include /* select() */<br />

#define REMOTE_SERVER_PORT 1500<br />

#define MAX_MSG 3000<br />

#define SOCKET_ERROR -1<br />

int isReadable(int sd,int * error,int timeOut)<br />

{ // milliseconds<br />

fd_set socketReadSet;<br />

FD_ZERO(&socketReadSet);<br />

FD_SET(sd,&socketReadSet);<br />

struct timeval tv;<br />

if (timeOut)<br />

{<br />

tv.tv_sec = timeOut / 1000;<br />

tv.tv_usec = (timeOut % 1000) * 1000;<br />

}<br />

else<br />

{<br />

tv.tv_sec = 0;<br />

tv.tv_usec = 0;<br />

} // if<br />

if (select(sd+1,&socketReadSet,0,0,&tv) == SOCKET_ERROR)<br />

{<br />

*error = 1;<br />

return 0;<br />

} // if<br />

NP Lab Manual , RNEC page: 53


*error = 0;<br />

return FD_ISSET(sd,&socketReadSet) != 0;<br />

} /* isReadable */<br />

/* END jcs 3/30/05 */<br />

int main(int argc, char *argv[])<br />

{<br />

int sd, rc, i, n, echoLen, flags, error, timeOut;<br />

struct sockaddr_in cliAddr, remoteServAddr, echoServAddr;<br />

struct hostent *h;<br />

char msg[MAX_MSG];<br />

/* check command line args */<br />

if(argch_name,<br />

inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));<br />

remoteServAddr.sin_family = h->h_addrtype;<br />

memcpy((char *) &remoteServAddr.sin_addr.s_addr,h->h_addr_list[0],<br />

h->h_length);<br />

remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);<br />

/* socket creation */<br />

sd = socket(AF_INET,SOCK_DGRAM,0);<br />

if(sd


cliAddr.sin_family = AF_INET;<br />

cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);<br />

cliAddr.sin_port = htons(0);<br />

rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));<br />

if(rc


}<br />

/* print received message */<br />

printf("%s: echo from %s:UDP%u : %s<br />

\n",argv[0],inet_ntoa(echoServAddr.sin_addr),ntohs(echoServAddr.sin_port),m<br />

sg);<br />

}<br />

return 1;<br />

/*<br />

Compiling………….<br />

$CC cc udp_file_clnt.c wrapsock.c -o udp_file_clnt<br />

Execution…………..<br />

$ udp_file_clnt <br />

*/<br />

NP Lab Manual , RNEC page: 56


Output:<br />

[student@localhost ~]$ cc udp_file_clnt.c wrapsock.c -o udp_file_clnt<br />

[student@localhost ~]$ ./ udp_file_clnt 169.254.17.41 io.c<br />

./ udp_file_clnt i: sending data to '169.254.17.41' (IP : 169.254.17.41)<br />

..........<br />

./ udp_file_clnt: echo from 169.254.17.41:UDP1500 : #include<br />

#include<br />

#include<br />

#include<br />

#define MAXLINE 4096<br />

int main(int argc,char*argv[])<br />

{<br />

int fd,n;<br />

char buff[2000];<br />

if(argc=2)<br />

{<br />

printf("\n wrong arg...");<br />

exit(0);<br />

}<br />

if((fd=open(argv[1],O_RDONLY))0)<br />

{<br />

write(1,buff,n);<br />

}<br />

exit(0);<br />

}<br />

NP Lab Manual , RNEC page: 57


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

WEEK 10:<br />

AIM: Design a UDP poll client server application to multiplex TCP and UDP requests<br />

for converting a given text in to upper case /* tcp_str_ser.c */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* close() */<br />

#include /* memset() */<br />

#define LOCAL_SERVER_PORT 1500<br />

#define MAX_MSG 100<br />

int main(int argc, char *argv[])<br />

{<br />

int sd, rc, n, cliLen, flags,x,l;<br />

struct sockaddr_in cliAddr, servAddr;<br />

char msg[MAX_MSG];<br />

/* socket creation */<br />

sd=socket(AF_INET, SOCK_DGRAM, 0);<br />

if(sd


if(rc


sleep(1);<br />

sendto(sd,msg,n,flags,(struct sockaddr *)&cliAddr,cliLen);<br />

/* END jcs 3/30/05 */<br />

}<br />

/* end of server infinite loop */<br />

return 0;<br />

}<br />

/*<br />

Compiling………….<br />

$CC udp_str_ser.c wrapsock.c -o udpstrser<br />

Execution…………..<br />

$udpstrser<br />

*/<br />

NP Lab Manual , RNEC page: 60


Output:<br />

[student@localhost ~]$ cc udp_str_ser.c wrapsock.c -o udpstrser<br />

[student@localhost ~]$./udpstrser<br />

udpstrser : waiting for data on port UDP 1500<br />

udpstrser : from 127.0.0.1 : UDP32768: HAI<br />

udpstrser : from 127.0.0.1 : UDP32768: HOW<br />

udpstrser : from 127.0.0.1 : UDP32768: ARE<br />

udpstrser : from 127.0.0.1 : UDP32768: YOU<br />

NP Lab Manual , RNEC page: 61


Subject: NETWORK PROGRAMMING LAB Year : 2010<br />

Class : IV B-Tech C.S.E Semester: First<br />

/*udp_str_clnt.c*/<br />

#include /* for exit() */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include /* memset() */<br />

#include /* select() */<br />

#define REMOTE_SERVER_PORT 1500<br />

#define MAX_MSG 100<br />

#define SOCKET_ERROR -1<br />

int isReadable(int sd,int * error,int timeOut)<br />

{<br />

// milliseconds<br />

fd_set socketReadSet;<br />

FD_ZERO(&socketReadSet);<br />

FD_SET(sd,&socketReadSet);<br />

struct timeval tv;<br />

if (timeOut)<br />

{<br />

tv.tv_sec = timeOut / 1000;<br />

tv.tv_usec = (timeOut % 1000) * 1000;<br />

}<br />

else<br />

{<br />

} // if<br />

tv.tv_sec = 0;<br />

tv.tv_usec = 0;<br />

NP Lab Manual , RNEC page: 62


if (select(sd+1,&socketReadSet,0,0,&tv) == SOCKET_ERROR)<br />

{<br />

*error = 1;<br />

return 0;<br />

} // if<br />

*error = 0;<br />

return FD_ISSET(sd,&socketReadSet) != 0;<br />

} /* isReadable */<br />

/* END jcs 3/30/05 */<br />

int main(int argc, char *argv[])<br />

{<br />

int sd, rc, i, n, echoLen, flags, error, timeOut;<br />

struct sockaddr_in cliAddr, remoteServAddr, echoServAddr;<br />

struct hostent *h;<br />

char msg[MAX_MSG];<br />

/* check command line args */<br />

if(argch_name,<br />

inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));<br />

remoteServAddr.sin_family = h->h_addrtype;<br />

memcpy((char *) &remoteServAddr.sin_addr.s_addr,<br />

h->h_addr_list[0], h->h_length);<br />

remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);<br />

/* socket creation */<br />

sd = socket(AF_INET,SOCK_DGRAM,0);<br />

NP Lab Manual , RNEC page: 63


if(sd


}<br />

(struct sockaddr *) &echoServAddr, &echoLen);<br />

if(n


Output:<br />

[student@localhost ~]$ cc udp_str_clnt.c wrapsock.c -o udpstrclnt<br />

[student@localhost ~]$./udpstrclnt 127.0.0.1 hai how are you<br />

udpstrclnt: sending data to „127.0.0.1‟ (IP:127.0.0.1)<br />

…………<br />

udpstrclnt : echo from 127.0.0.1 : UDP1500 : HAI<br />

…………<br />

udpstrclnt : echo from 127.0.0.1 : UDP1500 : HOW<br />

…………<br />

udpstrclnt : echo from 127.0.0.1 : UDP1500 : ARE<br />

…………<br />

udpstrclnt : echo from 127.0.0.1 : UDP1500 : YOU<br />

…………<br />

NP Lab Manual , RNEC page: 66

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!