Last active
          April 5, 2020 10:27 
        
      - 
      
- 
        Save deep5050/0cf3775fb632f02451ba19c5c42b1577 to your computer and use it in GitHub Desktop. 
    ./server <port> <backlog>
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include<stdio.h> | |
| #include<stdlib.h> | |
| #include<unistd.h> | |
| #include<sys/socket.h> | |
| #include<sys/types.h> | |
| #include<string.h> | |
| #include <netdb.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include<signal.h> | |
| #include<errno.h> | |
| #define max_mssg_len 1024 | |
| int server_fd; | |
| void INTR_handler(int sig) | |
| { | |
| close(server_fd); | |
| puts("\n-----Server closed-----"); | |
| exit(0); | |
| } | |
| int main(int argc,char *argv[]) | |
| { | |
| int myport,backlog; | |
| if(argc !=3 ) | |
| { | |
| puts("USAGE: <portno> <backlog>"); | |
| exit(EXIT_FALIURE); | |
| } | |
| if(argv[1]=="" || argv[1] ==NULL) | |
| { | |
| puts("ERROR: Enter a valid port number"); | |
| exit(EXIT_FAILURE); | |
| } | |
| myport = atoi(argv[1]); | |
| if( argv[2] == "" || argv[2] == NULL) | |
| { | |
| puts("ERROR: Enter a valid backlog value"); | |
| exit(EXIT_FAILURE); | |
| } | |
| backlog = atoi(argv[2]); | |
| struct sockaddr_in server_addr,client_addr; | |
| int client_fd,server_status,bind_status,listen_status,accept_status; | |
| socklen_t client_addr_size; | |
| char buff[max_mssg_len]; | |
| int n=0; | |
| memset(&server_addr,0,sizeof(server_addr)); | |
| memset(&client_addr,0,sizeof(client_addr)); | |
| /* | |
| struct sockaddr_in { | |
| short int sin_family; // Address family, AF_INET | |
| unsigned short int sin_port; // Port number | |
| struct in_addr sin_addr; // Internet address | |
| unsigned char sin_zero[8]; // Same size as struct sockaddr | |
| }; | |
| struct in_addr { | |
| uint32_t s_addr; // that's a 32-bit int (4 bytes) | |
| }; | |
| */ | |
| server_addr.sin_family = AF_INET; // IPv4 | |
| server_addr.sin_port = htons(myport); // big-endian short | |
| server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // big-endian long // my own IPv4 address | |
| server_fd = socket(AF_INET,SOCK_STREAM,0); | |
| if ( -1 == server_fd) | |
| { | |
| perror("ERROR"); | |
| exit(0); | |
| } | |
| bind_status = bind(server_fd,(struct sockaddr *) &server_addr,sizeof(server_addr)); | |
| if ( -1 == bind_status) | |
| { | |
| perror("ERROR"); | |
| exit(0); | |
| } | |
| listen_status = listen(server_fd,backlog); | |
| if( -1 == listen_status ) | |
| { | |
| perror("ERROR"); | |
| exit(0); | |
| } | |
| printf("-----------sever is UP and LISTENING on port: %d -----------\n",myport); | |
| char greetings[] = "~~~~~~~~SERVER: hi client, you are now connected~~~~~~~~\n"; | |
| int bytes_sent; | |
| char addr[30]=""; | |
| while(1) | |
| { | |
| signal(SIGINT,INTR_handler); | |
| puts("--------Waiting for a connection--------\n"); | |
| client_fd = accept(server_fd,(struct sockaddr *) &client_addr, &client_addr_size); | |
| bytes_sent = send(client_fd,&greetings,sizeof(greetings),0); | |
| inet_ntop(client_addr.sin_family,&client_addr.sin_addr.s_addr,addr,sizeof(addr)); | |
| printf("new client: %s :%d\n",addr,ntohs(client_addr.sin_port)); | |
| bzero(buff,max_mssg_len); | |
| puts("----------Client says---------\n"); | |
| while(1) | |
| { | |
| recv(client_fd,&buff,sizeof(buff),0); | |
| if(strncmp(buff,"!q",2)==0) | |
| { | |
| close(client_fd); | |
| printf("-------connection terminated by the client-------\n"); | |
| break;; | |
| } | |
| fflush(stdout); | |
| printf("%s",buff); | |
| } | |
| } | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment