In: Computer Science
Develop a client /server talk application in C or C++. You should run your server program first, and then open another window if you use loopback to connect the local host again, or you may go to another machine to run the client program that will connect to the server program. In this case you must find the server’s IP address first, and then connect to it.
If you use loopback, your procedure may look like:
Server is ready to talk… …
Open another window, and then run the client program
*You should use netstat | head command line to check if the server is in listening state.
*You should use netstat | head command line to check if the server and client processes are in the established state.
If you use two machines to run your programs, your procedure may look like:
Server is ready to talk… …
Open another window, and then run the client program
*You should use netstat | head command to check if the server is in listen state
*You should use netstat | head command to check if the server and client processes are in the established state.
After establishing connection, the server and client programs will be ready to talk. You should start talking from the client program, and then the server responds to it. The conversation will continue to talk to each other until the client quitsby hitting Ctrl+D .
Clientserver
HelloàHello
How are you?ßHow are you?
I am fine.àI am fine.
Let us begin to workßLet us begin to work
……
Hit Ctrl + D to terminate the conversation.
You might use the echo server and client application as an example to develop your talk program.
Requirements:
1. ----------- README -----------------------
first execute server
./server
then in another window execute client by giving server_ip_address as a command line argument
./client 127.0.0.1
2.
3.
--------------------------- Makefile ----------------------------------
for server:
compile:
gcc server.c -o server
for client:
compile:
gcc client.c -o client
4.
server code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX 1024
int main(int argc, char *argv[])
{
struct sockaddr_in server, client;
int sock, new;
int sockaddr_len = sizeof(struct sockaddr_in);
int data;
char buffer[MAX];
//creating socket and checking whether socket
was created or not
if((sock = socket(AF_INET, SOCK_STREAM, 0)) ==
-1)
{
perror("socket");
exit(-1);
}
//server details
server.sin_family = AF_INET;
server.sin_port = htons(9999); //assigning
9999 as port address
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero, 8);
//binding to the port
if( bind(sock,(struct sockaddr *)&server,
sockaddr_len) == -1)
{
perror("bind");
exit(-1);
}
//listening for incoming connections
if((listen(sock, 5)) == -1)
{
perror("listen");
exit(-1);
}
while(1)
{
printf("Server is
ready to talk.....\n");
//accept the incoming
connection
if( (new = accept(sock, (struct
sockaddr *)&client, &(sockaddr_len))) == -1)
{
perror("Listen");
exit(-1);
}
data = 1;
while(data)
{
data = recv(new, buffer, MAX, 0);
//receiving message from client
if(data)
{
send(new,buffer,
data, 0);//sending the same message to client
buffer[data] =
'\0';
printf("sent
messg : %s\n",buffer);//printing the sent message
}
}
printf("Client was disconnected\n\n");
close(new); //closing the file descriptor
}
}
Client Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX 1024
int main(int argc, char *argv[])
{
struct sockaddr_in remote_server;
int sock;
char input[MAX];
char output[MAX];
int len;
//creating socket
sock = socket(AF_INET, SOCK_STREAM, 0);
//checking socket was created or not
if(sock == -1)
{
perror("socket:");
exit(-1);
}
//remote server details like port and ip
address
remote_server.sin_family = AF_INET;
remote_server.sin_port =
htons(9999); //assigning 9999 as port address
remote_server.sin_addr.s_addr =
inet_addr(argv[1]); /*IP Address will be taken from command line
argument*/
bzero(&remote_server.sin_zero, 8);
//connecting to remote server
if( (connect(sock, (struct sockaddr
*)&remote_server, sizeof(struct sockaddr_in))) == -1 )
{
perror("connect:");
exit(-1);
}
while(printf("Client:") &&
fgets(input, MAX, stdin)!=NULL)
{
send(sock, input,
strlen(input),0); //sending the message to server
len = recv(sock,
output, MAX,0); //message received from
server
output[len] =
'\0';
printf("Server:
%s\n",output); // printing the message received from the
server
}
printf("\n");
close(sock); //closing the
socket descriptor
}