int main(int argc,char **argv) { int sock = socket(PF_INET, SOCK_DGRAM, 0); struct sockaddr_in address; int errors=0; if (sock<0) { perror("socket"); return 1; }; address.sin_family = AF_INET; address.sin_port = htons(44444); address.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock,(struct sockaddr *)&address,sizeof(address))<0) { perror("bind"); return 1; } signal(SIGUSR1, printlist); daemonise(argv[0]); put_pid("wansd"); openlog(argv[0],LOG_PID,LOG_DAEMON); syslog(LOG_NOTICE,"%s started.",argv[0]); for (;;) { int addrlen=sizeof(address); char buffer[65536]; int data=recvfrom(sock,(void *)buffer,sizeof(buffer),0, (struct sockaddr *)&address,&addrlen); if (data<0) { errors++; sleep(1); if (errors>10) { syslog(LOG_ERR,"recvfrom: %m\n"); syslog(LOG_ERR,"Too many errors, bailing.\n"); return 1; } } else { errors=0; } buffer[sizeof(buffer)-1]='\0'; sendupdate(sock,dopacket(buffer,data,address)); }; return 0; }
int main(int argc,char** argv){ int yes = 1; int i,j,rv; char *commands ; int length = 0 ; char array[100]; char *cmdargs; char *res ; cmdargs = array; head = NULL ; headRef = &head; int ln1; int obytes ; int retSel; char ipremote[INET_ADDRSTRLEN]; /* Bufferrs for read and write */ char buf[350] ; char opbuff[350]; if(argc != 3){ printf("Invalid call ..exiting............\n"); exit(0); } if(strcasecmp(argv[1],"s") != 0 && strcasecmp(argv[1],"c") != 0 ){ printf("Invalid arguments ..exiting............\n"); exit(0); } int iport ; iport = atoi(argv[2]); if(iport <= 1024 || iport > 45355){ printf("Invalid port ..exiting............\n"); exit(0); } /* Setting the client or server flag */ if(strcasecmp(argv[1],"S")== 0 ){ serverclientflag = 1; }else{ serverclientflag = 2 ; } portnum = argv[2]; // setting the portnum to global variable //Method to initialise the system initialize(); for(;;){ read_fds = master ; if(select(fdmax + 1,&read_fds,NULL,NULL,NULL) == -1){ perror("select"); exit(4); } //run through the existing connections looking for data for(i=0 ; i<= fdmax ; i++){ if(FD_ISSET(i,&read_fds)){ // we got one conncetion if(i == 0){ //read the command from the terminal //printf("[PA1]$"); if((res=fgets(cmd,sizeof cmd,stdin) )!= NULL){ length = strlen(cmd); cmd[length-1] = '\0'; //fflush(stdin); commands = cmd ; returnfd = processCommand(commands,serverclientflag,portnum); //get the file descriptor for server connection fflush(stdout); if(returnfd == -1){ printf("Invalid arguments to command."); fflush(stdout); printf("\n[PA1]$:"); fflush(stdout); } } }else{ if(i == listener){ //handle new connection accept the connection and add it to the read set addrlen = sizeof remoteaddr ; newfd = accept(listener,(struct sockaddr *)&remoteaddr,&addrlen); if(newfd == -1){ perror("accept"); } else{ /* Getting the ip address of the remote connection */ /*And adding the connection to list*/ struct sockaddr_in *s = (struct sockaddr_in *)&remoteaddr; struct hostent *hnet; inet_ntop(AF_INET, &s->sin_addr, ipremote, sizeof ipremote); int port = s->sin_port; char tempport[33]; sprintf(tempport,"%d",port); //hnet = gethostbyaddr(&ipremote,sizeof ipremote,AF_INET); if(serverclientflag == 1){ getConnectDetails(newfd,tempport,&head,ipremote); }else{ getConnectDetails(newfd,tempport,&headConnectdNode,ipremote); } //adding the connection details in the memory //addClientstoList(&headConnectdNode,ipremote,portnum,hnet->h_name,iconnid,i); //portnum; printf("----------A new connection requested from :%s",inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP,INET6_ADDRSTRLEN)); printf ("\n---------------------Connected to :%s-----------\n",inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP,INET6_ADDRSTRLEN)); FD_SET(newfd,&master); //End Send the updated list of clients if(newfd > fdmax){ fdmax = newfd ; } // process register command } }else{ /*handle data from the client clear the data from the buffer */ if((nbytes = recv(i,buf,sizeof buf,0)) <= 0 ){ fflush(stdout); if(nbytes == 0 ){ //connection closed if(serverclientflag == 1){ //Reset the client list and push data to other clients updateClientList(i,serverclientflag) ; buildclist(head); sendupdate(i,listener); }else{ updateClientList(i,serverclientflag); iconnid = iconnid - 1 ; } printf("\n[PA1]$:"); fflush(stdout); }else{ perror("recv"); printf("\n wrog connection"); } //close(i); FD_CLR(i,&master); printf("\n[PA1]$:"); }else{ //printf("\n Bytes received is %d",nbytes); //we have data from the client //buf[nbytes] ='\0'; //printf("\nBytes %d",nbytes) ; //Get the data about the server if(serverclientflag == 1){ //Adding the server details first //getClientDetails(i,buf,&head); char ips[INET6_ADDRSTRLEN] ; char *rip = ips ; updateConnctdPort(i,buf,&head,rip); //addClientstoList(&head,ipaddress,portnum,"dokken.cse.buffalo.edu",1); ln1 =buildclist(head); strcpy(opbuff,sbcheckdata); obytes = send(i,opbuff,sizeof opbuff,0); if(obytes < 0){ perror("senddata"); } //obytes = send(i,"HEllo",6,0); fflush(stdout); //printf("\n Bytes transferred %d",obytes); }else{ processCmd(buf,i,nbytes); memset(buf,0,sizeof buf); } if(serverclientflag == 1 ){ //Send the updated data to all the clients for(j=0;j<=fdmax;j++){ if(FD_ISSET(j,&master)){ //need not send the data to listener && this machine if(j!=listener && j!=i){ if(send(j,opbuff,sizeof opbuff,0)== -1){ //perror("Sending the list data"); } } } } } //Send the updated data to all the clients fflush(stdout); printf("\n[PA1]$:"); fflush(stdout); } } } } } //end of the readset polling loop } //end of the running loop return 0 ; }