Question

In: Computer Science

Develop a client /server talk application in C or C++. You should run your server program...

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

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.

Client 127.0.0.1

*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

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

Client server_IP_ address

*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. Write a readme file to explain how to run your program (5%);

2. Draw a data flow diagram to show your program design (10%);

3. Write a makefile that compiles your server and client programs (5%);

4. Comment your code properly (10%), and

Solutions

Expert Solution

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    
}


Related Solutions

Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
what will be the code in C programming for the client and server chat application for...
what will be the code in C programming for the client and server chat application for the below issue :- write the C Programming code that able client have a unique ID to be known by the server
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Explain the key difference between a web service application and a general client/server application
Explain the key difference between a web service application and a general client/server application
In this program you will develop an application that will display an amortization schedule.
Using Anaconda Python, program the following:OverviewIn this program you will develop an application that will display an amortization schedule. The word "amortize" means to "To write off gradually and systematically a given amount of money within a specific number of time periods." It will show, for each month of the loan, how much of your monthly loan payment is being applied towards interest costs, and how much is actually being applied to reduce the outstanding balance (principal) of your loan.The...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port as command line arguments​ Connect to Server​ Start "infinite loop"​ Take in message from command line​ Convert it to the packet form from last lab​ Instead of an array, store packet in a char array (string) so it can be easily sent​ Print out packet to terminal​ Send packet to server​ Print out reply from server End "infinite loop" Server Create connection on the...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port...
Instructions Write a Client/Server application to do the following​ Client​ Take in IP address and Port as command line arguments​ Connect to Server​ Start "infinite loop"​ Take in message from command line​ Convert it to the packet form from last lab​ Instead of an array, store packet in a char array (string) so it can be easily sent​ Print out packet to terminal​ Send packet to server​ Print out reply from server End "infinite loop" Server Create connection on the...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Client side program: Develop a client side program which will do the following: The client side...
Client side program: Develop a client side program which will do the following: The client side would accept the customer ID and the amount that customer has spent. The client will then forward the customer ID and the amount spend the server. (Include field validation wherever possible) a. Request connection to the server b. Accept and forward the necessary data to the server c. Receive and display the results from the server d. Close the connection after use
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT