Question

Write a program to implement a Distributed chat server using TCP sockets in ‘C’. Run the...

Write a program to implement
a Distributed chat server using
TCP sockets in ‘C’.

Run the program and screen shot the Output Please.

Homework Answers

Answer #1
//TCP Client

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
char buf[100];
int k;
int sock_desc;
struct sockaddr_in client;
memset(&client,0,sizeof(client));
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
{
printf(“Error in socket creation”);
exit(1);
}
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=3002;
k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
if(k==-1)
{
printf(“Error in connecting to server”);
exit(1);
}
while(1)
{
printf(“\nEnter data to be send to server: “);
fgets(buf,100,stdin);
if(strncmp(buf,”end”,3)==0)//Use “end” to end communication with server
break;
k=send(sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in sending”);
exit(1);
}
k=recv(sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in receiving”);
exit(1);
}
printf(“Message got from server is : %s”,buf);
}
close(sock_desc);
exit(0);
return 0;
}



server

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
char buf[100];
int k;
socklen_t len;
int sock_desc,temp_sock_desc;
struct sockaddr_in server,client;
memset(&server,0,sizeof(server));
memset(&client,0,sizeof(client));
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
{
printf(“Error in socket creation”);
exit(1);
}
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(“127.0.0.1”);
server.sin_port=3002;
k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
if(k==-1){
printf(“Error in binding”);
exit(1);
}
k=listen(sock_desc,20);
if(k==-1)
{
printf(“Error in listening”);
exit(1);
}
len=sizeof(client);//VERY IMPORTANT

temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);//VERY //IMPORTANT
if(temp_sock_desc==-1)
{
printf(“Error in temporary socket creation”);
exit(1);
}
while(1)
{
k=recv(temp_sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in receiving”);
exit(1);
}
printf(“Message got from client is : %s”,buf);
printf(“\nEnter data to be send to client: “);
fgets(buf,100,stdin);
if(strncmp(buf,”end”,3)==0)
break;
k=send(temp_sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in sending”);
exit(1);
}
}
close(temp_sock_desc);
exit(0);
return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please use Python programming language to write a client program and a server program, so that...
Please use Python programming language to write a client program and a server program, so that these two programs communicate through a TCP connection based on the following scenario: Both of the client program and the server program are running on the same localhost. The client program prompts the user to enter the radius of a circle and pass it to the server program. The server program computes the area of this circle and passes it to the client program....
Write a simple TCP program for a client that reads a line of text from its...
Write a simple TCP program for a client that reads a line of text from its standard input (keyboard) and sends the line out its socket to the server. The server reads the line from the socket and prints it in its standard output (screen). The server then reads a line from its standard input (keyboard) and sends it out its socket to the client. When the client sends “!” the server should reply back “bye” to the client and...
   Write a C++ program to generate all the truth tables needed for ( p ˄...
   Write a C++ program to generate all the truth tables needed for ( p ˄ q) ˅ (¬ p ˅ ( p ˄ ¬ q )). You need to submit your source code and a screen shot for the output
Write an assembly language program that prints your full name on the screen. Use .ASII pseudo-op...
Write an assembly language program that prints your full name on the screen. Use .ASII pseudo-op to store the characters at the top of your program Use BR to branch around the characters and use STRO to output your name. Comment each line except STOP and .END. Cut and paste the Assembler Listing into your document Paste a screen shot of the Output area of the Pep8 program Please use the name "Levin Fi" Assembler Listing Screen Shot of the...
Write an assembly language program that will implement the following:       If ( (AX >=BX) AND (CX...
Write an assembly language program that will implement the following:       If ( (AX >=BX) AND (CX < DX) ) goto LoopA Else go to LoopB (Here I want you to write the whole assembly program that could run in CMD. THANK YOU
Q) Write a computer program to test the stability of any continuous-time LTI system.( using Matlab)...
Q) Write a computer program to test the stability of any continuous-time LTI system.( using Matlab) Test your program on the impulse response.   h(t)= e^(-2t) u(t) Attached the program (Matlab) file and a screen shot of the results.
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need to write a client program and a server program for this question to be correct. ** **Cannot use socket** client the client application that will get the command from the user and pass the command to be executed from the command line, parses the command and puts a binary representation of the parse into a shared memory segment. At this point, the client will...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
The code I have written runs but I need it us UDP not TCP. Also I...
The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client. The directions: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT