mc9247 - network programming laboratory

Post on 09-Feb-2016

35 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

nw

TRANSCRIPT

MC9248 NETWORK PROGRAMMING LAB

LIST OF EXPERIMENTS:

1.Socket Programming

(a) TCP Sockets(b) UDP Sockets(c) Applications using Sockets

2.Simulation of Sliding window Protocol

3.Simulation of Routing Protocol

4.RPC

5.Development of applications such as DNS/HTTP/E-Mail- Multi User Chat

1.a TCP socket

MESSAGE TRANSFER USING TCP

AIM:To create a program that echos the client message in server using TCP socket.

ALGORITHM:

1.start the program2.create a socket for the server using socket() function.3.define the member variables of the structure sockaddr_in4.bind the server socket with the structure variable of sockadd_in5.listen the clients using the function listen()6.accept the clients using the function accept()7.using read() and write()function display the message from the client socket to the monitor8.create a socket for the client program9.define the structure sockaddr_in and declare structure variables10.connect client socket with structure variable11.using read() and write() function get input from the keyboard and write into the client socket12.stop the program

Server:

#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <stdio.h>#include <unistd.h> /* close */#define SUCCESS 0#define ERROR 1#define END_LINE 0x0#define SERVER_PORT 1500#define MAX_MSG 100/* function readline */

int read_line();int main (int argc, char *argv[]) {int sd, newSd, cliLen; struct sockaddr_in cliAddr, servAddr; char line[MAX_MSG];

/* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); return ERROR; }

/* bind server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT);

if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port "); return ERROR; }

listen(sd,5);

while(1) {

printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);

cliLen = sizeof(cliAddr); newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(newSd<0) { perror("cannot accept connection "); return ERROR; }

/* init line */ memset(line,0x0,MAX_MSG);

/* receive segments */ while(read_line(newSd,line)!=ERROR) {

printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line); /* init line */

memset(line,0x0,MAX_MSG);

} /* while(read_line) */

} /* while (1) */

}int read_line(int newSd, char *line_to_return) {

static int rcv_ptr=0; static char rcv_msg[MAX_MSG]; static int n; int offset;

offset=0;

while(1) { if(rcv_ptr==0) { /* read data from socket */ memset(rcv_msg,0x0,MAX_MSG); /* init buffer */ n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */ if (n<0) { perror(" cannot receive data "); return ERROR; } else if (n==0) { printf(" connection closed by client\n"); close(newSd); return ERROR; } }

/* if new data read on socket */ /* OR */ /* if another line is still in buffer */

/* copy line into 'line_to_return' */ while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) { memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1); offset++; rcv_ptr++; }

/* end of line + end of buffer => return line */ if(rcv_ptr==n-1) { /* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE;

rcv_ptr=0; return ++offset; }

/* end of line but still some data in buffer => return line */ if(rcv_ptr <n-1) { /* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE; rcv_ptr++; return ++offset; }

/* end of buffer but line is not ended => */ /* wait for more data to arrive on socket */ if(rcv_ptr == n) { rcv_ptr = 0; }

} /* while */}

Client

#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <stdio.h>#include <unistd.h> /* close */

#define SERVER_PORT 1500#define MAX_MSG 100

int main (int argc, char *argv[]) {

int sd, rc, i; struct sockaddr_in localAddr, servAddr; struct hostent *h;

if(argc < 3) { printf("usage: %s <server> <data1> <data2> ... <dataN>\n",argv[0]); exit(1); }

h = gethostbyname(argv[1]); if(h==NULL) { printf("%s: unknown host '%s'\n",argv[0],argv[1]); exit(1); }

servAddr.sin_family = h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); servAddr.sin_port = htons(SERVER_PORT);

/* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); exit(1); }

/* bind any port number */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0);

rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) { printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); perror("error "); exit(1); }

/* connect to server */ rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if(rc<0) { perror("cannot connect "); exit(1); }

for(i=2;i<argc;i++) {

rc = send(sd, argv[i], strlen(argv[i]) + 1, 0);

if(rc<0) { perror("cannot send data "); close(sd); exit(1); } printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]);

}return 0;}

Output:

[mca2k10b8@calinser tcp]$ cc server.c -o server[mca2k10b8@calinser tcp]$ ./server

TCPServer Waiting for client on portI got a connection from (127.0.0.1 , 32824)SEND (q or Q to quit) : hi

RECIEVED DATA = welcomeSEND (q or Q to quit) : thank u

[mca2k10b8@calinser tcp]$ cc client.c -o client[mca2k10b8@calinser tcp]$ ./client

Recieved data = hiSEND (q or Q to quit) : welcome

Recieved data = thankSEND (q or Q to quit) : q[mca2k10b8@calinser tcp]$

1.b UDP Socket

MESSAGE TRANSFER USING UDP

AIM:To create a program that echos the client message in the server using UDP socket

ALGORITHM:

1.create a socket for the server using socket() function2.use a user-defined function diep() to check whether the particular process is executing correctly or not3.use memset() function to create a memory for the structures variable of sockaddr_in and initialize it to zero4.define the member variables of sockaddr_in5.bind the server socket with structure variables of sockaddr_in6.using recvfrom() function get the client data and display it in the monitor using puts()function7.create a UDP socket for the client using socket() function8.using memset() allocate memory and initialize structure variable of sockaddr_in to zero9.define member variables of structure variable10.get ip address using inet_aton()11.get client data using scanf() and write the data into the client socket using sendto() function12.stop the program

UDP Server

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<netdb.h>

#include<stdio.h>

#include<unistd.h>

#include<string.h>

#define LOCAL_SERVER_PORT 4953

#define MAX_MSG 100

int main(int argc,char *argv[])

{

int sd,rc,n,clilen;

struct sockaddr_in cliAddr,servAddr;

char msg[MAX_MSG];

sd=socket(AF_INET,SOCK_DGRAM,0);

if(sd<0)

{

printf("%s:can't open socket \n",argv[0]);

exit(1);

}

servAddr.sin_family=AF_INET;

servAddr.sin_addr.s_addr=htonl(INADDR_ANY);

servAddr.sin_port=htons(LOCAL_SERVER_PORT);

rc=bind(sd,(struct sockaddr *) &servAddr,sizeof(servAddr));

if(rc<0)

{

printf("%s:can't bind port no %d/n",argv[0],LOCAL_SERVER_PORT);

exit(1);

}

printf("%s: Waiting for data on port UDP %u\n",argv[0],LOCAL_SERVER_PORT);

while(1)

{

memset(msg,0x0,MAX_MSG);

clilen=sizeof(cliAddr);

n=recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr *)&cliAddr,&clilen);

if(n<0)

{

printf("%s:can't recv data\n",argv[0]);

continue;

}

printf("%s:from %s:UDP%u:%s\n", argv[0],inet_ntoa(cliAddr.sin_addr),

ntohs(cliAddr.sin_port),msg);

}

return 0;

}

UDP Client

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<netdb.h>

#include<stdio.h>

#include<unistd.h>

#include<string.h>

#include<sys/time.h>

#define REMOTE_SERVER_PORT 4953

#define MAX_MSG 100

int main(int argc,char *argv[])

{

int sd,rc,i;

struct sockaddr_in cliAddr,remoteservAddr;

struct hostent *h;

if(argc<3)

{

printf("usage:%s <server><data1>..................<data n>\n",argv);

exit(1);

}

h=gethostbyname(argv[1]);

if(h==NULL)

{

printf("%s:unknown host '%s'\n",argv[0],argv[1]);

exit(1);

}

printf("%s:sending data to '%s'(IP:%s)\n",argv[0],inet_ntoa(*(struct in_addr*)

h->h_addr_list[0]));

remoteservAddr.sin_family=h->h_addrtype;

memcpy((char *)&remoteservAddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length);

remoteservAddr.sin_port=htons(REMOTE_SERVER_PORT);

sd=socket(AF_INET,SOCK_DGRAM,0);

if(sd<0)

{

printf("%s:can't open socket \n",argv[0]);

exit(1);

}

cliAddr.sin_family=AF_INET;

cliAddr.sin_addr.s_addr=htonl(INADDR_ANY);

cliAddr.sin_port=htons(0);

rc=bind(sd,(struct sockaddr *)&cliAddr,sizeof(cliAddr));

if(rc<0)

{

printf("%s:can't bind port\n",argv[0]);

exit(1);

}

for(i=2;i<argc;i++)

{

rc=sendto(sd,argv[i],strlen(argv[i])+1,0,(struct sockaddr *)&remoteservAddr,

sizeof(remoteservAddr));

if(rc<0)

{

printf("%s:can't send data %d\n",argv[0],i-1);

close(sd);

exit(1);

}

}

return 1;

}

Output:

[mca2k10b8@calinser udp]$ cc server.c -o server[mca2k10b8@calinser udp]$ ./server 3000

HiBye

[mca2k10b8@calinser udp]$ cc client.c -o client[mca2k10b8@calinser udp]$ ./client 172.16.1.5 3000ConnectedEnter the message to sendHi

Bye

1.c APPLICATION using SOCKETS

TCP SOCKETS CHAT APPLICATION(SERVER & CLIENT) USING C

SERVER#include<stdio.h>#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>#include<netdb.h> #include<stdlib.h>#include<string.h>#define MAX 80#define PORT 43454#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n;for(;;){bzero(buff,MAX);read(sockfd,buff,sizeof(buff));printf("From client: %s\t To client : ",buff);bzero(buff,MAX);n=0;while((buff[n++]=getchar())!='\n');write(sockfd,buff,sizeof(buff));if(strncmp("exit",buff,4)==0){printf("Server Exit...\n");break;

}}}

int main(){int sockfd,connfd,len;struct sockaddr_in servaddr,cli;

sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1)

{printf("socket creation failed...\n");exit(0);

}elseprintf("Socket successfully created..\n");bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(PORT);if((bind(sockfd,(SA*)&servaddr, sizeof(servaddr)))!=0){printf("socket bind failed...\n");exit(0);}elseprintf("Socket successfully binded..\n");if((listen(sockfd,5))!=0){printf("Listen failed...\n");exit(0);

}elseprintf("Server listening..\n");

len=sizeof(cli);connfd=accept(sockfd,(SA *)&cli,&len);if(connfd<0){printf("server acccept failed...\n");exit(0);}elseprintf("server acccept the client...\n");func(connfd);close(sockfd);}

CLIENT#include<stdio.h>#include<netinet/in.h>

#include<sys/types.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>#include<stdlib.h>#define MAX 80#define PORT 43454#define SA struct sockaddr

void func(int sockfd){

char buff[MAX];int n;for(;;)

{bzero(buff,sizeof(buff));printf("Enter the string : ");n=0;while((buff[n++]=getchar())!='\n');write(sockfd,buff,sizeof(buff));bzero(buff,sizeof(buff));read(sockfd,buff,sizeof(buff));printf("From Server : %s",buff);if((strncmp(buff,"exit",4))==0){printf("Client Exit...\n");break;}}}

int main(){int sockfd,connfd;struct sockaddr_in servaddr,cli;sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1){printf("socket creation failed...\n");exit(0);}elseprintf("Socket successfully created..\n");bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");servaddr.sin_port=htons(PORT);

if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0){printf("connection with the server failed...\n");

exit(0);}elseprintf("connected to the server..\n");func(sockfd);close(sockfd);}

OUTPUTSERVER SIDE$ cc tcpchatserver.c$ ./a.out

Socket successfully created..Socket successfully binded..Server listening..server acccept the client...

From client: haiTo client : helloFrom client: exitTo client : exitServer Exit...$

CLIENT SIDE$ cc tcpchatclient.c$ ./a.out

Socket successfully created..connected to the server..Enter the string : hai

From Server : helloEnter the string : exit

From Server : exitClient Exit...

UDP SOCKETS CHAT APPLICATION (SERVER & CLIENT) USING C

SERVER#include<stdio.h>#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>

#include<netdb.h>#include<string.h>#include<stdlib.h>#define MAX 80#define PORT 43454#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n,clen;struct sockaddr_in cli;clen=sizeof(cli);for(;;){bzero(buff,MAX);recvfrom(sockfd,buff,sizeof(buff),0,(SA *)&cli,&clen);

printf("From client %s To client",buff);bzero(buff,MAX);

n=0;while((buff[n++]=getchar())!='\n');sendto(sockfd,buff,sizeof(buff),0,(SA *)&cli,clen);if(strncmp("exit",buff,4)==0){printf("Server Exit...\n");break;}}}int main(){int sockfd;struct sockaddr_in servaddr;

sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){printf("socket creation failed...\n");exit(0);}elseprintf("Socket successfully created..\n");

bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(PORT);if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0){

printf("socket bind failed...\n");exit(0);}elseprintf("Socket successfully binded..\n");func(sockfd);close(sockfd);

}

CLIENT#include<sys/socket.h>#include<netdb.h>#include<string.h>#include<stdlib.h>#include<stdio.h>#define MAX 80#define PORT 43454#define SA struct sockaddrint main(){

char buff[MAX];int sockfd,len,n;struct sockaddr_in servaddr;sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){printf("socket creation failed...\n");exit(0);}elseprintf("Socket successfully created..\n");bzero(&servaddr,sizeof(len));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");servaddr.sin_port=htons(PORT);len=sizeof(servaddr);for(;;){printf("\nEnter string : ");n=0;while((buff[n++]=getchar())!='\n');sendto(sockfd,buff,sizeof(buff),0,(SA *)&servaddr,len);bzero(buff,sizeof(buff));

recvfrom(sockfd,buff,sizeof(buff),0,(SA *)&servaddr,&len);printf("From Server : %s\n",buff);if(strncmp("exit",buff,4)==0){printf("Client Exit...\n");

break;}}close(sockfd);}

OUTPUTSERVER SIDE$ cc udpchatserver.c$ ./a.out

Socket successfully created..Socket successfully binded..From client hai

To client helloFrom client exit

To client exitServer Exit...$

CLIENT SIDE$ cc udpchatclient.c$ ./a.out

Socket successfully created..Enter string : hai

From Server : helloEnter string : exit

From Server : exitClient Exit...

2. SIMULATION OF SLIDING WINDOW PROTOCOLS USING C

SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:

To create client and server program for sliding window protocol using TCP socket.

ALGORITHM:

1.create a TCP socket for the server2.define and declare the sockaddr_in structure3.bind the server socket with that of structure variable4.listen the client using listen() function5.accept the client using accept() function6.get the client data from the socket7.get the string length of client data using strlen() function8.if the string length is less than 5 write the length into the socket otherwise write NEGATIVEACK to the socket9.create a TCP socket for client10.declare and define the structure variable of type sockaddr_in11.connect the client socket with that of sockaddr_in structure variable12.get the data from the user13.write data from the user14.if the data from the socket(i.e) stringlength from the socket is less than 5 increase the string by size 1 otherwise decrease the string by half15.close the socket

SENDER#include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<netdb.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<errno.h>

int main() { int sock,bytes_received,connected,true=1,i=1,s,f=0,sin_size; char send_data[1024],data[1024],c,fr[30]=" "; struct sockaddr_in server_addr,client_addr; if((sock=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("Socket not created"); exit(1);

} if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)

{ perror("Setsockopt"); exit(1);

} server_addr.sin_family=AF_INET; server_addr.sin_port=htons(17000); server_addr.sin_addr.s_addr=INADDR_ANY;

if(bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1) { perror("Unable to bind"); exit(1); } if(listen(sock,5)==-1) { perror("Listen"); exit(1); } fflush(stdout); sin_size=sizeof(struct sockaddr_in); connected=accept(sock,(struct sockaddr *)&client_addr,&sin_size); while(strcmp(fr,"exit")!=0)

{ printf("Enter Data Frame %d:(Enter exit for End): ",i); scanf("%s",fr); send(connected,fr,strlen(fr),0); recv(sock,data,1024,0); if(strlen(data)!=0) printf("I got an acknowledgement : %s\n",data); fflush(stdout); i++; } close(sock); return (0);

}

RECEIVER#include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<netdb.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<errno.h>

int main() {

int sock,bytes_received,i=1; char receive[30]; struct hostent *host; struct sockaddr_in server_addr; host=gethostbyname("127.0.0.1"); if((sock=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("Socket not created"); exit(1); } printf("Socket created"); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(17000); server_addr.sin_addr=*((struct in_addr *)host->h_addr);bzero(&(server_addr.sin_zero),8);

if(connect(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1) { perror("Connect"); exit(1);

} while(1) { bytes_received=recv(sock,receive,20,0); receive[bytes_received]='\0'; if(strcmp(receive,"exit")==0||strcmp(receive,"exit")==0) { close(sock); break; } else { if(strlen(receive)<10) { printf("\n Frame %d data %s received\n",i,receive); send(0,receive,strlen(receive),0); } else { send(0,"negative",10,0); } i++; } } close(sock); return(0);

}

OUTPUTSENDER$ cc sender.c$ ./a.out

Enter Data Frame 1:(Enter exit for End): saveethaEnter Data Frame 2:(Enter exit for End): mcaEnter Data Frame 3:(Enter exit for End): exit

$

RECEIVER$ cc receiver.c$ ./a.out

Socket createdFrame 1 data saveetha receivedFrame 2 data mca received

3. SIMULATION OF ROUTING PROTOCOLS USING C

SIMULATION OF ROUTING PROTOCALS

A)BORDER GATEWAY PROTOCOL:

AIM:To create a connection between the speaker via certain path.

ALGORITHM:

1.Create aneed ed variables to communicate.2.Create the read function to get the number of anos.3.Get the name for 2 speaker to make a connection between them.4.Set the number ofnodes which are to be take places.by using for loop to continue the connect with nodeand speakers.5.Connection matrix to be held to make connection between them.6.Finally the stabilized routing table will be created with speaker nodes.

#include<stdio.h>#include<ctype.h>int graph[12][12];int e[12][12];int ad[12];int no,id,adc,small,chosen,i,j,ch1,ch2;

char nodes[12]={"abcdefghijkl"};int main(){adc=0;printf("Enter The Number Of Nodes: ");scanf("%d",&no);printf("\nEnter The Values For Adjacency Matrix\n");for(i=0;i <no;i++){for(j=0;j <no;j++){printf("Enter The Values For %d,%d Position: ",(i+1),(j+1));scanf("%d",&graph[i][j]);}}printf("\nEnter The Initial Estimates\t");for(i=0;i <no;i++){printf("\nEstimate For Node %c:\n",nodes[i]);for(j=0;j <no;j++){printf("To Node %c : ",nodes[j]);scanf("%d",&e[i][j]);}}do{printf("\nMENU:\n1.ROUTING INFO FOR NODE");printf("\n2.ESTIMATED TABLE\n");printf("Enter Your Choice: ");scanf("%d",&ch1);switch(ch1){case 1:printf("\nWhich Node Should Routing Table Be Built? (1-a)(2-b)...");scanf("%d",&id);id--;adc=0;printf("\nNeighbours For Particular Node ");for(i=0;i <no;i++){if(graph[id][i]==1){ad[adc]=i;adc++;printf("%c",nodes[i]);

}}for(i=0;i <no;i++){if(id!=i){small=100;chosen=1;for(j=0;j <no;j++){int total=e[ad[j]][i]+e[id][ad[j]];if(total <100){small=total;chosen=j;}}e[id][i]=small;printf("\nShortest Estimate To %c is %d",nodes[i],small);printf("\nNext Hop Is %c",nodes[ad[chosen]]);}elsee[id][i]=0;}break;case 2:printf("\n");for(i=0;i <no;i++){for(j=0;j <no;j++)printf("%d ",e[i][j]);printf("\n");}break;}printf("\nDo You Want To Continue?(1-YES) (2-NO): ");scanf("%d",&ch2);}while(ch2==1);return 0;

}

OUTPUT$ cc simulation.c$ ./a.out

Enter The Number Of Nodes: 2

Enter The Values For Adjacency MatrixEnter The Values For 1,1 Position: 1Enter The Values For 1,2 Position: 2Enter The Values For 2,1 Position: 3Enter The Values For 2,2 Position: 4Enter The Initial EstimatesEstim ate For Node a:To Node a : 1To Node b : 2Estimate For Node b:To Node a : 1To No de b : 2MENU:1.ROUTING INFO FOR NODE2.ESTIMATED TABLEEnter Your Choice: 1Which Node Should Routing Table Be Built? (1-a)(2-b)...1Neighbours For Particular NodeaShortest Estimate To b is 2Next Hop Is aDo You Want To Continue?(1-YES) (2-NO): 1MENU:1.ROUTING INFO FOR NODE2.ESTIMATED TABLEEnter Your Choice: 20 2 1 2 Do You Want To Continue?(1-YES) (2-NO): 2

4.Remote Procedure Call

Aim:

To make a Remote Procedure Call

Algorithm:

Steps to write Remote Procedure Call (RPC) Program on cslinux1.comp.hkbu.edu.hk

0. You are assigned a unique number for your RPC programming, if your login ID is d5049394, your RPC program ID will be 0x35049394.

1. Write your own RPC specification file “remote.x”.

2. Use rpcgen to generate the files you need (i.e. remote.h, remote_svc.c, and remote_clnt.c)

%rpcgen remote.x

3. Use rpcgen to generate sample server program

%rpcgen –Ss remote.x > remote_server.c

4. Use rpcgen to generate sample client program

%rpcgen –Sc remote.x > remote_users.c

5. Modify your sample server and sample client programs

6. Use gcc to compile your server program (remote_server.c)

%gcc –o remote_server remote_server.c remote_svc.c –lnsl

7. Use gcc to compile your client program (remote_users.c)

%gcc –o remote_users remote_users.c remote_clnt.c –lnsl

8. Login to a machine (e.g. cslinux1) to run your server program

cslinux1%remote_server

or to put the server to run in the background

cslinux1%remote_server &

9. Login to another machine (e.g. cslinux2) to run your client program

cslinux2%remote_users cslinux1

10. Use rpcinfo to manage your RPC resources, see the man page of rpcinfo for details.

Here is the local version of users – a program to find out who is logging in the local machine:

#include <stdio.h>

#include <string.h>

#include <utmp.h>

int main(int argc, char* argv[])

{

/* static char * result; */

FILE *fp;

struct utmp tmp;

char buffer[1024];

char tmp_buff[10];

buffer[0] = '\0';

fp = fopen("/var/run/utmp", "rb");

while (!feof(fp)){

fread(&tmp, sizeof(struct utmp), 1, fp);

if (strlen(tmp.ut_name)){

sprintf(tmp_buff, "%.8s ", tmp.ut_name);

strcat(buffer, tmp_buff);

}

}

fclose(fp);

printf("%s\n", buffer);

}

Appendix – Source code for remote_server & remote_users

/* Program Name: remote.x */

program REMOTE_PROG {

version REMOTE_VERS {

string REMOTE_USERS(void) = 1;

} = 1;

} = 0x32345678;

/* Program Name: remote_server.c */

#include <stdio.h>

#include <string.h>

#include <utmp.h>

#include "remote.h"

char **

remote_users_1_svc(void *argp, struct svc_req *rqstp)

{

static char * result;

FILE *fp;

struct utmp tmp;

char buffer[1024];

char tmp_buff[10];

buffer[0] = '\0';

fp = fopen("/var/run/utmp", "rb");

while (!feof(fp)){

fread(&tmp, sizeof(struct utmp), 1, fp);

if (strlen(tmp.ut_name)){

sprintf(tmp_buff, "%.8s ", tmp.ut_name);

strcat(buffer, tmp_buff);

}

}

fclose(fp);

result = buffer;

return &result;

}

/* Program Name: remote_users.c */

#include <stdio.h>

#include "remote.h"

void

remote_prog_1(char *host)

{

CLIENT *clnt;

char * *result_1;

char *remote_users_1_arg;

#ifndef DEBUG

clnt = clnt_create (host, REMOTE_PROG, REMOTE_VERS, "udp");

if (clnt == NULL) {

clnt_pcreateerror (host);

exit (1);

}

#endif /* DEBUG */

result_1 = remote_users_1((void*)&remote_users_1_arg, clnt);

if (result_1 == (char **) NULL) {

clnt_perror (clnt, "call failed");

}

printf("Users logging on Server %s\n%s\n", host, *result_1);

#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];

remote_prog_1 (host);

exit (0);

}

5. DOMAIN NAME SERVICE

AIM:To display the ip address of the server from dns using TCP socket.

ALGORITHM:

1.create a TCP socket using socket() function2.declare a structure variable of type hostent and structure variables of type in_addr and structure variable of type sockaddr_in3.define members of the structure sockaddr_in4.bind server socket with the structure variable using bind() function5.listen the client using listen() function

6.accept the client using accept() function7.get the server name from the client socket using read() function8.using gethostbyname() function get the ip address of the server and write it into the server socket using write() function9.create a TCP socket for client10.declare and define the structure variable of sockaddr_in11.connect client socket with that of structure variable12.get the servername from the user13.write it into the client socket using write() function14.the ip address would be read from the client socket using read() function15.stop the program

DNS_SERVER:

#include<stdio.h>#include<sys/socket.h>#include<netinet/in.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<netdb.h>#include<arpa/inet.h>#include<sys/types.h>#include<time.h>#define TCP_SERV_PORT 8890#define SA struct sockaddr

int main(){int listenfd,connfd;char status[20];char process[20];

socklen_t len;struct sockaddr_in servaddr,cliaddr;struct hostent *hp,tp;char host[50],buff[100];listenfd=socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(8890);

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);bind(listenfd,(SA*)&servaddr,sizeof(servaddr));listen(listenfd,10);

len=sizeof(cliaddr);connfd=accept(listenfd,(SA*)&cliaddr,&len);

if(connfd < 0)printf("server conn error\n");bzero(process,sizeof(process));read(connfd,process,sizeof(process));printf("%s\n",process);bzero(status,sizeof(status));

hp = gethostbyname (process);inet_ntop (AF_INET, hp -> h_addr, status, 100);printf("IP address%s \n",status);write(connfd,status,strlen(status)+1);close(connfd);return 0;}DNS_CLIENT:

#include<stdio.h>#include<sys/socket.h>//#include<iostream>#include<netinet/in.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<netdb.h>#include<arpa/inet.h>#include<sys/types.h>#include<time.h>#define SA struct sockaddr

int main(int argc,char** argv){socklen_t len;struct sockaddr_in servaddr;

char buff[100],host[50];int sockfd;sockfd=socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;

servaddr.sin_port=htons(8890);servaddr.sin_addr.s_addr=inet_addr("172.16.1.5");if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)printf("connect error");printf("enter the domain name");scanf("%s",host);write(sockfd,host,strlen(host)+1);

if(read(sockfd,buff,sizeof(buff))<0)printf("read error");printf("The host addr %s \n",buff);return 0;}

OUTPUT:

[mca2k10b8@calinser dns]$ cc server.c -o serv[mca2k10b8@calinser dns]$ ./serv 8890calinserIP address172.16.1.5[mca2k10b8@calinser dns]$

[mca2k10b8@calinser dns]$ cc client.c -o clie[mca2k10b8@calinser dns]$ ./clie 172.16.1.5 8890enter the domain namecalinserThe host addr 172.16.1.5[mca2k10b8@calinser dns]$

MULTI USER CHAT

Aim:

To create a program that multiple chat using TCP socket.

ALGORITHM:

1.start the program2.create a socket for the server using socket() function.3.define the member variables of the structure sockaddr_in4.bind the server socket with the structure variable of sockadd_in5.listen the clients using the function listen()6.accept the clients using the function accept()7.using read() and write()function display the message from the client socket to the monitor.8.create a socket for the two client program9.define the structure sockaddr_in and declare structure variables10.connect client socket with structure variable11.using read() and write() function get input from the keyboard and write into the client socket12.stop the program

SERVER:

#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<pthread.h>

int serv_fd,clnt_fd[2],n,i,pid;struct sockaddr_in serv_addr;char c;

void *cli1(void){ while(1){ int n=read(clnt_fd[0],&c,15);

write(clnt_fd[1],&c,n); }}

void *cli2(void){ while(1){ int n=read(clnt_fd[1],&c,15); write(clnt_fd[0],&c,n); }}

int main(int agrc,char **argv){pthread_t t1,t2;//create socketserv_fd=socket(AF_INET,SOCK_STREAM,0);

//prepare for bind serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(atoi(argv[1])); serv_addr.sin_addr.s_addr=INADDR_ANY;

//bind bind(serv_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); listen(serv_fd,2); perror("before");

//accept only 1 client clnt_fd[0]=accept(serv_fd,NULL,NULL);

perror("success");clnt_fd[1]=accept(serv_fd,NULL,NULL);

//read and write until ^D from client

pthread_create(&t1,NULL,&cli1,NULL);pthread_create(&t2,NULL,&cli2,NULL);pthread_join(t1,NULL);pthread_join(t2,NULL);

//close connection from client and stop serverclose(clnt_fd[0]);close(clnt_fd[1]);close(serv_fd);return 0;

}

CLIENT:

#include<stdio.h>#include<pthread.h>#include<ctype.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>

int clnt_fd,serv_fd,pid,n; struct sockaddr_in serv_addr; char buff[20],c;

void *writes(void){ while(1) { int n=read(0,&c,15); write(clnt_fd,&c,n); }}

void *reads(void){ while(1){ int n=read(clnt_fd,buff,15); write(1,"Other Cli : ",12); write(1,buff,n); }}int main(int agrc,char **argv){ pthread_t t1,t2;

//create socket clnt_fd=socket(AF_INET,SOCK_STREAM,0);

//prepare for connect serv_addr.sin_family=AF_INET;

serv_addr.sin_port=htons(atoi(argv[2])); serv_addr.sin_addr.s_addr=inet_addr(argv[1]);

//connect to echo serverperror("staty"); connect(clnt_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));

perror("stat:");pthread_create(&t1,NULL,&reads,NULL);pthread_create(&t2,NULL,&writes,NULL);pthread_join(t1,NULL);pthread_join(t2,NULL);close(clnt_fd);return 0;}

OUTPUT:

[mca2k10b8@calinser tmchat]$ cc server -lpthread -o sercc: server: No such file or directory[mca2k10b8@calinser tmchat]$ cc server.c -lpthread -o ser[mca2k108@calinser tmchat]$ ./ser 8889before: Successsuccess: Illegal seek

[mca2k10b8@calinser tmchat]$ cc client.c -lpthread -o cli1[mca2k10b8@calinser tmchat]$ ./cli1 172.16.1.5 8889staty: Successstat:: Illegal seekhiOther Cli : hello

[mca2k10b8@calinser tmchat]$ cc client.c -lpthread -o cli2[mca2k10b8@calinser tmchat]$ ./cli2 172.16.1.5 8889staty: Successstat:: Illegal seekOther Cli : hihello

HTTP

SERVER#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>

int main(){int sock, connected, bytes_recieved , true = 1;char send_data [1024] , recv_data[1024];char rep[40]={"File Received"};int s,i,f=0;struct sockaddr_in server_addr,client_addr;int sin_size;if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {perror("Socket");exit(1);}

if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1){perror("Setsockopt");exit(1);

}server_addr.sin_family = AF_INET;server_addr.sin_port = htons(2500);server_addr.sin_addr.s_addr = INADDR_ANY;if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1){

perror("Unable to bind");exit(1);}if (listen(sock, 5) == -1) {perror("Listen");exit(1);}fflush(stdout);

while(1){sin_size = sizeof(struct sockaddr_in);

connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);while (1){bytes_recieved = recv(connected,recv_data,1024,0);recv_data[bytes_recieved] = '\0';if(strcmp(recv_data,"\0")!=0){ printf("I got a file: %s", recv_data);send(connected, rep,strlen(rep), 0);}else send(connected,"No file ",22,0);fflush(stdout);}}

close(sock);return 0;}

CLIENT#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>

int main(){int sock, bytes_recieved;char send_data[1024],recv_data[1024];struct hostent *host;struct sockaddr_in server_addr;host = gethostbyname("127.0.0.1");if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)\{perror("Socket");exit(1);

}server_addr.sin_family = AF_INET;server_addr.sin_port = htons(2500);server_addr.sin_addr = *((struct in_addr *)host->h_addr);bzero(&(server_addr.sin_zero),8);if (connect(sock, (struct sockaddr *)&server_addr,

sizeof(struct sockaddr)) == -1){perror("Connect");exit(1);}while(1){printf("\nEnter the file name : ");gets(send_data);if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)send(sock,send_data,strlen(send_data), 0);

else{send(sock,send_data,strlen(send_data), 0);close(sock);break;}bytes_recieved=recv(sock,recv_data,1024,0);recv_data[bytes_recieved] = '\0';if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0){close(sock);break;}elseprintf("\n Acknowledgment from Server: %s " , recv_data);}return 0;

}

OUTPUTSERVER$ cc httpserver.c$ ./a.out

I got a file: UNIX.docI got a file: UNIX.txtI got a file: savee.txt

CLIENT$ cc httpclient.c$ ./a.out

Enter the file name : UNIXdocAcknowledgment from Server: File Received Enter the file name : UNIX.txtAcknowledgment from Server: File Received Enter the file name : savee.txtAcknowledgment from Server: File Received Enter the file name :

top related