Advertisement
captainIBM

network.c

Jan 17th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #if defined (WIN32)
  2. #include <winsock2.h>
  3. #include <pthread.h>
  4. typedef int socklen_t;
  5. #elif defined (linux)
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <unistd.h>
  11. #define INVALID_SOCKET -1
  12. #define SOCKET_ERROR -1
  13. #define closesocket(s) close(s)
  14. typedef int SOCKET;
  15. typedef struct sockaddr_in SOCKADDR_IN;
  16. typedef struct sockaddr SOCKADDR;
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include "network.h"
  23. #define PORT 23
  24.  
  25.  
  26. int sendData(int csock, Question question){
  27. int error;
  28. error = send(csock, &question, sizeof(question), 0);
  29. if(error == SOCKET_ERROR){
  30. return 0;
  31. }
  32. return 1;
  33. }
  34.  
  35.  
  36. char receiveData(int csock){
  37. char bufferRecv[50];
  38. int error;
  39. error = recv(csock, bufferRecv, sizeof(bufferRecv)-1, 0);
  40. if(error != SOCKET_ERROR){
  41. bufferRecv[error] = '\0';
  42. printf("client dit: %s \n", bufferRecv);
  43. return bufferRecv;
  44. }else{
  45. return '0';
  46. }
  47. }
  48.  
  49. int init_connection(){
  50. WSADATA WSAData;
  51. WSAStartup(MAKEWORD(2,0), &WSAData);
  52. SOCKADDR_IN server, client;
  53. int clientSock, c;
  54. SOCKET socket_desc;
  55. socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  56. if (socket_desc == -1)
  57. {
  58. printf("Could not create socket");
  59. }
  60. puts("Socket created");
  61.  
  62. //Prepare the sockaddr_in structure
  63. server.sin_family = AF_INET;
  64. server.sin_addr.s_addr = INADDR_ANY;
  65. server.sin_port = htons( 8888 );
  66.  
  67. //Bind
  68. if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
  69. {
  70. //print the error message
  71. perror("bind failed. Error");
  72. return 1;
  73. }
  74. puts("bind done");
  75.  
  76. //Listen
  77. listen(socket_desc , 3);
  78.  
  79. //Accept and incoming connection
  80. puts("Waiting for incoming connections...");
  81. c = sizeof(struct sockaddr_in);
  82.  
  83.  
  84. //Accept and incoming connection
  85. puts("Waiting for incoming connections...");
  86. c = sizeof(struct sockaddr_in);
  87. pthread_t thread_id;
  88.  
  89. while( (clientSock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
  90. {
  91. puts("Connection accepted");
  92.  
  93. if( pthread_create( &thread_id , NULL , connectionHandler , (void*) &clientSock) < 0)
  94. {
  95. perror("could not create thread");
  96. return 1;
  97. }
  98.  
  99. //Now join the thread , so that we dont terminate before the thread
  100. //pthread_join( thread_id , NULL);
  101. puts("Handler assigned");
  102. printf("Un client se connecte avec la socket %d de %s:%d\n", clientSock, inet_ntoa(server.sin_addr), htons(server.sin_port));
  103. }
  104.  
  105. if (clientSock < 0)
  106. {
  107. perror("accept failed");
  108. return 1;
  109. }
  110.  
  111. return 0;
  112. }
  113.  
  114.  
  115.  
  116. void *connectionHandler(void *socket_desc)
  117. {
  118. //Get the socket descriptor
  119. int sock = *(int*)socket_desc;
  120. int read_size;
  121. int numberQ; /**< rajouter la commande sql de comptage des question */
  122. char *message , client_message[2000];
  123. char bufferSend[50];
  124.  
  125. Question question;
  126.  
  127. numberQ = question.numberQ;
  128. strcpy(question.email, "hubertgauthier5@gmail.com");
  129. question.id_question = malloc(sizeof(int)* numberQ);
  130.  
  131. sendData(sock, question);
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement