Bossuche9

Untitled

Apr 20th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netdb.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11.  
  12. void * client_service_thread(void*arg);
  13.  
  14.  
  15. int main(int argc, char **argv )
  16. {
  17. // sd is the server socket descriptor. csd is client socket descriptor
  18. int sd, csd, status, on , queueLenghth = 10;
  19. struct addrinfo request, *result, *p;
  20. struct sockaddr_ln clientaddr;
  21. //struct sockaddr_ln clientaddr;
  22. socklen_t addrlen;
  23.  
  24.  
  25.  
  26. request. ai_flags = AI_PASSIVE; // use my IP
  27. request.ai_socktype = SOCK_STREAM; // want TCP/IP connection_based communication.
  28. request.ai_protocol = 0;
  29. request.ai_addrlen = 0;
  30. request.ai_addr = NULL;
  31. request.ai_canonname = NULL;
  32. request.ai_next = NULL;
  33. request.ai_family = AF_INET;// want to use IPV4 32 bit IP addrress
  34.  
  35. // get ready to connect
  36. status = getaddrinfo(0,"4008",&request, &result );
  37. // result now points to addrinfo
  38. if (status != 0) {
  39.  
  40. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
  41. exit(EXIT_FAILURE);
  42. }
  43.  
  44. for (p = result; p != NULL; p = p->ai_next) {
  45.  
  46. sd = socket(result->ai_family, result->ai_socktype, result-> ai_protocol);
  47.  
  48. if (sd == -1) {
  49. perror("socket");
  50. continue;
  51. }
  52.  
  53. on = 1;
  54. // make port reusable by eliminating the adress already in use.
  55. if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,&on,sizeof (on)) != 0) {
  56. perror("setsockopt");
  57. close(sd);
  58. continue;
  59. }
  60. // bind the descriptor to the address
  61. if (bind(sd, p->ai_addr,p->ai_addrlen) == -1) {
  62. close(sd);
  63. perror("server: bind");
  64. continue;
  65. }
  66.  
  67. break;
  68.  
  69. }
  70.  
  71. freeaddrinfo(result);// free because we done using it
  72.  
  73. if (p == NULL) {
  74. fprintf(stderr, "server: faiuled to bind\n");
  75. exit(1);
  76. }
  77.  
  78. if (listen(sd,10) == -1){
  79. perror("listen");
  80. exit(1);
  81. }
  82.  
  83. printf("server: waiting for connections...\n");
  84.  
  85.  
  86. addrlen = sizeof(clientaddr);
  87.  
  88.  
  89. while((csd = accept(sd,(struct sockaddr *)&clientaddr, &addrlen)) !=-1){
  90.  
  91. pthread_t tid;
  92.  
  93. int * csdptr;
  94.  
  95. csdptr = malloc(sizeof(csd));
  96. *csdptr = csd;
  97.  
  98. pthread_create(&tid,NULL, client_service_thread,csdptr);
  99.  
  100. }
  101.  
  102. if (csd){
  103.  
  104. }
  105.  
  106.  
  107. }
  108.  
  109.  
  110. void * client_service_thread(void*arg){
  111. int csd;
  112. csd = *((int*)arg);
  113. free(arg);
  114. pthread_detach(pthread_self());
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. }
Add Comment
Please, Sign In to add comment