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...
Run the socket programming examples using the client and server programs, as demonstrated in class. You...
Run the socket programming examples using the client and server programs, as demonstrated in class. You may have to install Python 3 on your laptop. Place the server program on your server account. Place the client program on your laptop. Start the server program on the server. Then execute the client program on your client to send a request to the server. The server program should process the request and send a response to the client. Terminate the server programs...
Write a Python Program to ask the team captain to enter the scores of the top...
Write a Python Program to ask the team captain to enter the scores of the top 4 team members. Using the max function, output the top score. Insert into your HW document a screen shot of your program file and of the output in IDLE when you run the program.
   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...
implement a lexical analyzer that returns a token lexeme pair to recognize vaild C/C++ identifiers. (you...
implement a lexical analyzer that returns a token lexeme pair to recognize vaild C/C++ identifiers. (you do not have to exclude the reserve words). Do not use String library. run the program once to accept a valid identifier second time to reject a string that is not an indentifier. Submit code, screen shot of each execution
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.
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...