Advertisement
Guest User

client

a guest
Jan 13th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include<stdio.h> //printf
  2. #include<string.h> //strlen
  3. #include<stdlib.h> //atoi
  4. #include<sys/socket.h> //socket
  5. #include<arpa/inet.h> //inet_addr
  6. #include <fcntl.h> //for open
  7. #include <unistd.h> //for close
  8.  
  9. #define DEFAULT_BUFLEN 512
  10. #define DEFAULT_PORT 27015
  11.  
  12. char username[32],password[15];
  13. char chek[2];
  14.  
  15.  
  16. void SendMessage();
  17. void ReceiveMessage();
  18. void LogIn();
  19. void LogOut();
  20.  
  21.  
  22.  
  23. void LogIn()
  24. {
  25. int j;
  26.  
  27.  
  28. begin:
  29.  
  30. for(j = 0; j < 15; j++)
  31. username[j] = '\0';
  32.  
  33. for(j = 0; j < 15; j++)
  34. password[j] = '\0';
  35.  
  36. printf("\n");
  37. printf(" \n Connecting to proxy server \n");
  38. printf("\n");
  39. char buffer[256];
  40. char *ptrBuff;
  41. ptrBuff = buffer;
  42. *(ptrBuff++) = 5;
  43. *(ptrBuff++) = 2;
  44. *(ptrBuff++) = 0x00;
  45. *(ptrBuff++) = 0x02;
  46. send(proxySocket, ptrBuff, buffer-ptrBuff, 0);
  47.  
  48. printf("Username: ");
  49. gets(username);
  50. printf("Password: ");
  51. gets(password);
  52.  
  53. send(sock, username, 15, 0);
  54. send(sock, password, 15, 0);
  55.  
  56. recv(Proxysock, chek, 2, 0);
  57. if(chek[0] == '0'){
  58. printf("\n Wrong username/password! Try again. \n");
  59. goto begin;
  60. }
  61. else if(chek[0] == '1')
  62. printf("\n You have access to server\n");
  63.  
  64. }
  65.  
  66.  
  67. void ReceiveMessage()
  68. {
  69.  
  70. char message[20];
  71. int j;
  72.  
  73. recv(Proxysock,message,256,0);
  74. printf(" \n You sent this message to server \n");
  75.  
  76. printf(message);
  77. printf("\n");
  78.  
  79.  
  80. }
  81.  
  82. void SendMessage()
  83. {
  84.  
  85. char message[256];
  86.  
  87. printf("\n Send message to server.\n");
  88.  
  89. printf("\n Message: \n");
  90. printf("\n");
  91. scanf(" %s",message);
  92. if(send(Proxysock, message, 256, 0) < 0)
  93. printf("Sending failed.");
  94. else
  95. printf("\n Message sent to server.\n", receiver);
  96.  
  97.  
  98. }
  99.  
  100. void LogOut()
  101. {
  102.  
  103. printf("\n You are logged out.\n\n");
  104. printf(" \n --------------------- \n");
  105.  
  106. LogIn();
  107.  
  108. }
  109.  
  110. int main(int argc , char *argv[])
  111. {
  112. char option='1';
  113. char answer='N';
  114. struct sockaddr_in proxy_server_side;
  115.  
  116. Proxysock = socket(AF_INET , SOCK_STREAM , 0);
  117.  
  118. proxy_server_side.sin_addr.s_addr = inet_addr("127.0.0.1");
  119. proxy_server_side.sin_family = AF_INET;
  120. proxy_server_side.sin_port = htons(DEFAULT_PORT);
  121.  
  122. if (connect(Proxysock , (struct sockaddr *)&proxy_server_side , sizeof(proxy_server_side)) < 0){
  123. perror("Connect failed. Error");
  124. return 1;
  125. }
  126.  
  127. LogIn();
  128.  
  129. while(answer=='N')
  130. {
  131.  
  132. SendMessage();
  133. ReceiveMessage();
  134. printf("Would you like to dissconnect? Y/N \n");
  135. scanf(answer);
  136. }
  137. send(Proxysock,option,2,0);
  138. LogOut();
  139.  
  140. close(Proxysock);
  141.  
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement