want code in C:
and please answer the question!
Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
void *threadFunction(int mysockfd)
{
char bufferThread[256];
int nThread;
while(1)
{
bzero(bufferThread,256);
nThread =
read(mysockfd,bufferThread,255);
//read message from mysockfd
if (nThread < 0) error("ERROR
reading from socket");
printf("Here is the message:
%s\n",bufferThread);
if(strcmp(bufferThread, "EXIT\n")
== 0)
{
printf("Network
Terminated\n");
close(mysockfd);
return
NULL;
}
//close mysockfd if message is
"EXIT\n"
nThread = write(mysockfd,"I got
your message",18);
//acknowledge to client
if (nThread < 0) error("ERROR
writing to socket");
}
close(mysockfd);
return NULL;
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr,
cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening
socket");
bzero((char *) &serv_addr,
sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr =
INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)
&serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
while(1)
{
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *)
&cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
pthread_t pth; //this
is the thread identifier
pthread_create(&pth,NULL,threadFunction,newsockfd);
//generage a new thread
}
close(sockfd);
return 0;
}
want code in C: and please answer the question! Goal: To improve the client-server model introduced...
Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...
Project Description In this project, you will be developing a multithreaded Web server and a simple web client. The Web server and Web client communicate using a text-based protocol called HTTP (Hypertext Transfer Protocol). Requirements for the Web server The server is able to handle multiple requests concurrently. This means the implementation is multithreaded. In the main thread, the server listens to a specified port, e.g., 8080. Upon receiving an HTTP request, the server sets up a TCP connection to...
In Python, make changes in the client code, so that the client allows the user to continue to send multiple requests until the user types in “Quit”. This means that the client process should not exit after the user sends one request and receives one response. Instead, the client process should be able to receive subsequent inputs from the user. You need to have a loop in the client code, so that it can accept the user request until the...
This is in C. For this assignment we will write a simple database server. We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows: typedef struct student { char lname[ 10 ], initial, fname[ 10 ]; unsigned long SID; float GPA; } SREC; Part One – the Server We will create a database server. The job of the server is to accept a...