Advertisement
wzLeonardo

CPPClientTCP

Jan 4th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <WS2tcpip.h>
  4. #pragma comment(lib, "ws2_32.lib")
  5.  
  6. using namespace std;
  7.  
  8. void main()
  9. {
  10. string ipAddress = "127.0.0.1"; // IP Address of the server
  11. int port = 54000; // Listening port # on the server
  12.  
  13. // Initialize WinSock
  14. WSAData data;
  15. WORD ver = MAKEWORD(2, 2);
  16. int wsResult = WSAStartup(ver, &data);
  17. if (wsResult != 0)
  18. {
  19. cerr << "Can't start Winsock, Err #" << wsResult << endl;
  20. return;
  21. }
  22.  
  23. // Create socket
  24. SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
  25. if (sock == INVALID_SOCKET)
  26. {
  27. cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
  28. WSACleanup();
  29. return;
  30. }
  31.  
  32. // Fill in a hint structure
  33. sockaddr_in hint;
  34. hint.sin_family = AF_INET;
  35. hint.sin_port = htons(port);
  36. inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
  37.  
  38. // Connect to server
  39. int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
  40. if (connResult == SOCKET_ERROR)
  41. {
  42. cerr << "Can't connect to server, Err #" << WSAGetLastError() << endl;
  43. closesocket(sock);
  44. WSACleanup();
  45. return;
  46. }
  47.  
  48. // Do-while loop to send and receive data
  49. char buf[4096];
  50. string userInput;
  51. string SvCMD;
  52. while (true)
  53. {
  54. ZeroMemory(buf, 4096);
  55. int bytesReceived = recv(sock, buf, 4096, 0);
  56. if (bytesReceived > 0)
  57. {
  58. // Echo response to console
  59. // Respostas
  60. SvCMD = string(buf);
  61. if (SvCMD == "Key") {
  62. cout << "Key Logger On> " << endl;
  63. }
  64. cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
  65. }
  66. }
  67.  
  68.  
  69. // Gracefully close down everything
  70. closesocket(sock);
  71. WSACleanup();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement