Skip to content
Snippets Groups Projects
Commit 73c3732e authored by Neeserg Parajuli's avatar Neeserg Parajuli
Browse files

A simple multihtreaded server sending hello world

parent 2c0fed23
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -7,10 +7,17 @@ ...@@ -7,10 +7,17 @@
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
# include <pthread.h>
#define MAXUSER 5
void* handleclient(void * newsockfd);
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
struct addrinfo info, *res, *p; struct addrinfo info, *res, *p;
int status; int sockfd, newsockfd;
struct sockaddr_storage clien_addr;
socklen_t clien_size;
char ipstr[INET6_ADDRSTRLEN]; char ipstr[INET6_ADDRSTRLEN];
if (argc <2){ if (argc <2){
...@@ -19,40 +26,94 @@ int main(int argc, char *argv[]){ ...@@ -19,40 +26,94 @@ int main(int argc, char *argv[]){
} }
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
info.ai_family = AF_UNSPEC; ///sets it to IPv4 info.ai_family = AF_UNSPEC; ///sets it to unspecified
info.ai_socktype = SOCK_STREAM; //TCP connection stream oriented info.ai_socktype = SOCK_STREAM; //TCP connection stream oriented
info.ai_flags = AI_PASSIVE; //use my current IP adress info.ai_flags = AI_PASSIVE; //listen on on all ip interface of machine
info.ai_protocol = 0; //set this to zero as default
if((status = getaddrinfo(NULL, argv[1], &info, &res) != 0)){ if((getaddrinfo(NULL, argv[1], &info, &res) != 0)){
fprintf(stderr,"ERROR, no port provided\n");//to check if getaddrinfo gets any addrinfos fprintf(stderr,"ERROR, no port provided\n");//to check if getaddrinfo gets any addrinfos
exit(1); exit(1);
} }
for (p = res; p!= NULL; p = p->ai_next) for (p = res; p!= NULL; p = p->ai_next)
{ {
void *addr; if((sockfd = socket(p-> ai_family, p->ai_socktype, p->ai_protocol)) < 0){
char *ipver; perror("ERROR opening socket");
continue;
}
if ((bind(sockfd, p->ai_addr, p->ai_addrlen)) < 0){
perror("ERROR on binding");
continue;
}
break;
}
if (p == NULL)
{
perror("ERROR on creating and binding");
return 2;
}
if (p->ai_family == AF_INET) { // IPv4 if(listen(sockfd,MAXUSER)<0){
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; perror("ERROR listening");
addr = &(ipv4->sin_addr); return 2;
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
} }
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr); while(1){
clien_size = sizeof (clien_addr);
if((newsockfd = accept(sockfd, (struct sockaddr *) &clien_addr, &clien_size)) < 0){
perror("ERROR accepting");
return 3;
}
pthread_t tid;
if (pthread_create(&tid, NULL, handleclient, &newsockfd) <0)
{
perror("ERROR creating thread");
return 3;
}
pthread_detach(tid);
} }
freeaddrinfo(res);
return 0; return 0;
} }
void* handleclient(void * newsockfd){
int *sockfd_ptr = (int*) newsockfd;
int numbytes;
int sockfd = *sockfd_ptr;
char buffer[256];
bzero(buffer, 256);
if ((numbytes = recv(sockfd, buffer, 255, 0)) <0)
{
perror("ERROR recieving");
return NULL;
}
printf("%s\n", buffer);
if (send(sockfd, "hello world", 13, 0) <0)
{
perror("ERROR sending");
return NULL;
}
close(sockfd);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment