Guest User

Untitled

a guest
Jul 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock2.h>
  3. #include <ws2tcpip.h>
  4. #pragma comment(lib, "ws2_32.lib")
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. // Startup
  10. WSADATA wdata;
  11. int iStart = WSAStartup(MAKEWORD(2,2), &wdata);
  12. if( iStart == SOCKET_ERROR )
  13. {
  14. cout << "Startup Fail" << endl;
  15. }
  16. else {
  17. cout << "Startup Success" << endl;
  18. }
  19.  
  20. // Socket
  21. SOCKET sConnect = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  22. if( sConnect == SOCKET_ERROR )
  23. {
  24. cout << "Socket Fail" << endl;
  25. WSACleanup();
  26. }
  27. else {
  28. cout << "Socket Success" << endl;
  29. }
  30.  
  31. // Info
  32. SOCKADDR_IN info;
  33. info.sin_family = AF_INET;
  34. info.sin_addr.s_addr = inet_addr("127.0.0.1");
  35. info.sin_port = htons(22580);
  36.  
  37. // Connect
  38. int iConn = connect(sConnect, (SOCKADDR *)&info, sizeof(info));
  39. if( iConn == SOCKET_ERROR )
  40. {
  41. cout << "Connect Fail" << endl;
  42. WSACleanup();
  43. closesocket(sConnect);
  44. }
  45. else {
  46. cout << "Connect Success" << endl;
  47. }
  48.  
  49. // Recieve
  50. char cRecv[8192];
  51. int iRecieve;
  52. while(true)
  53. {
  54. iRecieve = recv(sConnect, cRecv, sizeof(cRecv), 0);
  55. for(int i = 0; i < iReceive; i++) printf("%.2X", cRecv[i]);
  56. printf("\n\n");
  57. }
  58. if( iRecieve == SOCKET_ERROR )
  59. {
  60. cout << "Recieve Fail" << endl;
  61. }
  62. else {
  63. cout << cRecv << endl;
  64. }
  65.  
  66.  
  67. WSACleanup();
  68. closesocket(sConnect);
  69.  
  70. cin.get();
  71. return 0;
  72. }
Add Comment
Please, Sign In to add comment