Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

61 rader
1.5 KiB

  1. #include <arpa/inet.h>
  2. #include <netdb.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #define BACKLOG 10
  9. int main(int argc, char **argv) {
  10. int status;
  11. struct addrinfo hints;
  12. struct addrinfo *servinfo;
  13. struct sockaddr_storage their_addr;
  14. socklen_t addr_size;
  15. memset(&hints, 0, sizeof hints);
  16. hints.ai_family = AF_UNSPEC;
  17. hints.ai_socktype = SOCK_STREAM;
  18. hints.ai_flags = AI_PASSIVE;
  19. if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) {
  20. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  21. exit(1);
  22. }
  23. int s;
  24. if ((s = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) {
  25. fprintf(stderr, "socket error: %d\n", status);
  26. exit(1);
  27. }
  28. printf("%d\n", s);
  29. int yes=1;
  30. /* when port is already bound to this program */
  31. if ((status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)) == -1) {
  32. fprintf(stderr, "setsockopt error: %d\n", status);
  33. exit(1);
  34. }
  35. if((status = bind(s, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) {
  36. fprintf(stderr, "bind error: %d\n", status);
  37. exit(1);
  38. }
  39. freeaddrinfo(servinfo);
  40. listen(s, BACKLOG);
  41. addr_size = sizeof their_addr;
  42. int new_fd = accept(s, (struct sockaddr *)&their_addr, &addr_size);
  43. printf("%d\n", new_fd);
  44. char * buf = malloc (100000000);
  45. if ()
  46. int bytes = recv(new_fd, buf, 100000000, 0);
  47. printf("bytes received %d\n", bytes);
  48. }