Advertisement
wzLeonardo

CPPSeeverTCP

Jan 4th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <WS2tcpip.h>
  3. #include <string>
  4.  
  5. #pragma comment (lib, "ws2_32.lib")
  6.  
  7. using namespace std;
  8.  
  9. void main()
  10. {
  11. // Initialze winsock
  12. WSADATA wsData;
  13. WORD ver = MAKEWORD(2, 2);
  14.  
  15. int wsOk = WSAStartup(ver, &wsData);
  16. if (wsOk != 0)
  17. {
  18. cerr << "Can't Initialize winsock! Quitting" << endl;
  19. return;
  20. }
  21.  
  22. // Create a socket
  23. SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
  24. if (listening == INVALID_SOCKET)
  25. {
  26. cerr << "Can't create a socket! Quitting" << endl;
  27. return;
  28. }
  29.  
  30. // Bind the ip address and port to a socket
  31. sockaddr_in hint;
  32. hint.sin_family = AF_INET;
  33. hint.sin_port = htons(54000);
  34. hint.sin_addr.S_un.S_addr = INADDR_ANY; // Could also use inet_pton ....
  35.  
  36. bind(listening, (sockaddr*)&hint, sizeof(hint));
  37.  
  38. // Tell Winsock the socket is for listening
  39. listen(listening, SOMAXCONN);
  40.  
  41. // Wait for a connection
  42. sockaddr_in client;
  43. int clientSize = sizeof(client);
  44.  
  45. SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
  46.  
  47. char host[NI_MAXHOST]; // Client's remote name
  48. char service[NI_MAXSERV]; // Service (i.e. port) the client is connect on
  49.  
  50. ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
  51. ZeroMemory(service, NI_MAXSERV);
  52.  
  53. if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
  54. {
  55. cout << host << " connected on port " << service << endl;
  56. }
  57. else
  58. {
  59. inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
  60. cout << host << " connected on port " <<
  61. ntohs(client.sin_port) << endl;
  62. }
  63.  
  64. // Close listening socket
  65. closesocket(listening);
  66.  
  67. // While loop: accept and echo message back to client
  68. char buf[4096];
  69. string userInput;
  70. string SvCMD;
  71. do{
  72. ZeroMemory(buf, 4096);
  73. /*int bytesReceived = recv(clientSocket, buf, 4096, 0);
  74. if (bytesReceived == SOCKET_ERROR)
  75. {
  76. cerr << "Error in recv(). Quitting" << endl;
  77. break;
  78. }
  79. */
  80. /* if (bytesReceived == 0)
  81. {
  82. cout << "Client disconnected " << endl;
  83. break;
  84. }
  85. */
  86. // cout << string(buf, 0, bytesReceived) << endl;
  87.  
  88. // Echo message back to client
  89.  
  90. cout << "> ";
  91. getline(cin, userInput);
  92. send(clientSocket, userInput.c_str(), userInput.size() + 1, 0);
  93.  
  94. } while (true);
  95.  
  96.  
  97.  
  98. // Close the socket
  99. closesocket(clientSocket);
  100.  
  101. // Cleanup winsock
  102. WSACleanup();
  103.  
  104. system("pause");
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement