Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8.  
  9. char inusername[100];
  10. char inpassword[100];
  11. int pid;
  12. char usernames[50];
  13. char passwords[50];
  14. int Ucomp = -1000;
  15. int Pcomp = -1000;
  16. int counter = 0;
  17. int i = 0;
  18. void parser(char*, FILE*);
  19.  
  20. void error(char *msg)
  21. {
  22. perror(msg);
  23. exit(1);
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28. char input[512];
  29.  
  30. int sockfd, newsockfd, portno, clilen;
  31. char buffer[256];
  32. struct sockaddr_in serv_addr, cli_addr;
  33. int n, r;
  34. if (argc < 2)
  35. {
  36. fprintf(stderr,"ERROR, no port providedn");
  37. exit(1);
  38. }
  39. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  40. if (sockfd < 0)
  41. {
  42. error("ERROR opening socket");
  43. }
  44.  
  45. bzero((char *) &serv_addr, sizeof(serv_addr));
  46. portno = atoi(argv[1]);
  47. serv_addr.sin_family = AF_INET;
  48. serv_addr.sin_addr.s_addr = INADDR_ANY;
  49. serv_addr.sin_port = htons(portno);
  50. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  51. {
  52. error("ERROR on binding");
  53. }
  54. listen(sockfd,5);
  55. clilen = sizeof(cli_addr);
  56. while (1)
  57. {
  58. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  59. puts("Connection accepted");
  60. pid = fork();
  61. if (pid < 0) {
  62. error("ERROR in creating new socket");
  63. }
  64. if (pid == 0) {
  65. close(sockfd);
  66. while (counter <= 3)
  67. {
  68. FILE *pToFile = fopen("pwd.txt", "r");
  69.  
  70. write(newsockfd, "Username:", strlen("Usernamen"));
  71. recv(newsockfd, inusername, 20, 0);
  72.  
  73. write(newsockfd, "Password:", strlen("Passwordn"));
  74. recv(newsockfd, inpassword, 20, 0);
  75.  
  76. parser(input, pToFile);
  77.  
  78. if (Ucomp == 0)
  79. {
  80.  
  81. if (Pcomp == 0)
  82. {
  83. write(newsockfd, "Welcome to chat", strlen("Welcome to chatn"));
  84. close(newsockfd);
  85. exit(1);
  86. }
  87. }
  88. else if (counter < 2)
  89. {
  90.  
  91. write(newsockfd, "InvalidnUsername:", strlen("Invalid"));
  92. memset(&inusername[0], 0, sizeof(inusername));
  93. memset(&inpassword[0], 0, sizeof(inpassword));
  94. counter++;
  95. }
  96. else
  97. {
  98.  
  99. write(newsockfd, "Access Denied", strlen("Access Deniedn"));
  100. exit(1);
  101. close(newsockfd);
  102.  
  103. }
  104.  
  105. fclose(pToFile);
  106. }
  107. }
  108. else {
  109. close(newsockfd);
  110. }
  111. }
  112.  
  113. return 0;
  114. }
  115.  
  116. void parser(char *input, FILE *pToFile)
  117. {
  118. int pCounter = 0;
  119. char * pch;
  120.  
  121. while (fgets(input, 512, pToFile))
  122. {
  123. pch = strtok(input, " ");
  124.  
  125. while (pch != NULL)
  126. {
  127. if (Ucomp == 0 && Pcomp == 0)
  128. {
  129. return;
  130. }
  131.  
  132. if (pCounter == 0)
  133. {
  134. strcpy(usernames, pch);
  135.  
  136. Ucomp = strcmp(inusername, usernames);
  137.  
  138. }
  139. else if (pCounter == 1)
  140. {
  141. strcpy(passwords, pch);
  142.  
  143. Pcomp = strcmp(inpassword, passwords);
  144.  
  145. }
  146. else if (pCounter == 4)
  147. {
  148. pCounter = -1;
  149. }
  150.  
  151.  
  152. pch = strtok(NULL, " ");
  153.  
  154. pCounter++;
  155. }
  156.  
  157. if (Ucomp == 0 && Pcomp == 0)
  158. {
  159. return;
  160. }
  161. }
  162. }
  163.  
  164. #include <stdio.h>
  165. #include <sys/types.h>
  166. #include <sys/socket.h>
  167. #include <netinet/in.h>
  168. #include <netdb.h>
  169. #include <stdlib.h>
  170. #include <string.h>
  171. #include <unistd.h>
  172. #include <fcntl.h>
  173.  
  174. char reply[100], message[2000];
  175. int counter = 1;
  176.  
  177. void error(char *msg)
  178. {
  179. perror(msg);
  180. exit(0);
  181. }
  182.  
  183. int main(int argc, char *argv[])
  184. {
  185. int sockfd, portno, n, r;
  186. char buffer[256];
  187.  
  188. struct sockaddr_in serv_addr;
  189. struct hostent *server;
  190.  
  191. if (argc < 3)
  192. {
  193. fprintf(stderr,"usage %s hostname portn", argv[0]);
  194. exit(0);
  195. }
  196.  
  197. portno = atoi(argv[2]);
  198. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  199.  
  200. if (sockfd < 0)
  201. {
  202. error("ERROR opening socket");
  203. }
  204.  
  205. server = gethostbyname(argv[1]);
  206.  
  207. if (server == NULL)
  208. {
  209. fprintf(stderr,"ERROR, no such hostn");
  210. exit(0);
  211. }
  212.  
  213. bzero((char *) &serv_addr, sizeof(serv_addr));
  214. serv_addr.sin_family = AF_INET;
  215. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  216. serv_addr.sin_port = htons(portno);
  217.  
  218. if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  219. {
  220. error("ERROR connecting");
  221. }
  222.  
  223. while(1)
  224. {
  225. recv(sockfd,reply,2000,0);
  226. puts(reply);
  227.  
  228. if (counter == 3)
  229. {
  230. recv(sockfd, reply, 2000, 0);
  231. puts(reply);
  232. counter++;
  233. }
  234. else if (counter == 6)
  235. {
  236. recv(sockfd, reply, 2000, 0);
  237. puts(reply);
  238. counter++;
  239. }
  240. else if (counter == 9)
  241. {
  242. close(sockfd);
  243. break;
  244. }
  245. counter++;
  246. scanf("%s",message);
  247. send(sockfd,message,strlen(message),0);.
  248. memset(&reply[0], 0, sizeof(reply));
  249. }
  250. return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement