Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. //server.c
  2.  
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <pthread.h>
  10. #include <signal.h>
  11. #include <string.h>
  12.  
  13. #define MAXCON 100
  14.  
  15. int keepAlive = 1;
  16. int rs;
  17. int thrcount=0;
  18.  
  19. pthread_t* thr[MAXCON];
  20. pthread_t daemonthr;
  21.  
  22.  
  23. struct users {
  24. char username[1024];
  25. char password[1024];
  26. int status;
  27. };
  28.  
  29. typedef struct users users;
  30.  
  31. users uservect[1024];
  32. int usercnt;
  33.  
  34. struct thrarg {
  35. int sock;
  36. int thrindex;
  37. struct sockaddr_in caddr;
  38. int caddr_len;
  39. };
  40.  
  41. typedef struct thrarg thrarg;
  42.  
  43. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  44. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  45.  
  46. int findFreeThread() {
  47. int i;
  48. for(i=0;i<MAXCON;i++) {
  49. if(thr[i] == NULL) {
  50. return i;
  51. }
  52. }
  53.  
  54. return -1;
  55. }
  56.  
  57. int logcount() {
  58. int i;
  59. int k=0;
  60. for(i=0;i<MAXCON;i++) {
  61. if(uservect[i].status>0) k++;
  62. }
  63. return k;
  64. }
  65.  
  66. int response(char* username, char* password) {
  67. printf("%s %s \n",username,password);
  68. int i;
  69. for(i=0;i<usercnt;i++) {
  70. if(strcmp(username,uservect[i].username) == 0) {
  71. if(strcmp(password,uservect[i].password) == 0 ) {
  72. if(uservect[i].status == 0) {
  73. uservect[i].status = 1;
  74. return logcount();
  75. }
  76. return -2;
  77. }
  78. return -1;
  79. }
  80.  
  81. }
  82. return -1;
  83. }
  84.  
  85.  
  86. void logout(char* username) {
  87. int i;
  88. for(i=0;i<usercnt;i++) {
  89. if(strcmp(username,uservect[i].username) == 0) {
  90. uservect[i].status = 0;
  91. }
  92. }
  93. }
  94.  
  95.  
  96.  
  97. void serve(thrarg* arg) {
  98. pthread_mutex_lock(&mtx);
  99. printf("Threadcount:%d\n",thrcount);
  100. thrcount++;
  101. if(thrcount % 3 == 0) {
  102. pthread_cond_signal(&cond);
  103. }
  104. pthread_mutex_unlock(&mtx);
  105.  
  106.  
  107. char buf[1024];
  108. int k = recv(arg->sock,buf,1024,0);
  109.  
  110. if(k<=0) {
  111. perror("Could not receive");
  112. }
  113.  
  114.  
  115. buf[k] = 0;
  116.  
  117. char username[1024];
  118. strcpy(username,buf);
  119.  
  120. k = recv(arg->sock,buf,1024,0);
  121. if(k<=0) {
  122. perror("Could not receive password");
  123. }
  124.  
  125. buf[k] = 0;
  126.  
  127. char password[1024];
  128. strcpy(password,buf);
  129.  
  130. pthread_mutex_lock(&mtx);
  131. printf("Username:%s\nPassword:%s\n",username,password);
  132. pthread_mutex_unlock(&mtx);
  133.  
  134. int resp = response(username,password);
  135.  
  136. pthread_mutex_lock(&mtx);
  137. printf("%d\n",resp);
  138. pthread_mutex_unlock(&mtx);
  139.  
  140. resp = htonl(resp);
  141. k = send(arg->sock,&resp,sizeof(int),0);
  142. if(k<=0) {
  143. perror("Could not send");
  144. }
  145.  
  146. pthread_mutex_lock(&mtx);
  147. if(resp>0) {
  148. //logout(username);
  149. }
  150.  
  151. thrcount--;
  152. free(thr[arg->thrindex]);
  153. thr[arg->thrindex] = NULL;
  154. free(arg);
  155. pthread_mutex_unlock(&mtx);
  156. }
  157.  
  158.  
  159. void handler() {
  160. keepAlive = 0;
  161.  
  162. if(close(rs)<0) {
  163. perror("Could not close socket");
  164. return;
  165. }
  166.  
  167.  
  168. int i;
  169. for(i=0;i<MAXCON;i++) {
  170. pthread_t* p = thr[i];
  171. if(p) {
  172. pthread_join(*p,NULL);
  173. }
  174. }
  175.  
  176. pthread_cond_signal(&cond);
  177. pthread_join(daemonthr,NULL);
  178. return;
  179. }
  180.  
  181.  
  182. void readUsers() {
  183. FILE* fd = fopen("users.txt","r");
  184. if(fd==0) {
  185. printf("Could not open file!\n");
  186. exit(3);
  187. }
  188.  
  189. int i=0;
  190. while( !feof(fd) ) {
  191. fscanf(fd,"%s %s",uservect[i].username, uservect[i].password);
  192. //printf("%s %s\n",uservect[i].username,uservect[i].password);
  193. i++;
  194. }
  195. usercnt = i;
  196. }
  197.  
  198.  
  199. void daemonf() {
  200.  
  201. while (keepAlive) {
  202. pthread_mutex_lock(&mtx);
  203. pthread_cond_wait(&cond,&mtx);
  204. if(thrcount % 3 == 0) {
  205. printf("Connection is multiple of 3 : %d \n",thrcount);
  206. }
  207.  
  208. pthread_mutex_unlock(&mtx);
  209. }
  210. }
  211.  
  212.  
  213. int main() {
  214.  
  215. pthread_create(&daemonthr,NULL,(void * )daemonf,NULL);
  216.  
  217. readUsers();
  218.  
  219. rs = socket(AF_INET,SOCK_STREAM,0);
  220. if(rs < 0) {
  221. perror("Could not create rendezvous socket!");
  222. }
  223.  
  224. struct sockaddr_in addr,caddr;
  225. addr.sin_family = AF_INET;
  226. addr.sin_port = htons(1754);
  227. addr.sin_addr.s_addr = INADDR_ANY;
  228.  
  229. if(bind(rs,(struct sockaddr*) &addr,sizeof(addr))<0) {
  230. perror("Could not bind server");
  231. }
  232.  
  233. if(listen(rs,5) < 0 ) {
  234. perror("Could not listen");
  235. }
  236.  
  237. signal(SIGINT,handler);
  238.  
  239. int sock;
  240. unsigned int len = sizeof(caddr);
  241. while(keepAlive) {
  242. sock = accept(rs,(struct sockaddr*) &caddr,&len);
  243.  
  244. if((sock<0)&&keepAlive) {
  245. perror("Could not accept connection!");
  246. }
  247.  
  248. int k = findFreeThread();
  249. if(k<0) {
  250. close(sock);
  251. continue;
  252. }
  253.  
  254. if(len&&keepAlive) {
  255. printf("Incomming connection from %s on port %d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
  256. }
  257.  
  258. thrarg *arg = malloc(sizeof(thrarg));
  259. arg->sock = sock;
  260. arg->thrindex = k;
  261. arg->caddr = caddr;
  262. arg->caddr_len = len;
  263.  
  264. thr[k] = malloc(sizeof(pthread_t));
  265. pthread_create(thr[k],NULL,(void*) serve,arg);
  266.  
  267.  
  268. }
  269.  
  270. //join remaining threads
  271. /*
  272. int i;
  273. for(i=0;i<MAXCON;i++) {
  274. pthread_t* p = thr[i];
  275. if(p) {
  276. pthread_join(*p,NULL);
  277. }
  278. }*/
  279.  
  280. return 0;
  281. }
  282.  
  283.  
  284.  
  285. //client.c
  286.  
  287.  
  288. #include <unistd.h>
  289. #include <stdio.h>
  290. #include <stdlib.h>
  291. #include <sys/socket.h>
  292. #include <netinet/in.h>
  293. #include <arpa/inet.h>
  294. #include <pthread.h>
  295. #include <signal.h>
  296. #include <string.h>
  297.  
  298.  
  299. int main(int argc,char** argv) {
  300.  
  301. if(argc<3) {
  302. printf("Give the username and password as a parameter!\n");
  303. return 1;
  304. }
  305.  
  306. char username[1024];
  307. strcpy(username,argv[1]);
  308.  
  309. char password[1024];
  310. strcpy(password,argv[2]);
  311.  
  312. int sock = socket(AF_INET,SOCK_STREAM,0);
  313. if(sock<0) {
  314. perror("Could not create socket");
  315. }
  316.  
  317. struct sockaddr_in addr;
  318. addr.sin_family = AF_INET;
  319. addr.sin_port = htons(1754);
  320. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  321.  
  322. if( connect(sock,(struct sockaddr*) &addr,sizeof(addr) ) < 0 ) {
  323. perror("Could not connect to server");
  324. }
  325.  
  326. int k = send(sock,username,strlen(username),0);
  327. if(k<=0) {
  328. perror("Could not send username");
  329. }
  330.  
  331. sleep(1);
  332.  
  333. k = send(sock,password,strlen(password),0);
  334. if(k<=0) {
  335. perror("Could not send password");
  336. }
  337.  
  338. int resp;
  339. k = recv(sock,&resp,sizeof(int),0);
  340. if(k<=0) {
  341. perror("Could not receive response");
  342. }
  343.  
  344. resp = ntohl(resp);
  345. printf("Response:%d\n",resp);
  346.  
  347. close(sock);
  348.  
  349. return 0;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement