Guest User

Untitled

a guest
May 27th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. C sockets - Blocked on select
  2. void main(int argc, char **argv) {
  3.  
  4. int s1, s2, n, server_port, sc1, sc2, rv, rc, left_peer_port;
  5. int peer_port;
  6. fd_set writefds, readfds;
  7. struct timeval tv;
  8. struct hostent *server_info, *child_info, *left_peer_info;
  9. int start_flag = 0;
  10.  
  11. struct sockaddr_in server, peer, incoming;
  12.  
  13. char host_child[64];
  14. char *left_host = malloc(1);
  15. char *right_host = malloc(1);
  16. char buf1[256];
  17. char buf2[256];
  18.  
  19. server_port = atoi(argv[2]);
  20.  
  21. //speak to peer using this
  22. s2 = socket(AF_INET, SOCK_STREAM, 0);
  23. if (s2 < 0) {
  24. perror("socket:");
  25. exit(s2);
  26. }
  27.  
  28. peer_port = server_port + 1;
  29.  
  30. gethostname(host_child, sizeof host_child);
  31.  
  32. child_info = gethostbyname(host_child);
  33.  
  34. if (child_info == NULL) {
  35. fprintf(stderr, "%s: host not found (%s)n", argv[0], host_child);
  36. exit(1);
  37. }
  38.  
  39.  
  40. peer.sin_family = AF_INET;
  41. memcpy(&peer.sin_addr, child_info->h_addr_list[0], child_info->h_length);
  42. int changeport = 0;
  43. do {
  44. peer.sin_port = htons(peer_port);
  45. rc = bind(s2, (struct sockaddr *) &peer, sizeof(peer));
  46.  
  47. if (rc < 0) {
  48. //perror("bind:");
  49. peer_port++;
  50. changeport = 1;
  51. //exit(rc);
  52.  
  53. } else {
  54. changeport = 0;
  55. }
  56.  
  57. } while (changeport == 1);
  58.  
  59. if (listen(s2, 100) == -1) {
  60. perror("listen");
  61. exit(3);
  62. }
  63.  
  64.  
  65.  
  66. //Now talk to server
  67.  
  68. server_info = gethostbyname(argv[1]);
  69.  
  70. if (server_info == NULL) {
  71. fprintf(stderr, "%s: host not foundn", argv[0]);
  72. exit(1);
  73. }
  74.  
  75.  
  76. // pretend we've connected both to a server at this point
  77. //speak to server using this
  78. s1 = socket(AF_INET, SOCK_STREAM, 0);
  79. if (s1 < 0) {
  80. perror("socket:");
  81. exit(s1);
  82. }
  83.  
  84.  
  85. server.sin_family = AF_INET;
  86. server.sin_port = htons(server_port);
  87. memcpy(&server.sin_addr, server_info->h_addr_list[0], server_info->h_length);
  88.  
  89. //To talk to the server
  90.  
  91. sc1 = connect(s1, (struct sockaddr *) &server, sizeof(server));
  92. if (sc1 < 0) {
  93. perror("connect:");
  94. exit(sc1);
  95. }
  96.  
  97. int send_len;
  98. char *str = malloc(1);
  99. sprintf(str, "%d", peer_port);
  100. printf("nport-here=%sn", str);
  101.  
  102.  
  103. send_len = send(s1, str, strlen(str), 0);
  104. if (send_len != strlen(str)) {
  105. perror("send");
  106. exit(1);
  107. }
  108.  
  109. int recv_len;
  110. char buf[100];
  111. int ref = 0;
  112. int recv_stage = 0;
  113. int start_id;
  114.  
  115.  
  116. recv_len = recv(s1, buf, 34, 0);
  117. if (recv_len < 0) {
  118. perror("recv");
  119. exit(1);
  120. }
  121. buf[recv_len] = '';
  122. char *temp_port;
  123.  
  124. if (!strcmp("close", buf))
  125. printf("%s", buf);
  126. //break;
  127. else {
  128. char *temp_buffer = malloc(1);
  129. char *id = malloc(100);
  130. char *pp = malloc(1);
  131. strcpy(temp_buffer, buf);
  132.  
  133. char *search = ":";
  134. temp_port = strtok(temp_buffer, search);
  135. strcpy(buf, temp_port);
  136. printf("temp_name%s", temp_port);
  137.  
  138. temp_port = strtok(NULL, search);
  139. strcpy(pp, temp_port);
  140. printf("temp_port%s", temp_port);
  141. temp_port = strtok(NULL, search);
  142. strcpy(id, temp_port);
  143. printf("id%s", temp_port);
  144.  
  145. strcpy(temp_port, pp);
  146. printf("nbuf=%sn", buf);
  147. printf("nport=%sn", temp_port);
  148. printf("nid=%sn", id);
  149. start_id = atoi(id);
  150. }
  151.  
  152. //To send packet to its neighbour
  153. left_peer_info = gethostbyname(buf);
  154. printf("nleft host=%sn", buf);
  155. if (left_peer_info == NULL) {
  156. fprintf(stderr, "%s: host not foundn", left_host);
  157. exit(1);
  158. }
  159. left_peer_port = atoi(temp_port);
  160.  
  161.  
  162. int neighbour_socket;
  163. struct hostent *neighbour_info;
  164. struct sockaddr_in neighbour;
  165. neighbour_socket = socket(AF_INET, SOCK_STREAM, 0);
  166. if (neighbour_socket < 0) {
  167. perror("socket:");
  168. exit(neighbour_socket);
  169. }
  170.  
  171. neighbour_info = left_peer_info;
  172.  
  173.  
  174. neighbour.sin_family = AF_INET;
  175. neighbour.sin_port = htons(left_peer_port);
  176. memcpy(&neighbour.sin_addr, neighbour_info->h_addr_list[0], neighbour_info->h_length);
  177.  
  178. printf("nconnected to port %dn", left_peer_port);
  179. //To talk to the neighbour
  180. printf("ncomes heren");
  181.  
  182.  
  183. //Listen on this socket connection for potato
  184.  
  185. int send_peer_len;
  186. int nfds;
  187.  
  188. nfds = MAX(MAX(neighbour_socket, s2), s1);
  189.  
  190. // clear the set ahead of time
  191. FD_ZERO(&writefds);
  192.  
  193. // add our descriptors to the set
  194. FD_SET(neighbour_socket, &writefds);
  195. FD_SET(s1, &writefds);
  196. FD_SET(s2, &writefds);
  197.  
  198. //FD_SET(s2, &writefds);
  199.  
  200.  
  201. FD_ZERO(&readfds);
  202. FD_SET(neighbour_socket, &readfds);
  203. FD_SET(s1, &readfds);
  204. FD_SET(s2, &readfds);
  205.  
  206. //select()
  207.  
  208. // since we got s2 second, it's the "greater", so we use that for
  209. // the n param in select()
  210. //n = s1 + 1;
  211.  
  212. // wait until either socket has data ready to be recv()d (timeout 10.5 secs)
  213. tv.tv_sec = 10;
  214. tv.tv_usec = 500000;
  215.  
  216. int fds[3];
  217. fds[0] = s1;
  218. fds[1] = s2;
  219. fds[2] = neighbour_socket;
  220. int p = 0;
  221. int p_flag = 0;
  222.  
  223. while (1) {
  224. printf("n nfds = %d , p = %d n", nfds, p);
  225. char buf_msg[64];
  226.  
  227.  
  228. //This is where the error occurs //
  229.  
  230. rv = select(nfds, &readfds, NULL, NULL, 0);
  231.  
  232. //This is where the error occurs //
  233.  
  234. if (rv == -1) {
  235. perror("select"); // error occurred in select()
  236. } else if (rv == 0) {
  237. printf("Timeout occurred! No data after 10.5 seconds.n");
  238. } else {
  239. // one or both of the descriptors have data
  240. //reading message from server
  241. int select_fd;
  242. for (select_fd = 0; select_fd <= nfds; select_fd++) {
  243.  
  244. if (FD_ISSET(select_fd, &readfds) != 0) {
  245. if (select_fd == s1) {
  246.  
  247. recv_len = 0;
  248. recv_len = recv(s1, buf_msg, 34, 0);
  249. if (recv_len < 0) {
  250. perror("recv");
  251. exit(1);
  252. }
  253. buf_msg[recv_len] = '';
  254. printf("nreceived from server = %sn", buf_msg);
  255. //send to neighbour
  256.  
  257. int sc3;
  258. sc3 = connect(neighbour_socket, (struct sockaddr *) &neighbour, sizeof(neighbour));
  259. if (sc3 < 0) {
  260. perror("connect:");
  261. exit(sc3);
  262.  
  263. }
  264.  
  265. str = malloc(1);
  266. strcpy(str, buf_msg);
  267. send_len = send(neighbour_socket, str, strlen(str), 0);
  268. printf("n send - len - s1 - %dn", send_len);
  269. if (send_len != strlen(str)) {
  270. perror("send");
  271. exit(1);
  272. }
  273. start_flag = 1;
  274. //FD_CLR(s1, &readfds);
  275.  
  276. printf("ncrossed servern");
  277.  
  278. } else if (select_fd == s2) {
  279.  
  280. int list_len = sizeof incoming;
  281. printf("ninside clientn");
  282. printf("nWaiting for accept in S2n");
  283. if (p_flag == 0) {
  284. p_flag = 1;
  285. p = accept(s2, (struct sockaddr *) &incoming, &list_len);
  286. printf("nConnection accepted in S2n");
  287. if (p < 0) {
  288. perror("bind:");
  289. exit(rc);
  290. }
  291. }
  292. nfds = MAX(nfds, p);
  293. recv_len = 0;
  294. buf_msg[recv_len] = '';
  295. recv_len = recv(p, buf_msg, 34, 0);
  296. if (recv_len < 0) {
  297. perror("recv");
  298. exit(1);
  299. }
  300. buf_msg[recv_len] = '';
  301. printf("nreceived from client = %sn", buf_msg);
  302. //send to neighbour
  303.  
  304. //if(start_id!=1){
  305. int sc3;
  306. sc3 = connect(neighbour_socket, (struct sockaddr *) &neighbour, sizeof(neighbour));
  307. if (sc3 < 0) {
  308. perror("connect:");
  309. //exit(sc3);
  310. }
  311. //}
  312.  
  313. str = malloc(1);
  314. strcpy(str, buf_msg);
  315. send_len = send(neighbour_socket, str, strlen(str), 0);
  316. printf("n send - len - s2 - %dn", send_len);
  317. if (send_len != strlen(str)) {
  318. perror("send");
  319. exit(1);
  320. }
  321.  
  322. } else if (select_fd == neighbour_socket) {
  323.  
  324. printf("ncomes inn");
  325.  
  326. } else if (select_fd == p && p != 0) {
  327.  
  328. int list_len = sizeof incoming;
  329. printf("ninside pn");
  330. recv_len = 0;
  331. buf_msg[recv_len] = '';
  332. printf("nwaiting at recv in Pn");
  333.  
  334. recv_len = recv(p, buf_msg, 34, 0);
  335.  
  336. printf("ncrossed at recv in Pn");
  337. if (recv_len < 0) {
  338. perror("recv");
  339. exit(1);
  340. }
  341. buf_msg[recv_len] = '';
  342. printf("nreceived from client = %sn", buf_msg);
  343. //send to neighbour
  344. str = malloc(1);
  345. strcpy(str, buf_msg);
  346. send_len = send(neighbour_socket, str, strlen(str), 0);
  347. printf("n send - len - neighbour - %dn", send_len);
  348. if (send_len != strlen(str)) {
  349. perror("send");
  350. exit(1);
  351. }
  352. }
  353. }
  354. }
  355.  
  356. FD_ZERO(&readfds);
  357. //FD_SET(neighbour_socket,&readfds);
  358. FD_SET(s1, &readfds);
  359. FD_SET(neighbour_socket, &readfds);
  360.  
  361. if (p_flag == 1) {
  362. printf("nsetting Pn");
  363. FD_SET(p, &readfds);
  364. FD_SET(s2, &readfds);
  365.  
  366. p_flag = 0;
  367. } else {
  368. printf("nNot setting Pn");
  369. FD_SET(s2, &readfds);
  370. }
  371. }
  372. }
  373. close(s1);
  374. close(s2);
  375. }
  376.  
  377. int send_len;
  378. char *str=malloc(1);
  379. sprintf(str,"%d",peer_port);
  380. printf("nport-here=%sn",str);
  381.  
  382. buf[recv_len] = '';
  383. char *temp_port;
  384. //printf("n-%sn",buf);
  385. if ( !strcmp("close", buf) )
  386. printf("%s",buf);
  387. //break;
  388. else{
  389. char *temp_buffer=malloc(1);
  390. char *id=malloc(100);
  391. char *pp=malloc(1);
  392. strcpy(temp_buffer,buf);
Advertisement
Add Comment
Please, Sign In to add comment