Guest User

Untitled

a guest
Jan 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. struct sockaddr_storage their_addr;
  2. socklen_t addr_size;
  3. struct addrinfo hints, *servinfo;
  4. int sockfd, new_fd ,bindfd, listenfd,connectfd;
  5. char recvmessage[500];
  6. int len, bytes_sent,bytes_received;
  7. char MYPORT[5] = "8080";
  8.  
  9. WSADATA wsaData;
  10. if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
  11. fprintf(stderr, "WSAStartup failed.\n");
  12. exit(1);
  13. }
  14.  
  15. memset(&hints, 0, sizeof hints);
  16. hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
  17. hints.ai_socktype = SOCK_STREAM; //TCP
  18. hints.ai_flags = AI_PASSIVE; // fill in my IP for me
  19.  
  20.  
  21. //Info
  22. getaddrinfo(NULL,MYPORT, &hints, &servinfo);
  23.  
  24. //Create a socket
  25. sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
  26. if(sockfd > 0)
  27. printf("\n\Socket(%i) OK\n\n",sockfd);
  28. else
  29. wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
  30.  
  31. //Bind
  32. bindfd = bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
  33. if(bindfd == 0)
  34. printf("Bind OK\n\n",bindfd);
  35. else
  36. wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
  37.  
  38. //Listen
  39. listenfd = listen(sockfd, 10);
  40. if(listenfd == 0)
  41. printf("Listening on port %s...\n\n",MYPORT);
  42. else
  43. wprintf(L"### Listen failed with error: %ld\n\n", WSAGetLastError());
  44.  
  45. //Accept
  46. addr_size = sizeof their_addr;
  47. new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
  48. if (new_fd > 0)
  49. printf("Accepting %i bytes\n\n",new_fd);
  50. else if (new_fd == -1)
  51. wprintf(L"### Accept failed with error: %ld\n\n", WSAGetLastError());
Add Comment
Please, Sign In to add comment