Socket multithreading Implementation C -



Socket multithreading Implementation C -

i working on implementing multithread multi client single server socket in c. whatever reason program, when using pthread_create() create new thread, not advance past line of code. have set print lines before , after line of code , of print lines before hand print fine none of them after print. leads me believe pthread_create() somehow buggy. unusual thing can have 1 client connect , sent/receive info server because loop listen() command in not advancing cannot take on additional clients. appreciate help in matter.

server code

#include <stdio.h> #include <stdlib.h> //for ios #include <string.h> #include <unistd.h> #include <sys/types.h> //for scheme calls #include <sys/socket.h> //for sockets #include <netinet/in.h> //for net #include <pthread.h> void error(const char *msg) { perror(msg); exit(1); } void *threadfunc(int mysockfd) { int n; char buffer[256]; { bzero(buffer,256); n = read(mysockfd,buffer,255); if (n < 0) { error("error reading socket"); } else if(strcmp(buffer, "exit\n") == 0) { printf("exit user\n"); pthread_exit(null); } else { printf("here message: %s\n",buffer); n = write(mysockfd,"i got message",18); if (n < 0) { error("error writing socket"); } } }while(n >= 0); } int main(int argc, char *argv[]) { int sockfd; int newsockfd; int portno; pthread_t pth; int n; /*n homecoming value read() , write() calls; i.e. contains number of characters read or written.*/ int = 0; printf("after var decl"); socklen_t clilen; /*clilen stores size of address of client. needed take scheme call.*/ char buffer[256]; /*the server reads characters socket connection buffer.*/ struct sockaddr_in serv_addr; struct sockaddr_in cli_addr; 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"); } { printf("before listen"); listen(sockfd,5); printf("after listen"); clilen = sizeof(cli_addr); printf("before accept"); newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); printf("after accept"); pthread_create(&pth,null,threadfunc(newsockfd),(void*) &i); printf("after pthread create"); if (newsockfd < 0) { error("error on accept"); } }while(1 == 1); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) { error("error reading socket"); } printf("here message: %s\n",buffer); if (n < 0) error("error writing socket"); close(newsockfd); close(sockfd); homecoming 0;

and here client code

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> /*the file netdb.h defines construction hostent, used below.*/ void error(const char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd; int portno; int n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0) { error("error opening socket"); } server = gethostbyname(argv[1]); if (server == null) { fprintf(stderr,"error, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = af_inet; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { error("error connecting"); } { printf("please come in message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if(strcmp(buffer,"exit\n") == 0) { printf("connection terminated\n"); break; } if (n < 0) { error("error writing socket"); } bzero(buffer,256); n = read(sockfd,buffer,255); printf("%s\n",buffer); if (n < 0) { error("error reading socket"); printf("%s\n",buffer); } }while(1 == 1); close(sockfd); homecoming 0; }

two errors:

you casting much, place here should inaddr stuff. you not listening compiler, crank warning level.

now, problem (or maybe one?) this:

pthread_create(&pth,null,threadfunc(newsockfd),(void*) &i);

this phone call threadfunc(newsockfd) , pass result pthread_create. sec part never happen though, because function calls pthread_exit or falls off end without returning anything, cause happen.

c multithreading sockets pthreads client-server

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -