Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #ifndef WIN32_LEAN_AND_MEAN
  2. #define WIN32_LEAN_AND_MEAN
  3. #endif
  4.  
  5. #include "stdafx.h"
  6. #include <windows.h>
  7. #include <iostream>
  8. #include <string>
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #include <stdio.h>
  12.  
  13. #pragma comment(lib, "Ws2_32.lib")
  14.  
  15. using namespace std;
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. int a = 0;
  20. while (a == 0) {
  21. WSADATA wsaData;
  22.  
  23. int iResult;
  24.  
  25. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  26. if (iResult != 0) {
  27. printf("WSAStartup failed: %d\n", iResult);
  28. }
  29.  
  30. struct addrinfo *result = NULL,
  31. *ptr = NULL,
  32. hints;
  33.  
  34. ZeroMemory(&hints, sizeof(hints));
  35. hints.ai_family = AF_UNSPEC;
  36. hints.ai_socktype = SOCK_STREAM;
  37. hints.ai_protocol = IPPROTO_TCP;
  38.  
  39. #define DEFAULT_PORT "6667"
  40.  
  41. iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
  42. if (iResult != 0) {
  43. printf("getaddrinfo failed: %d\n", iResult);
  44. WSACleanup();
  45. return 1;
  46. }
  47.  
  48. SOCKET ConnectSocket = INVALID_SOCKET;
  49.  
  50. ptr = result;
  51.  
  52. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  53. ptr->ai_protocol);
  54.  
  55. if (ConnectSocket == INVALID_SOCKET) {
  56. printf("Error at socket(): %ld\n", WSAGetLastError());
  57. freeaddrinfo(result);
  58. WSACleanup();
  59. return 1;
  60. }
  61.  
  62. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  63. if (iResult == SOCKET_ERROR) {
  64. closesocket(ConnectSocket);
  65. ConnectSocket = INVALID_SOCKET;
  66. }
  67.  
  68. cout << "working";
  69. cin >> a;
  70. }
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement