Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. ============================= Client ==========================
  2.  
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <netinet/in.h>
  9. #include <resolv.h>
  10. #include <sys/socket.h>
  11. #include <arpa/inet.h>
  12. #include <unistd.h>
  13.  
  14. int main(int argv, char** argc){
  15.  
  16. int host_port= 20;
  17. char* host_name="127.0.0.1";
  18.  
  19. struct sockaddr_in my_addr;
  20.  
  21. char buffer[1024];
  22. int bytecount;
  23. int buffer_len=0;
  24.  
  25. int hsock;
  26. int * p_int;
  27. int err;
  28.  
  29. hsock = socket(AF_INET, SOCK_STREAM, 0);
  30. if(hsock == -1){
  31. printf("Error initializing socket %d\n",errno);
  32. goto FINISH;
  33. }
  34.  
  35. p_int = (int*)malloc(sizeof(int));
  36. *p_int = 1;
  37.  
  38. if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
  39. (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
  40. printf("Error setting options %d\n",errno);
  41. free(p_int);
  42. goto FINISH;
  43. }
  44. free(p_int);
  45.  
  46. my_addr.sin_family = AF_INET ;
  47. my_addr.sin_port = htons(host_port);
  48.  
  49. memset(&(my_addr.sin_zero), 0, 8);
  50. my_addr.sin_addr.s_addr = inet_addr(host_name);
  51.  
  52. if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
  53. if((err = errno) != EINPROGRESS){
  54. fprintf(stderr, "Error connecting socket %d\n", errno);
  55. goto FINISH;
  56. }
  57. }
  58.  
  59.  
  60. buffer_len = 1024;
  61.  
  62. memset(buffer, '\0', buffer_len);
  63.  
  64. printf("Enter some text to send to the server (press enter)\n");
  65. fgets(buffer, 1024, stdin);
  66. buffer[strlen(buffer)-1]='\0';
  67.  
  68. if( (bytecount=send(hsock, buffer, strlen(buffer),0))== -1){
  69. fprintf(stderr, "Error sending data %d\n", errno);
  70. goto FINISH;
  71. }
  72. printf("Sent bytes %d\n", bytecount);
  73.  
  74. if((bytecount = recv(hsock, buffer, buffer_len, 0))== -1){
  75. fprintf(stderr, "Error receiving data %d\n", errno);
  76. goto FINISH;
  77. }
  78. printf("Recieved bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
  79.  
  80. close(hsock);
  81.  
  82. FINISH:
  83. ;
  84. }
  85.  
  86.  
  87. ===================== Server ===============
  88.  
  89. #include <fcntl.h>
  90. #include <string.h>
  91. #include <stdlib.h>
  92. #include <errno.h>
  93. #include <stdio.h>
  94. #include <netinet/in.h>
  95. #include <resolv.h>
  96. #include <sys/socket.h>
  97. #include <arpa/inet.h>
  98. #include <unistd.h>
  99. #include <pthread.h>
  100. #include <time.h>
  101.  
  102. void WriteLogFile(const char* szString)
  103. {
  104. //#IFDEF DEBUG
  105.  
  106. FILE* pFile = fopen("logFile.txt", "a");
  107. fprintf(pFile, "%s\n",szString);
  108. fclose(pFile);
  109. char buff[20];
  110. struct tm *sTm;
  111.  
  112. time_t now = time (0);
  113. sTm = gmtime (&now);
  114.  
  115. strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
  116. printf ("%s %s\n", buff, "");
  117.  
  118. // return 0;
  119. //#ENDIF
  120.  
  121. }
  122.  
  123. void* SocketHandler(void*);
  124.  
  125. int main(int argv, char** argc){
  126.  
  127. int host_port= 20;
  128.  
  129. struct sockaddr_in my_addr;
  130.  
  131. int hsock;
  132. int * p_int ;
  133. int err;
  134.  
  135. socklen_t addr_size = 0;
  136. int* csock;
  137. sockaddr_in sadr;
  138. pthread_t thread_id=0;
  139.  
  140.  
  141. hsock = socket(AF_INET, SOCK_STREAM, 0);
  142. if(hsock == -1){
  143. printf("Error initializing socket %d\n", errno);
  144. goto FINISH;
  145. }
  146.  
  147. p_int = (int*)malloc(sizeof(int));
  148. *p_int = 1;
  149.  
  150. if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
  151. (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
  152. printf("Error setting options %d\n", errno);
  153. free(p_int);
  154. goto FINISH;
  155. }
  156. free(p_int);
  157.  
  158. my_addr.sin_family = AF_INET ;
  159. my_addr.sin_port = htons(host_port);
  160.  
  161. memset(&(my_addr.sin_zero), 0, 8);
  162. my_addr.sin_addr.s_addr = INADDR_ANY ;
  163.  
  164. if( bind( hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
  165. fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",errno);
  166. goto FINISH;
  167. }
  168. if(listen( hsock, 10) == -1 ){
  169. fprintf(stderr, "Error listening %d\n",errno);
  170. goto FINISH;
  171. }
  172.  
  173. addr_size = sizeof(sockaddr_in);
  174.  
  175. while(true){
  176. printf("waiting for a connection\n");
  177. csock = (int*)malloc(sizeof(int));
  178. if((*csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1){
  179. printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr));
  180. WriteLogFile("---------------------\nReceived connection from IP Address: ");
  181. WriteLogFile(inet_ntoa(sadr.sin_addr));
  182. pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
  183. pthread_detach(thread_id);
  184. }
  185. else{
  186. fprintf(stderr, "Error accepting %d\n", errno);
  187. }
  188. }
  189.  
  190. FINISH:
  191. ;
  192. }
  193.  
  194. void* SocketHandler(void* lp){
  195. int *csock = (int*)lp;
  196.  
  197. char buffer[100];
  198. int buffer_len = 100;
  199. int bytecount;
  200.  
  201. memset(buffer, 0, buffer_len);
  202. if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
  203. fprintf(stderr, "Error receiving data %d\n", errno);
  204. goto FINISH;
  205. }
  206. printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
  207. WriteLogFile(buffer);
  208.  
  209. strcat(buffer, " On Server");
  210.  
  211. if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
  212. fprintf(stderr, "Error sending data %d\n", errno);
  213. goto FINISH;
  214. }
  215.  
  216. printf("Sent bytes %d\n", bytecount);
  217.  
  218.  
  219. FINISH:
  220. free(csock);
  221. return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement