Advertisement
Adnana_Ciongic

Socket()

Feb 20th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<sys/socket.h>
  3. #include<netinet/in.h>
  4. #include<unistd.h>
  5. #include<fcntl.h>
  6. #include<arpa/inet.h>
  7. #include<string.h>
  8. #include<pthread.h>
  9. #include <dirent.h>
  10.  
  11. void *connectionHandler(void *args)
  12. {
  13. int connectionSocket = *((int*) args);
  14. char message[2048] = "Acesta este un mesaj";
  15. printf("This is in function!\n");
  16.  
  17. send(connectionSocket, message, strlen(message), 0);
  18. }
  19.  
  20. void directoryContent()
  21. {
  22. DIR* dir = opendir(".");
  23. struct dirent* dp;
  24. char* file_name;
  25.  
  26.  
  27. if(dir == NULL)
  28. {
  29. printf("Can't open");
  30. }
  31.  
  32. while ((dp=readdir(dir)) != NULL)
  33. {
  34. printf("The files are: %s\n", dp->d_name);
  35. //if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
  36. //{
  37. // do nothing (straight logic)
  38. // }
  39. //else
  40. //{
  41. // file_name = dp->d_name; // use it
  42. // printf("file_name: \"%s\"\n",file_name);
  43. //}
  44. }
  45.  
  46. closedir(dir);
  47.  
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53.  
  54. //def socket
  55. // bind socket
  56. // listen on socket
  57. directoryContent();
  58. exit(-1);
  59.  
  60. struct sockaddr_in serverAddress;
  61. struct sockaddr_storage clientAddress;
  62. socklen_t addr_len;
  63. pthread_t thandle;
  64. //char message[128] = "Acesta este un mesaj";
  65. int serverSocket = socket(PF_INET, SOCK_STREAM, 0);
  66.  
  67. serverAddress.sin_family = AF_INET;
  68. serverAddress.sin_port = htons(12345);
  69. serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); //local host
  70.  
  71. bind(serverSocket, &serverAddress, sizeof(serverAddress));
  72.  
  73. printf("Before\n");
  74. listen(serverSocket, 100);
  75. printf("After\n");
  76.  
  77.  
  78. //send(serverSocket, message, strlen(message), 0);
  79.  
  80. while(1)
  81. {
  82. int clientSocket = accept(serverSocket, &clientAddress, &addr_len);
  83. pthread_create(&thandle, NULL, connectionHandler, &clientSocket);
  84.  
  85. usleep(50000);
  86. }
  87.  
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement