I\'m trying to create a client server model written in C. I believe to have comp
ID: 3596358 • Letter: I
Question
I'm trying to create a client server model written in C. I believe to have completed the client portion but the server is giving me issues. Below is the pseudo code for what is needed.
Pseudo code for the server
Create a socket
Bind the socket to server's address and port
Loop
{
Listen to the socket
When a request arrives, accept the request and establish connection
Generate a new thread (Threadfunction) to take care of the communication on the new connection (you need to pass the newsockfd to it)
}
void * Threadfunction (int mysockfd)
loop
{
Read a message from mysockfd
If message is "EXIT", close mysockfd and terminate this thread.
Otherwise, reply an acknowledge to the client (using write( ))
}
This is what I have so far:
#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 *threadFunc(void *arg)
{
char *str;
int i = 0;
str = (char*)arg;
while(i < 110)
{
usleep(1);
printf("threadFunc says: %s ",str);
++i;
}
return NULL;
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, mysockfd;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr, "ERROR, no port provided ");
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);
pthread_t mysockfd = threadFunc(newsockfd);
//mysockfd = pthread_create(newsockfd, NULL,threadFunc,"processing...");
}
int pthread_create(mysockfd, newsockfd, threadFunc, "foo");
//pthread_join(mysockfd,NULL);
if (mysockfd < 0)
error("ERROR on accept");
bzero(buffer, 256);
n = read(mysockfd, buffer, 255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s ",buffer);
n = write(mysockfd, "I got your message", 18);
if (n < 0) error("ERROR writing to socket");
if ("%s " == "EXIT"){
printf("Client exited");
close(mysockfd);
pthread_exit;
close(sockfd);
};
return 0;
}
Explanation / Answer
#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 ",bufferThread);
if(strcmp(bufferThread, "EXIT ") == 0)
{
printf("Network Terminated ");
close(mysockfd);
return NULL;
}
//close mysockfd if message is "EXIT "
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 ");
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;
}