Skip to content
Snippets Groups Projects
Select Git revision
  • c79f0bbf64590ce6f5b7603d05cc8c69293428ff
  • master default protected
  • hai
  • isaac
  • CheHao
  • Eldar
  • mpriymak
  • master-before-merging-with-hai
  • master-before-merging-with-isaac
  • rmi-working-before-merging-with-isaac
  • all-code-merged-by-hai-v1
11 results

ChatUpdate.java

Blame
  • Forked from Ho Dac Hai / COMP90015-DSAss2-InfinityMonkeys-remaster
    Source project has a limited visibility.
    server.c 2.07 KiB
    # include <pthread.h>
    #include "server.h"
    #include "GETheader.h"
    #include "RESPheader.h"
    
    #define MAXUSER 5
    
    void* handleclient(void * newsockfd);
    
    int main(int argc, char *argv[]){
    	struct addrinfo info, *res, *p;
    	int sockfd, newsockfd;
    	struct sockaddr_storage clien_addr;
    	socklen_t clien_size;
    	if (argc <2){
    		fprintf(stderr,"ERROR, no port provided\n");//to check if there is no port provided at start
    		exit(1);
    	}
    	memset(&info, 0, sizeof(info));
    	info.ai_family = AF_UNSPEC; ///sets it to unspecified
    	info.ai_socktype = SOCK_STREAM; //TCP connection stream oriented
    	info.ai_flags = AI_PASSIVE; //listen on on all ip interface of machine
    	info.ai_protocol = 0; //set this to zero as default
    
    	if((getaddrinfo(NULL, argv[1], &info, &res) != 0)){
    		fprintf(stderr,"ERROR, no port provided\n");//to check if getaddrinfo gets any addrinfos
    		exit(1);
    	}
    
    	for (p = res; p!= NULL; p = p->ai_next)
    	{
    		if((sockfd = socket(p-> ai_family, p->ai_socktype, p->ai_protocol)) < 0){
    			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(listen(sockfd,MAXUSER)<0){
    		perror("ERROR listening");
    		return 2;
    	}
    
    
    	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");