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

Server Code

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];
    time_t ticks; 

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 10); 

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff)); 

        close(connfd);
        sleep(1);
     }
}

Client Code

#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>
#include <arpa/inet.h> 

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in serv_addr; 

    if(argc != 2)
    {
        printf("\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    } 

    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    } 

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000); 

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
        return 1;
    } 

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    } 

    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            printf("\n Error : Fputs error\n");
        }
    } 

    if(n < 0)
    {
        printf("\n Read error \n");
    } 

    return 0;
}

Put the following in the command line

./filename 127.0.0.1

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
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...
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...
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
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     
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT