Question

In: Computer Science

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()


Solutions

Expert Solution

/Below code will as you wanted...

//server.c

/*

** C socket server example, handles multiple clients using threads

** Compile

** gcc server.c -lpthread -o server

*/

#include <stdio.h>

#include <string.h> // strlen

#include <stdlib.h> // strlen

#include <sys/socket.h>

#include <arpa/inet.h> //inet_addr

#include <unistd.h> // write

#include <pthread.h> // for threading, link with lpthread

// the thread function

void *connection_handler(void *);

int main(int argc, char *argv[]) {

   int socket_desc, client_sock, c;

   struct sockaddr_in server, client;

   // Create socket

   socket_desc = socket(AF_INET, SOCK_STREAM, 0);

   if(socket_desc == -1) {

       printf("Could not create socket");

   }

   puts("Socket created");

   // Prepare the sockaddr_in structure

   server.sin_family = AF_INET;

   server.sin_addr.s_addr = INADDR_ANY;

   server.sin_port = htons(8888);

   // Bind

   if(bind(socket_desc,(struct sockaddr *)&server, sizeof(server)) < 0) {

       // print the error message

       perror("bind failed. Error");

       return 1;

   }

   puts("bind done");

   // Listen

   listen(socket_desc, 3);

   // Accept and incoming connection

   puts("Waiting for incoming connections...");

   c = sizeof(struct sockaddr_in);

   // Accept and incoming connection

   puts("Waiting for incoming connections...");

   c = sizeof(struct sockaddr_in);

   pthread_t thread_id;

   while((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) {

       puts("Connection accepted");

       if(pthread_create( &thread_id, NULL, connection_handler, (void*) &client_sock) < 0) {

           perror("could not create thread");

           return 1;

       }

       // Now join the thread, so that we dont terminate before the thread

       // pthread_join( thread_id, NULL);

       puts("Handler assigned");

   }

   if (client_sock < 0) {

       perror("accept failed");

       return 1;

   }

   return 0;

}

//

// This will handle connection for each client

//

void *connection_handler(void *socket_desc) {

   // Get the socket descriptor

   int sock = *(int*)socket_desc;

   int read_size;

   char *message, client_message[2000];

   // Send some messages to the client

   message = "Greetings! I am your connection handler\n";

   write(sock, message, strlen(message));

   message = "Now type something and i shall repeat what you type \n";

   write(sock, message, strlen(message));

   // Receive a message from client

   while( (read_size = recv(sock, client_message, 2000, 0)) > 0 ) {

       // end of string marker

       client_message[read_size] = '\0';

       // Send the message back to client

       write(sock, client_message, strlen(client_message));

       // clear the message buffer

       memset(client_message, 0, 2000);

   }

   if(read_size == 0) {

       puts("Client disconnected");

       fflush(stdout);

   }

   else if(read_size == -1) {

       perror("recv failed");

   }

   return 0;

}

//client.c

/* CLIENT CODE */

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

int main()

{

int clientSocket;

char buffer[1024];

struct sockaddr_in serverAddr;

socklen_t addr_size;

/*---- Create the socket. The three arguments are: ----*/

/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  

/*---- Configure settings of the server address struct ----*/

/* Address family = Internet */

serverAddr.sin_family = AF_INET;

/* Set port number, using htons function to use proper byte order */

serverAddr.sin_port = htons(8888);

/* Set IP address to localhost */

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

/* Set all bits of the padding field to 0 */

memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*---- Connect the socket to the server using the address struct ----*/

addr_size = sizeof serverAddr;

connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

/*---- Read the message from the server into the buffer ----*/

recv(clientSocket, buffer, 1024, 0);

/*---- Print the received message ----*/

printf("Data received: %s",buffer);   

return 0;

}

output:


Related Solutions

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...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
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.
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
using c++. ALWAYS GRADE MY ANSWERS Write a program that reads students’ names followed by their...
using c++. ALWAYS GRADE MY ANSWERS Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type...
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT