Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0501
  2.  
  3. #include <iostream>
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int s;
  12. int status;
  13. struct addrinfo hints, *p;
  14. struct addrinfo *servinfo; // will point to the results
  15. WORD wVersionRequested;
  16. WSADATA wsaData;
  17. char ipstr[INET6_ADDRSTRLEN];
  18.  
  19. /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
  20. wVersionRequested = MAKEWORD(2, 0);
  21.  
  22. status = WSAStartup(wVersionRequested, &wsaData);
  23. if (status != 0) {
  24. /* Tell the user that we could not find a usable */
  25. /* Winsock DLL. */
  26. printf("WSAStartup failed with error: %d\n", status);
  27. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  28. return 1;
  29. }
  30.  
  31.  
  32. memset(&hints, 0, sizeof hints); // make sure the struct is empty
  33. hints.ai_family = AF_INET; // don't care IPv4 or IPv6
  34. hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
  35.  
  36. // get ready to connect
  37. if ((status = getaddrinfo("manuelfrw01.dyndns.org", "8000", &hints, &servinfo)) != 0) {
  38. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  39. WSACleanup();
  40. exit(1);
  41. }
  42.  
  43. s=socket(AF_INET,SOCK_STREAM,0);
  44. if (status == INVALID_SOCKET) {
  45. cout << "socket: Scoket error Last Error:" << WSAGetLastError() << endl;
  46. }
  47.  
  48.  
  49. status = connect(s, servinfo->ai_addr, servinfo->ai_addrlen);
  50. if (status == SOCKET_ERROR) {
  51. cout << "socket: Scoket error Last Error:" << WSAGetLastError() << endl;
  52. }
  53.  
  54.  
  55. char buffer[10];
  56. strcpy(buffer, "Hallo");
  57.  
  58.  
  59. while(1) {
  60. status = send(s, buffer, sizeof(buffer), 0);
  61. if (status == SOCKET_ERROR) {
  62. cout << "send Error: " << WSAGetLastError() << endl;
  63. } else {
  64. cout << "Bytes Sent: " << status << endl;
  65. }
  66. Sleep(10);
  67. }
  68.  
  69. cout << "done" << endl;
  70.  
  71. WSACleanup();
  72.  
  73. // servinfo now points to a linked list of 1 or more struct addrinfos
  74.  
  75. // etc.
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement