Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <sys/socket.h>
  5. #include <netinet/ip.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. static int bind_server_socket(int fd, int port);
  10.  
  11. /*
  12. * create a server socket bound to port
  13. * and listening.
  14. *
  15. * return positive file descriptor
  16. * or negative value on error
  17. */
  18. int create_server_socket(int port)
  19. {
  20. int fd = -1;
  21.  
  22. if(port < 0 || port > 65535) {
  23. errno = EINVAL;
  24. return -1;
  25. }
  26. fd = socket(AF_INET,SOCK_STREAM,0);
  27. if(fd < 0) return fd;
  28. if(bind_server_socket(fd,port) < 0) return -1;
  29.  
  30. if(listen(fd,10)) return -1;
  31.  
  32. return fd;
  33. }
  34.  
  35. static int bind_server_socket(int fd, int port){
  36.  
  37. struct sockaddr_in addr;
  38. int val = 1;
  39. if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) {
  40. perror("setsockopt");
  41. return -1;
  42. }
  43.  
  44. /* see man page ip(7) */
  45. addr.sin_family = AF_INET;
  46. addr.sin_port = htons(port);
  47. addr.sin_addr.s_addr = INADDR_ANY;
  48. if(bind(fd, (struct sockaddr*) &addr, sizeof(addr))) return -1;
  49.  
  50. #ifdef INFO
  51. printf("simple_tcp_server: bound fd %d to port %d\n",fd,port);
  52. #endif
  53.  
  54. return fd;
  55. }
  56.  
  57. /*
  58. * Serve one client: send a message and close the socket
  59. */
  60. static int do_serve(int fd)
  61. {
  62. int clientfd;
  63.  
  64. if((clientfd = accept(fd, NULL, NULL)) < 0) return -1;
  65. #ifdef INFO
  66. printf("simple_tcp_server: writing msg (len=%lu) to clientfd (%d)\n",len,clientfd);
  67. #endif
  68.  
  69. #ifdef WRITE_LOOP
  70. size_t written = 0;
  71. do {
  72. int res = write(clientfd, msg, len);
  73. if (res < 0) {
  74. perror("write to clientfd");
  75. goto error;
  76. }
  77. #ifdef INFO
  78. printf("simple_tcp_server: write returned %d\n",res);
  79. #endif
  80. written += res;
  81. } while (written < len);
  82. #else
  83. {
  84. while(1==1) {
  85. /*char run = 1;
  86. FILE *fileptr;
  87. char *buffer;
  88. int filelen;
  89. int curr = 1;
  90. char *url = ("/Users/axeldelbom/Downloads/samplevideo/");
  91. while(run) {
  92. char *fileUrl = malloc(strlen(url) + 13);
  93. char *num = malloc(4);
  94.  
  95. sprintf (num, "test%04d.jpg", curr);
  96. strcpy(fileUrl,url);
  97. strcat(fileUrl,num);
  98.  
  99.  
  100. fileptr = fopen(fileUrl,
  101. "rb"); // Open the file in binary mode
  102. fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
  103. filelen = ftell(fileptr); // Get the current byte offset in the file
  104. rewind(fileptr); // Jump back to the beginning of the file
  105. uint32_t filelenWeb = htonl(filelen);
  106.  
  107.  
  108. int written = write(clientfd, &(filelenWeb), 4);
  109.  
  110. buffer = (char *) malloc(1024 * sizeof(char) +1 ); // Enough memory for file + \0
  111. int total = 0;
  112. int sent = 0;
  113. int target = 1024;
  114. while(1)
  115. {
  116. total = total + fread(buffer,1,1024,fileptr);
  117. sent += write(clientfd, buffer, target);
  118. if(filelen - sent < 1024){
  119. target = filelen - sent;
  120. }
  121. if(total >= filelen){
  122. break;}
  123. } // Read in the entire file
  124. fclose(fileptr); // Close the file
  125. curr++;
  126. if(curr > 711) curr = 1;
  127. }*/
  128. char *text = "Hello\n";
  129. write(clientfd, text, strlen(text));
  130. sleep(2);
  131. }
  132. }
  133. #endif
  134.  
  135. error:
  136. printf("simple_tcp_server: closing clientfd (%d)\n",clientfd);
  137. return close(clientfd);
  138. }
  139.  
  140. int main()
  141. {
  142. int fd = create_server_socket(8888);
  143.  
  144. if(fd < 0){
  145. perror("create_server_socket");
  146. return 1;
  147. }
  148. while(1) {
  149. do_serve(fd);
  150. }
  151. printf("simple_tcp_server: closing socket: %d\n", fd);
  152. close(fd);
  153.  
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement