Question

In: Computer Science

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 IP address/port​ (obtain these from command line args)
    • Start infinite loop​
      • Wait for messages​
      • When a message is received​
        • Check to see if it is valid, including the checksum calculation​
        • If valid​
          • Print out message to terminal​
          • Send message back to client saying message received​
        • If not valid​
          • Send message back to client saying message rejected​
      • End infinite loop

Solutions

Expert Solution

//*********************************TCP_client.c********************************//

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

int main()

{

        char sendMessage[512],receiveMessage[512];

        int sock, result;

        struct hostent *host;

        struct sockaddr_in serverAdd;

        host = gethostbyname(“xxx.xx.xx.xx”);     

        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)

            {

            perror(“Socket creation failed”);

exit(1);

        }

        serverAdd.sin_family = AF_INET;    

        serverAdd.sin_port = htons(5000);  

        serverAdd.sin_addr = *((struct in_addr *)host->h_addr);

        bzero(&(serverAdd.sin_zero),8);

        if (connect(sock, (struct sockaddr *)&serverAdd, sizeof(struct sockaddr)) == -1)

        {

            perror(“Connection failed”);

            exit(1);

        }

        while(1)

        {

                        result = recv(sock,receiveMessage,1024,0);

                        receiveMessage[result] = ”;

                        printf(“nRecieved Message: %s ” , receiveMessage);

                        printf(“nSEND The message: “);

                        fgets(sendMessage,512,stdin);

                        send(sock,sendMessage,strlen(sendMessage), 0);

        }  

        return 0;

}

//*********************************TCP_client.c********************************//

Server side program:

//*********************************TCP_server.c*******************************//

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

int main()

{

         char sendMessage[1024] ,receiveMessage[1024];

         int sock, connected, result;        

        struct sockaddr_in serverAdd, clientAdd;   

        int length;

        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)

        {

            perror(“Socket creation is failed”);

            exit(1);

        }

        if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&1,sizeof(int)) == -1)

       {

            perror(“Set socket option”);

            exit(1);

        }

       

        serverAdd.sin_family = AF_INET;        

        serverAdd.sin_port = htons(5000);

serverAdd.sin_addr.s_addr = INADDR_ANY;

        bzero(&(serverAdd.sin_zero),8);

       if (bind(sock, (struct sockaddr *)&serverAdd, sizeof(struct sockaddr))== -1)

       {

            perror(“Unable to bind”);

            exit(1);

        }

        if (listen(sock, 3) == -1)

       {

            perror(“Listen”);

            exit(1);

        }

        printf(“TCPServer Waiting for client connectionn”);

        fflush(stdout);

        while(1)

        {

            length = sizeof(struct sockaddr_in);

            connected = accept(sock,(struct sockaddr *)&clientAdd,&length);

            printf(“Server is connected with IP address %s and port %d    n”,inet_ntoa(clientAdd.sin_addr),ntohs(clientAdd.sin_port));

           

while (1)

            {

                        printf(” SEND the message : “);

                        fgets(sendMessage,100,stdin);

                        send(connected, sendMessage,strlen(sendMessage), 0);

result = recv(connected,receiveMessage,512,0);

receiveMessage[result] = ”;

printf(“Received message : %s n” , receiveMessage);

                        fflush(stdout);

            }

        }      

        return 0;

}

//*********************************TCP_server.c*******************************/
write machine ip in host = gethostbyname(“xxx.xx.xx.xx”); run both program in same machine .

first compile the c code gcc -c clinet.c and gcc -c server.c then run the compiled files program .

./client in one terminal anf ./server in another terminal .

hurray


Related Solutions

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...
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.
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
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
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...
A email application is an instance of a client-server model of computer networking. T/F Which of...
A email application is an instance of a client-server model of computer networking. T/F Which of the following command is used to list out process details in Unix system. mkdir process_a ls -;a bg ps -l -u $(whoami) Which of the file is used to sink the output of a process to Null. /dev/null /dev/tty /dev /dev/empty What does a pipe operator do? performs an OR operation takes input form a file and fees as input to other command it...
Consider a router with the: IP address 192.168.1.254/26 Subnet mask: 255.255.255.192 Network Address: 192.168.1.192 Write the...
Consider a router with the: IP address 192.168.1.254/26 Subnet mask: 255.255.255.192 Network Address: 192.168.1.192 Write the relevant DHCP subnet configuration text for this network where the first 50 addresses will be dynamically allocated to hosts.
What is the network address if given this IP address and the following subnet mask?  Please note...
What is the network address if given this IP address and the following subnet mask?  Please note that the network may be different if the subnet mask is different. IP address 178.47.188.176 How many network bits if the following subnet masks? 255.254.0.0 255.255.252.0 255.255.255.248    4. 255.128.0.0     
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()
For this assignment you are to write both the server and client applications for a Knock-Knock...
For this assignment you are to write both the server and client applications for a Knock-Knock joke system based on the Java TCP socket client-server example that has been discussed in lectures. The joke protocol goes like this: Client: "Tell me a joke." Server: "Knock knock!" Client: "Who's there?" Server: "Witches." Client: "Witches who?" Server: "Witches the way to the store." Client: "Groan." The Client The client code should connect to the joke server and allow the user to type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT