Unix LAB MANUAL
Unix LAB MANUAL
Date:
AIM:
To write a C Program to implement a simple socket program using TCP
sockets.
ALGORITHM:
TCP server:
Step 1: Create a TCP socket using SOCK_STREAM.
sd = socket(AF_INET, SOCK_STREAM, 0)
Step 2: Specify the family, port and address for the server.
Step 3: Bind the address and port to ensure the association of bidirectional
information.
Step 4: listen to the client using l = listen(sid, 5).
Step 5: Accept the connection from the client using
a = accept(sid, (struct sockadd *) &cliaddr, &clilen)
Step 6: Receive the message from the client using
r = recv (sid, buffer, 1023)
Step 7: Print the message in the buffer
Step 8: Close the socket.
Step 9: Stop the process.
TCP client:
Step 1: Create a TCP socket using SOCK_STREAM
cd = socket(AF_INF, SOCK_STREAM, 0)
Step 2: Get the host name by using
h = gethostbyname( argv[1])
TCP
Server Program
/* tcpserver*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<errno.h>
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
if (bind(sockfd,struct sockaddr*)
&serv_addr,sizeof(serv_addr)) < 0)
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *)
&cli_addr,&clilen);
bzero(buffer,256);
n = read(newsockfd,buffer,255);
Client Program
/*tcpclient*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<errno.h>
if (argc < 3)
{
fprintf(stderr,"Enter Hostname and Portnumber");
exit(1);
}
portno = atoi(argv[2]);
SAN INTERNATIONAL INFO SCHOOL Page | 3
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
if (connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr)) < 0)
printf("ERROR connecting");
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
printf("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
[prabha@localhost ~]$
Client
[prabha@localhost ~]$ gcc tcpclient.c
[prabha@localhost ~]$ ./a.out localhost 2500
[prabha@localhost ~]$
Result:
Thus the C program to implement a simple socket programming using
TCP sockets has been completed successfully.
AIM:
To write a C program to implement a simple socket program using UDP
sockets.
ALGORITHM:
UDP Server:
Step 1: Create a UDP socket using SOCK_DGRAM.
sd = socket(AF_INET, SOCK_DGRAM,0)
Step 2: Specify the family, port and address for both server and client.
Step 3: Bind the address and port of the server with the client using
b = bind(sd, (struct sockaddr *) & servaddr, sizeof (servaddr))
Step 4: Calculate the size of client address.
Step 5: Send the information from the server to client .
Step 6: Close the socket.
Step 7: Stop the process.
UDP Client:
Step 1:Create a UDP socket using SOCK_DREAM.
sd = socket(AF_INET, SOCK_DGRAM,0)
Step 2: Specify the family, port and address for both server and client.
Step 3: Bind the address and port of the client with the server using
b = bind(sd, (struct sockaddr *) & cliaddr, sizeof (cliaddr))
Step 4: Receive the information from the server.
Step 5: Close the socket.
Step 6: Stop the process.
int main()
{
char x[15],y[15];
int n,sd,size,sersock;
struct sockaddr_in cliaddr,servaddr;
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(8330);
cliaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8331);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);
sd=socket(AF_INET,SOCK_DGRAM,0);
Client Program
/*udpclient*/
#include<sys/socket.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(8330);
cliaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8331);
servaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);
sd=socket(AF_INET,SOCK_DGRAM,0);
size=sizeof(servaddr);
sendto(sd,x,sizeof(x),0,(struct sockaddr *)
&servaddr,size);
printf("\nData sent successfully... \n");
close(sd);
}
Hi
Client
[prabha@localhost ~]$ gcc udpclient.c
[prabha@localhost ~]$ ./a.out
hi
Result:
Thus the C Program to implement a simple socket program using
sockets has been completed successfully.
AIM:
To write a C Program to implement chat application using TCP/IP
Sockets.
ALGORITHM:
Chat server:
Step 1: Create a TCP/IP socket.
Step 2: Initialize socket address structure with protocol family, port number
and IP address.
Step 3: Bind the socket.
Step 4: Convert the socket into server socket by invoking listen function.
Step 5: Accept the connection from the client using accept function.
Step 6: Send and receive the message to and from client.
Step 7: Close the socket.
Step 8: Stop the process.
Chat Client:
Step 1: Create a TCP/IP socket.
Step 2: Initialize socket address structure with protocol family, port number
and IP address.
Step 3: Invoke the convert function to connect to the server.
Step 4: Send and receive the message to and from server.
Step 5: Close the socket.
Step 6: Stop the process.
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#define SERV_TCP_PORT 1433
#define MAX 1024
sockfd = socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
clientlen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)
&cli_addr,&clientlen);
do
{
bzero(buffer,MAX);
read(newsockfd,buffer,MAX);
printf(“\nClient message: %s”,buffer);
bzero(buffer,MAX);
printf(“\nServer message :”);
fgets(buffer,MAX,stdin);
write(newsockfd,buffer,MAX);
printf(“\n”);
}while(strcmp(buffer,”bye\n”)!=0);
close(sockfd);
return(0);
}
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#define SERV_TCP_PORT 1433
#define MAX 1024
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr));
do
{
bzero(buffer,MAX);
printf(“\nClient message :”);
fgets(buffer,MAX,stdin);
write(sockfd,buffer,MAX);
bzero(buffer,MAX);
read(sockfd,buffer,MAX);
printf(“\nServer message : %s”,buffer);
printf(“\n”);
}while(strcmp(buffer,”bye\n”)!=0);
close(sockfd);
return(0);
}
Client message : Hi
Server message : Hi da
[prabha@localhost ~]$
Client
[prabha@localhost ~]$ gcc chatclient.c
[prabha@localhost ~]$ ./a.out
Client message : Hi
Server message : Hi da
[prabha@localhost ~]$
Result:
Thus the c program to implement chat application using TCP/IP sockets
has been completed successfully.
AIM:
To write a C program to simulate a simple sliding window protocol.
ALGORITHM:
Server:
Step 1: Start the process.
Step 2: Open the file which contains the frames to be sent.
Step 3: Get the starting and ending frame numbers to be sent.
Step 4: Read the frames from the file and send that frames to client side.
Step 5: Stop the process.
Client:
Step 1: Start the process.
Step 2: Enter the number frames going to be received.
Step 3: Receive the frames one by one.
Step 4: After receiving all the frames, check the frames received order.
Step 5: If it is received in correct order, print as frames were received in correct
sequence, otherwise print frames were not received in correct sequence.
Step 6: Stop the process.
Sliding Window
SAN INTERNATIONAL INFO SCHOOL Page | 14
Server program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct mymsgbuf
{
long mtype;
char mtext[25];
};
FILE *fp;
int main()
{
Struct mymsgbuf buf;
int si,ei,sz;
int msgid;
int i=0,s;
int a[100];
char d;
if((fp=fopen(“send”,”r”))= = NULL)
printf(“\nFile opened”);
if((msgid=msgget(80,IPC_CREAT|0666))= = -1)
{
printf(“\nError inMSGGET”);
exit(0);
}
while(!feof(fp))
{
d=getc(fp);
a[i]=d;
i++;
}
Client program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct mymsgbuf
{
long mtype;
char mtext[25];
};
FILE *fp;
int main()
{
Struct mymsgbuf buf;
int msgid;
int i=0,s;
int count=0,frmsz;
int a[100];
if((msgid=msgget(89,IPC_CREAT|0666))= = -1)
{
printf(“\nError inMSGGET”);
exit(0);
}
printf(“\nEnter the frame size:”);
scanf(“%d”,&frmsz);
if((fp=fopen(“check”,”r”))= = NULL)
printf(“\nFile not opened”);
else
printf(“\nFile opened”);
while(!feof(fp))
{
d=getc(fp);
a[i]=d;
i++;
}
S=i;
for(i =0;i<frmsz;i++)
printf(“\t %c”,a[i]);
for(i=0;i<frmsz;i++)
{
if((msgrcv(msgid,&buf,sizeof(buf),0,1))= = -1)
{
printf(“\nError in MSGRCV”);
exit(0);
}
Printf(“\nFrame received is :%c”,buf.mtext[i]);
}
for(i=0;i<frmsz;i++)
{
if(a[i]= =buf.mtext[i])
count++;
}
if(count= =0)
{
printf(“\nFrames were not received in
correct sequence”);
printf(“%d”,count);
exit(0);
}
if(count==frmsz)
Server
[prabha@localhost ~]$ gcc slidserver.c
[prabha@localhost ~]$ ./a.out
File opened
Enter starting and ending index of frame array :0 6
N e t w o r k
Frames send
[prabha@localhost ~]$
Client
[prabha@localhost ~]$ gcc slidclient.c
[prabha@localhost ~]$ ./a.out
File opened n e t w o r k
Frame received is : n
Frame received is : e
Frame received is : t
Frame received is : w
Frame received is : o
Frame received is : r
Frame received is : k
Result:
Thus the C program to simulate a simple sliding window protocol has
been completed successfully.
AIM:
To write a c program to simulate selective repeat and go back-N sliding
window protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: Enter the number of frames.
Step 3: If your option is selective repeat
Enter the frames one by one
Step 4: Send the frames one by one and receive acknowledgements for the sent
frames.
Step 5: If you received NACK, retransmit the frame for which you received
NACK, otherwise no need to retransmit.
Step 6: If your option is go-back-N
Send the frames one by one & receive acknowledgement for the sent
frames.
Step 7: If you received NACK, retransmit the frames from that frame for which
received NACK.
Step 8: Stop the Process.
Sliding Window
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int data[50],ack[10];
int m,n,a,i,d,index=0;
p:
if(a==3)
exit(0);
else
{
printf(“\n Enter the size of the data :”);
scanf(“%d”,&n);
printf(“\n Enter the data one by one : “);
for(i=0;i<n;i++)
scanf(“%d”,&data[i]);
switch(a)
{
case 1:
{
printf(“\n Data is ready to send\n”);
for(i=0;i<n;i++)
ack[i]=0;
do
{
index=0;
for(i=0;i<n;i++)
{
if(ack[i]==0)
{
printf(“\n%d is sent”,data[i]); printf(“\nEnter the
ack[1-ACK: 0-NACK] :”);
scanf(“%d”,&ack[i]);
}
for(i=0;i<n;i++)
if(ack[i]==0)
{
printf(“\n Received negative ack for frame
%d”,i+1);
index=1;
}
}while(index!=0);
printf(“\n Do you want to continue[1- Yes:0-No]:”);
scanf(“%d”,&d);
if(d==1)
goto p;
if(d==0)
exit(0);
break;
}
case 2:
{
for(i=0;i<n;i++)
{
printf(“\n %d is sent”,data[i]);
printf(“\nEnter the ack [1-ACK : 0-NACK] :”);
scanf(“%d”,&ack[i]);
printf(“%d”,ack[i]);
if(ack[i]==1)
printf(“\n Received ack”);
else
{
printf(“\n Negarive ack for %d”,data[i]);
if(i==0)
i=-1;
else
i=i-1;
}
}
printf(“\nDo you want to continue [1-Yes:0-No]: “);
scanf(“%d”,&d);
if(d==1)
goto p;
if(d==0)
exit(0);
break;
}
1. Selective Repeat
2. Go Back-N
3. Exit
1 is sent
Enter the ack [1-ACK : 0-NACK} :1
2 is sent
Enter the ack [1-ACK : 0-NACK) :0
3 is sent
Enter the ack [1-ACK : 0-NACK} :1
1. Selective Repeat
2. Go Back-N
3. Exit
1 is sent
1
Received ack
2 is sent
Enter the ack [1-ACK : 0-NACK} :0
0
Negative ack for 2
2 is sent
Enter the ack [1-ACK : 0-NACK} :1
1
Received ack
3 is sent
Enter the ack [1-ACK : 0-NACK} :1
1
Received ack
3 is sent
Enter the ack [1-ACK : 0-NACK} :1
1
Received ack
Do you want to continue [1-Yes : 0-No] :1
1. Selective Repeat
2. Go Back-N
3. Exit
Result:
ALGORITHM:
Server:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Bind the socket.
Step 5: Listen to the client and accept the connection from the client.
Step 6: Initialize cwnd to 1
i) Read the message from client.
ii) Print the message
iii) Send the acknowledgement to the client.
iv) Increment cwnd as cwnd*=2(exponentially).
Step 7: Close the socket.
Step 8: Stop the process.
Client:
Step 1: Start the process.
Step 2: Create a TCP socket.
#include<stdio.h>
if(argc<2)
{
fprintf(stderr,“Error:Port number is not given\n”);
exit(1);
}
sid=socket(AF_INET,SOCK_STREAM,0);
bzero((char*)&addr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[1]));
saddr.sin_addr.s_addr=INADDR_ANY;
if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
{
perror(“socket_bind”);
}
listen(sid,5);
clen=sizeof(struct sockaddr_in);
newsid=accept(sid,(struct sockaddr*)&caddr,&clen);
if(newsid<0)
{
perror(“socket_accept”);
}
bzero(buffer,1024);
for(cwnd=1;cwnd<=MAX;cwnd*=2)
{
printf(“\n\nCurrent Window size:%d\n”,cwnd);
Client program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<netdb.h>
#define MAX 10
if(argc<3)
{
fprintf(stderr, “Enter Hostname and Portnumber”);
exit(1);
}
cid=socket(AF_INET, SOCK_STREAM, 0);
if(cid<0)
if(h==NULL)
{
printf(“Host is not found”);
exit(1);
}
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[2]));
bcopy((char*)h->h_addr,(char*)&saddr.sin_addr.s_addr,
h->h_length);
if(connect(cid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
{
perror(“Socket_connect”);
}
bzero(buffer,1024);
for(cwnd=1;cwnd<=MAX;cwnd*=2)
{
printf(“\nCurrent Window Size:%d\n”,cwnd);
for(dummy=1; dummy<=cwnd; dummy++)
{
printf(“\nEnter your message to server:”);
fgets(buffer,1023,stdin);
n=write(cid,buffer,strlen(buffer));
n=read(cid, buffer, strlen(buffer));
}
count++;
printf(“Ack %d from server\n”, count);
}
return o;
}
Output:
Server
[prabha@localhost ~]$ gcc slows.c
[prabha@localhost ~]$
Client
[prabha@localhost ~]$
Result:
Thus the c program to simulate a slow start window management
technique has been completed successfully.
ALGORITHM:
Step 1: Start the process.
Step 2: Enter the number of nodes.
Step 3: Enter the cost between the nodes.
Step 4: enter the source and destination nodes to find the shortest path.
Step 5: If the given source and destination nodes are same, print the
Source and destination nodes are same, try again.
Step 6: If no path exists between source and destination node, print no
Path exists.
Step 7: If a path exists between source and destination nodes, print the as
The shortest path with cost.
Step 8: If more than one path exists between source and destination node,
Find the shortest path by adding the cost, and print the shortest path.
Step 9: Stop the process.
OSPF
Program:
#include<stdio.h>
#include<stdlib.h>
OSPF Output:
[prabha@localhost ~]$ gcc ospf.c 2 3
[prabha@localhost ~]$ ./a.out 2
Enter the number of nodes :4 2
2
2 SCHOOL
SAN INTERNATIONAL INFO Page | 36
4
2
OSPF
Enter the cost between nodes[1 2] :1
Enter the cost between nodes[1 3] :0
Enter the cost between nodes[1 4] :2
Enter the cost between nodes[2 1] :0
Enter the cost between nodes[2 3] :0
Enter the cost between nodes[2 4] :2
Enter the cost between nodes[3 1] :0
Enter the cost between nodes[3 2] :0
Enter the cost between nodes[3 4] :6
Enter the cost between nodes[4 1] :0
Enter the cost between nodes[4 2] :3
Enter the cost between nodes[4 3] :3
MENU:
1.Find shortest path
2.Exit
Result:
Thus the c program to simulate the OSPF routing protocol has been
completed successfully.
AIM:
SAN INTERNATIONAL INFO SCHOOL Page | 37
To write a c program to simulate the distance vector routing protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: Enter the no of nodes.
Step 3: Enter the distance between each node
Step 4: Enter the node to display the routing table
Step 5: Routing table contains route value from source node to each node
Step 6: Calculate the distance for each adjacent node.
Step 7: Display the resulting distance vector routing table
Step 8: Stop the process.
#include<stdio.h>
#include<string.h>
int main()
{
SAN INTERNATIONAL INFO SCHOOL Page | 38
int i,j,k,n,a[10][10],b[10][10],source,s,d;
char ch;
printf(“Enter the number of nodes: “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
a[i][j]=0;
else
{
printf(“\n Distance between the host %d %d : “,I,j);
scanf(“%d”,&a[i][j]);
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“\t%d”,a[i][j]);
}
printf(“\n”);
}
do
{
printf(“\n Enter the node to display the routing table :”);
scanf(“%d”,&source);
for(j=1;j<=n;j++)
{
if(source!=j)
{
if(a[source][j] != 0)
printf(“\n Adjacent path from %d to %d is :%d”,source,j,a[source][j]);
else
printf(“\n There is no path from %d to %d”,source,j);
}
}
printf(“\n\n Do you want to continue [y/n] “);
scanf(“%s”,&ch);
}while(ch != ‘n’);
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
0 2 1 0
0 0 1 0
0 3 0 2
0 2 3 0
[prabha@localhost ~]$
Result:
Thus the C program to simulate the distance vector routing protocol
has been completed successfully.
AIM:
ALGORITHM:
FTP Server:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Bind the socket.
Step 5: Listen to the client and accept the connection from the client.
Step 6: Get the source file name.
Step 7: Get the destination file name and transfer the source to destination and
display the content.
Step 8: Close the socket.
Step 9: Stop the process.
FTP Client:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the client.
Step 4: Connect with the server.
Step 5: Receive the file name from the server.
Step 6: Display the content of the requested file.
Step 7: Close the socket.
Step 8: Stop the process.
FTP
Server Program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<fcntl.h>
Client Program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr));
for(;;)
{
printf(“\nEnter the File Name: “);
scanf(“%s”,sendline);
printf(“\nEnter the Destination File Name: “);
scanf(“%s”,name);
fd=fopen(name,”w”);
write(sockfd,sendline,MAX);
while(1)
{
read(sockfd,recvline,MAX);
printf(“%s”,recvline);
fputs(recvline,fp);
exit(0);
}
fclose(fp);
}close(sockfd);
return 0;
}
Output
[prabha@localhost ~]$ cat sample.c
Welcome to Loyola Institute of Technology
[prabha@localhost ~]$
Server
[prabha@localhost ~]$
Client
[prabha@localhost ~]$
Result:
Thus the C program to develop a FTP application has been completed
successfully.
AIM:
To write a C program to develop a Multi-user chat Application.
Client 1:
Step 1: start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Connect with the server.
Step 5: Proceed the communication.
Step 6: Close the socket.
Step 6: Stop the process.
Client 2:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Connect with the server.
Step 5: Proceed the communication.
Step 6: Close the socket
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
int i,sd,sd2,nsd,client,sport,len;
char sendmsg[20],rcvmsg[20];
struct sockaddr_in servaddr,cliaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf(“\nCant’t created\n”);
else
printf(“\nSocket is created\n”);
servadd.sin_family=AF_INET;
servadd.sin_addr.s_addr.s=hto|(INADDR_ANY);
servaddr.sin_port=htons(sport);
sd2=bind(sd,(struct sockaddr*)&seraddr,sizeof(seraddr));
if(sd2<0)
printf(“\nCan’t bind\n”);
else
printf(“\nBinded\n”);
listen(sd,5);
do
{
Printf(“\nEnter the client number to
communication :”);
scanf(“%d”,&i);
if(i==0)
exit(0);
printf(“\nClint %d is connected\n”,i);
if(nsd<0)
printf(\nCan’t accept\n”);
else
printf(“\nAccepted\n”);
printf(“\nStart to communication
\n\nServer\t\tClient”);
do
{
recv(nsd,rcvmsg,20,0);
printf(“\t\t%s\n\n”,rcvmsg);
fgets(sendmsg,20,stdin);
len=strlen(sendmsg);
sendmsg[len-1]=’\0’;
send(nsd,sendmsg,20,0);
wait(20);
}while(strcmp(sendmsg,”bye”)!=0);
}while(i!=0);
}
Client 1Program
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int csd,cport,len;
char sendmsg[20],revmsg[20];
struct sockaddr_in servaddr;
printf(“\nEnter the port :”);
scanf(“%d”,&cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
printf(“\nCan’t Create\n”);
else
printf(“\nSocket is created\n”);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=hton|(INADDR_ANY);
servaddr.sin_port=htons(cport);
Client 2 Program
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int csd,cport,len;
char sendmsg[20],revmsg[20];
struct sockaddr_in servaddr;
printf(“\nEnter the port :”);
scanf(“%d”,&cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
printf(“\nCan’t Create\n”);
else
printf(“\nSocket is created\n”);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=hton|(INADDR_ANY);
servaddr.sin_port=htons(cport);
if(connect(csd,(struct sockaddr*)
&servaddr,sizeof(servaddr))<0)
Printf(“\nCan’t Connect\n”);
Output
Server
[prabha@localhost ~]$ gcc mucs.c
[prabha@localhost ~]$ ./a.out
Start to communication
Server Client
Hi server
Hi client1
Bye
Why?
Urgent
Ok
Bye
Bye
Client 2 is connected
Accepted
Start to communication
Server Client
Hi server
Hi client2
How r u?
Fine
Where r u?
Bye
Enter the client number to communicate :0
[prabha@localhost ~]$
Client 1
Socket is created
Connected
[prabha@localhost ~]$
Client 2
Client Server
Hi server
Hi client2
How r u?
Fine
Where r u?
Bye
[prabha@localhost ~]$
Result:
Thus the C program to develop a Multi -user Chat Application has been
completed successfully.