daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Dec 15th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     ********************************************************************
  3.     Odsek:          Elektrotehnika i racunarstvo
  4.     Departman:      Racunarstvo i automatika
  5.     Katedra:        Racunarska tehnika i racunarske komunikacije (RT-RK)
  6.     Predmet:        Osnovi Racunarskih Mreza 1
  7.     Godina studija: Treca (III)
  8.     Skolska godina: 2018/2019
  9.     Semestar:       Zimski (V)
  10.    
  11.     Ime fajla:      client.c
  12.     Opis:           TCP klijent
  13.    
  14.     Platforma:      Raspberry Pi 2 - Model B
  15.     OS:             Raspbian
  16.     ********************************************************************
  17. */
  18. #include<stdio.h>
  19. #include<string.h>
  20. #include<sys/socket.h>
  21. #include<arpa/inet.h>
  22.  
  23. #define DEFAULT_BUFLEN 512
  24. #define DEFAULT_PORT   27015
  25.  
  26. int sock;
  27. struct sockaddr_in server;
  28. char message[DEFAULT_BUFLEN] , server_reply[DEFAULT_BUFLEN];
  29. char user[DEFAULT_BUFLEN];
  30. char password[DEFAULT_BUFLEN];
  31.  
  32. void login();
  33.  
  34. int main(int argc , char *argv[])
  35. {
  36.      
  37.     //keep communicating with server
  38.     login();
  39.      
  40.     close(sock);
  41.    
  42.     return 0;
  43. }
  44.  
  45. void login(){
  46.     //Create socket
  47.     sock = socket(AF_INET , SOCK_STREAM , 0);
  48.     if (sock == -1)
  49.     {
  50.         printf("Could not create socket");
  51.     }
  52.     puts("Socket created");
  53.      
  54.     server.sin_addr.s_addr = inet_addr("127.0.0.1");
  55.     server.sin_family = AF_INET;
  56.     server.sin_port = htons( DEFAULT_PORT );
  57.  
  58.     //Connect to remote server
  59.     if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
  60.     {
  61.         perror("connect failed. Error");
  62.         return;
  63.     }
  64.      
  65.     puts("Connected\n");
  66.     while(1)
  67.     {
  68.         memset(server_reply,0,strlen(server_reply));
  69.         printf("Enter username : ");
  70.         scanf("%s" , user);
  71.          
  72.         //Send some data
  73.         if( send(sock , user , strlen(user) , 0) < 0)
  74.         {
  75.             puts("Send failed");
  76.             return;
  77.         }
  78.  
  79.         printf("Enter password : ");
  80.         scanf("%s" , password);
  81.          
  82.         //Send some data
  83.         if( send(sock , password , strlen(password) , 0) < 0)
  84.         {
  85.             puts("Send failed");
  86.             return;
  87.         }
  88.  
  89.          
  90.         //Receive a reply from the server
  91.         if( recv(sock , server_reply , DEFAULT_BUFLEN , 0) < 0)
  92.         {
  93.             puts("recv failed");
  94.             break;
  95.         }
  96.          
  97.         puts("Server reply :");
  98.         puts(server_reply);
  99.  
  100.         if(strcmp(server_reply, "success!") == 0){
  101.             return;
  102.         }
  103.     }
  104. }
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