Question

In: Computer Science

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

Solutions

Expert Solution

program for server

file name as .........server.c

#include <stdio.h> // standard input and output library

#include <stdlib.h> // this includes functions regarding memory allocation

#include <string.h> // contains string functions

#include <errno.h> //It defines macros for reporting and retrieving error conditions through error codes

#include <time.h> //contains various functions for manipulating date and time

#include <unistd.h> //contains various constants

#include <sys/types.h> //contains a number of basic derived types that should be used whenever appropriate

#include <arpa/inet.h> // defines in_addr structure

#include <sys/socket.h> // for socket creation

#include <netinet/in.h> //contains constants and structures needed for internet domain addresses

int main()

{

    time_t clock;

char dataSending[1025]; // Actually this is called packet in Network Communication, which contain data and send through.

int clintListn = 0, clintConnt = 0;

struct sockaddr_in ipOfServer;

clintListn = socket(AF_INET, SOCK_STREAM, 0); // creating socket

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

memset(dataSending, '0', sizeof(dataSending));

ipOfServer.sin_family = AF_INET;

ipOfServer.sin_addr.s_addr = htonl(INADDR_ANY);

ipOfServer.sin_port = htons(2017); // this is the port number of running server

bind(clintListn, (struct sockaddr*)&ipOfServer , sizeof(ipOfServer));

listen(clintListn , 20);

while(1)

{

printf("\n\nHi,Iam running server.Some Client hit me\n"); // whenever a request from client came. It will be processed here.

clintConnt = accept(clintListn, (struct sockaddr*)NULL, NULL);

clock = time(NULL);

snprintf(dataSending, sizeof(dataSending), "%.24s\r\n", ctime(&clock)); // Printing successful message

write(clintConnt, dataSending, strlen(dataSending));

        close(clintConnt);

        sleep(1);

     }

     return 0;

}

for client program

client.c as file name

#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 CreateSocket = 0,n = 0;

    char dataReceived[1024];

    struct sockaddr_in ipOfServer;

    memset(dataReceived, '0' ,sizeof(dataReceived));

    if((CreateSocket = socket(AF_INET, SOCK_STREAM, 0))< 0)

    {

        printf("Socket not created \n");

        return 1;

    }

    ipOfServer.sin_family = AF_INET;

    ipOfServer.sin_port = htons(2017);

    ipOfServer.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(connect(CreateSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)

    {

        printf("Connection failed due to port and ip problems\n");

        return 1;

    }

    while((n = read(CreateSocket, dataReceived, sizeof(dataReceived)-1)) > 0)

    {

        dataReceived[n] = 0;

        if(fputs(dataReceived, stdout) == EOF)

        {

            printf("\nStandard output error");

        }

        printf("\n");

    }

    if( n < 0)

    {

        printf("Standard input error \n");

    }

    return 0;

}


Related Solutions

Implement the following socket programming in C (b) Chat Application using TCP
Implement the following socket programming in C (b) Chat Application using TCP
In Simple Chat, if the server shuts down while a client is connected, the client does...
In Simple Chat, if the server shuts down while a client is connected, the client does not respond, and continues to wait for messages. Modify the client so that it responds to the shutdown of server by printing a message saying the server has shut down, and quitting. (look at the methods called connectionClosed and connectionException). //ChatClient.java // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source //...
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 limitation of the chat server is that it can handle only one client because it...
A limitation of the chat server is that it can handle only one client because it is a single threaded application. Using the pThread library, modify the chat server so that it can handle multiple clients simultaneously, i.e., by creating a new thread whenever a client is connected so that the client is handled individually with a new thread, and at the same time, by having the main thread (i.e., the thread that runs the main function) of the chat...
In the provided client and server code, the server can serve to single client at a...
In the provided client and server code, the server can serve to single client at a time. You have to change server.java code so that it can connect and serve multiple clients at the same time. Use multithreading. =============================================================================== import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { String serverHostname = new String ("127.0.0.1"); if (args.length > 0) { //pass the hsotname through cmd argument serverHostname = args[0]; } System.out.println ("Attemping to connect...
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.
TCP client and server using C programming I am having trouble on how to read in...
TCP client and server using C programming I am having trouble on how to read in the IP adress and port number from the terminal Example: Enter IP address: 127.0.0.1 Enter Port Number: 8000 in both client and server code. How do can I make I can assign the Ip address and port number using the example above. the error I get is that the client couldn't connect with the server whenever i get the port number from the user...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT