SHARE
TWEET

Untitled

a guest Dec 15th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<sys/socket.h>
  4. #include<arpa/inet.h>
  5. #include<unistd.h>
  6.  
  7. #define DEFAULT_BUFLEN 512
  8. #define DEFAULT_PORT   27015
  9.  
  10.    int socket_desc , client_sock , c , read_size;
  11.    struct sockaddr_in server , client;
  12.    char client_message[DEFAULT_BUFLEN];
  13.    char username[] = "user";
  14.    char password[] = "password";
  15.    char succ[] = "success!";
  16.    char fail[] = "failure!";
  17.    char message1[DEFAULT_BUFLEN], message2[DEFAULT_BUFLEN];
  18.  
  19. void login();
  20.  
  21. int main(int argc , char *argv[])
  22. {
  23.  
  24.    
  25.     login();
  26.    
  27.    
  28.  
  29.     if(read_size == 0)
  30.     {
  31.         puts("Client disconnected");
  32.         fflush(stdout);
  33.     }
  34.     else if(read_size == -1)
  35.     {
  36.         perror("recv failed");
  37.     }
  38.      
  39.     return 0;
  40. }
  41.  
  42. void login(){
  43.     socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  44.     if (socket_desc == -1)
  45.     {
  46.         printf("Could not create socket");
  47.     }
  48.     puts("Socket created");
  49.      
  50.     server.sin_family = AF_INET;
  51.     server.sin_addr.s_addr = INADDR_ANY;
  52.     server.sin_port = htons( DEFAULT_PORT );
  53.      
  54.     if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
  55.     {
  56.         perror("bind failed. Error");
  57.         return;
  58.     }
  59.    
  60.     puts("bind done");
  61.      
  62.     listen(socket_desc , 3);
  63.      
  64.     puts("Waiting for incoming connections...");
  65.     c = sizeof(struct sockaddr_in);
  66.    
  67.     client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
  68.     if (client_sock < 0)
  69.     {
  70.         perror("accept failed");
  71.         return;
  72.     }
  73.     puts("Connection accepted");
  74.    
  75.     while(1)
  76.     {
  77.         memset(client_message,0,strlen(client_message));
  78.         if((read_size=recv(client_sock , client_message ,       DEFAULT_BUFLEN , 0)) < 0){
  79.         puts("recv failed");
  80.         return;
  81.     }
  82.         strcpy(message1, client_message);
  83.        
  84.         if((read_size=recv(client_sock , client_message ,       DEFAULT_BUFLEN , 0)) < 0){
  85.         puts("recv failed");
  86.         return;
  87.     }
  88.         strcpy(message2, client_message);
  89.        
  90.         if((strcmp(message1, username)!=0) || (strcmp(message2, password)!=0)){
  91.             if(send(client_sock, fail, strlen(fail), 0)<0)
  92.             {
  93.             puts("Send failed");
  94.             return;
  95.             }
  96.         }
  97.         else{
  98.             if(send(client_sock, succ, strlen(succ), 0)<0)
  99.             {
  100.             puts("Send failed");
  101.             return;
  102.             }
  103.             break;
  104.         }
  105.     }
  106.  
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top