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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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; }
/* 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
[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
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);
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(){
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
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.
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)
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];
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;
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);
[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
[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
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 :