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 architectural patterns-layered, client-server, and application architecture.
Explain the architectural patterns-layered, client-server, and application architecture.
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
Given an IP address of 220.84.9.123 and a subnet mask of 255.255.255.240 Write down the address...
Given an IP address of 220.84.9.123 and a subnet mask of 255.255.255.240 Write down the address of the network, show your calculation. Write also the broadcast address 2. On a network with an IP address of 133.133.133.13 and a subnet mask of 255.255.248.0 what is the network ID?  Show your calculation.  Write also the broadcast address On a Class C network, you have a subnet mask of 255.255.255.192.   How many hosts per subnet do you have? On how many subnets was divided...
What is the glue, from a client perspective, between a URL and an IP address? Explain...
What is the glue, from a client perspective, between a URL and an IP address? Explain how this process works.
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...
Write two python codes for a TCP client and a server using a Socket/Server Socket. The...
Write two python codes for a TCP client and a server using a Socket/Server Socket. The client sends a file name to the server. The server checks if there is any integer value in the file content. If there is an integer number, the server will send a message to the client “Integer exists” otherwise, the message “Free of Integers”
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT