Advertisement
kyobreck

C++ First Winsock Server

Mar 24th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1.  
  2. #undef UNICODE
  3.  
  4. #define WIN32_LEAN_AND_MEAN
  5.  
  6.  
  7. #include <sdkddkver.h>
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include <Ws2tcpip.h>
  13. #include <WinSock2.h>
  14. #include <Windows.h>
  15. #include <iostream>
  16. #include <signal.h>
  17.  
  18.  
  19.  
  20. #include <sys\types.h>
  21.  
  22. #pragma comment(lib, "Ws2_32.lib")
  23.  
  24.  
  25.  
  26. #define DEFAULT_BUFLEN 512
  27. #define DEFAULT_PORT "27015"
  28.  
  29. int main()
  30. {
  31. WSADATA WSAdata1;
  32. int iResult;
  33.  
  34. SOCKET listenSocket = INVALID_SOCKET;
  35. SOCKET clientSocket = INVALID_SOCKET;
  36.  
  37. struct addrinfo *result = NULL;
  38. struct addrinfo hints;
  39.  
  40. int iSendresult;
  41.  
  42. char recvbuf[DEFAULT_BUFLEN];
  43. int recvbuflen = DEFAULT_BUFLEN;
  44.  
  45. //initialize winsock
  46. iResult = WSAStartup(MAKEWORD(2, 2), &WSAdata1);
  47. if (iResult != 0)
  48. {
  49. std::cout << "Error initializing Winsock\n";
  50. }
  51.  
  52. ZeroMemory(&hints, sizeof(hints));
  53. hints.ai_family = AF_INET;
  54. hints.ai_socktype = SOCK_STREAM;
  55. hints.ai_protocol = IPPROTO_TCP;
  56. hints.ai_flags = AI_PASSIVE;
  57.  
  58. //Resolve server address and port
  59. iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  60. if (iResult != 0)
  61. {
  62. std::cout << "Getaddrinfo failed with: " << WSAGetLastError() << '\n';
  63. WSACleanup();
  64. return 1;
  65. }
  66.  
  67. //Create a socket for connecting to server
  68. listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  69. if (listenSocket == INVALID_SOCKET)
  70. {
  71. std::cout << "Error getting file descriptor with: " << WSAGetLastError() << '\n';
  72. WSACleanup();
  73. return 1;
  74. }
  75.  
  76. // Setup the TCP listening socket
  77. iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
  78. if (iResult == SOCKET_ERROR)
  79. {
  80. std::cout << "Bind failed with: " << WSAGetLastError();
  81. freeaddrinfo(result);
  82. closesocket(listenSocket);
  83. WSACleanup();
  84. return 1;
  85. }
  86.  
  87. freeaddrinfo(result);
  88.  
  89. iResult = listen(listenSocket, SOMAXCONN);
  90. if (iResult == SOCKET_ERROR)
  91. {
  92. std::cout << "Error listening with: " << WSAGetLastError();
  93. closesocket(listenSocket);
  94. WSACleanup();
  95. return 1;
  96. }
  97.  
  98. clientSocket = accept(listenSocket, NULL, NULL); //program waits here
  99. if (clientSocket == INVALID_SOCKET)
  100. {
  101. std::cout << "Accept error with: " << WSAGetLastError() << '\n';
  102. WSACleanup();
  103. closesocket(listenSocket);
  104.  
  105. return 1;
  106.  
  107. }
  108.  
  109. closesocket(listenSocket);
  110.  
  111.  
  112.  
  113. //Recieve until peer shutsdown connection
  114. do
  115. {
  116. iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
  117. if (iResult > 0)
  118. {
  119. std::cout << "Bytes recieved: " << iResult << '\n';
  120. iSendresult = send(clientSocket, recvbuf, iResult, 0);
  121. if (iSendresult == SOCKET_ERROR)
  122. {
  123. std::cout << "Eerror with sending information with: " << WSAGetLastError();
  124. closesocket(clientSocket);
  125. WSACleanup();
  126. return 1;
  127. }
  128. std::cout << "Bytes sent: " << iSendresult << '\n';
  129. }
  130. else if (iResult == 0)
  131. {
  132. std::cout << "Connection closing...\n";
  133. }
  134. else
  135. {
  136. std::cout << "Recievure failed with error with: " << WSAGetLastError << '\n';
  137. closesocket(clientSocket);
  138. WSACleanup();
  139. return 1;
  140. }
  141.  
  142.  
  143. } while (iResult > 0);
  144.  
  145. iResult = shutdown(clientSocket, SD_SEND);
  146. if (iResult == SOCKET_ERROR)
  147. {
  148. std::cout << "Shutdown error with: " << WSAGetLastError() << '\n';
  149. WSACleanup();
  150. closesocket(clientSocket);
  151. return 1;
  152. }
  153.  
  154. //cleanup
  155. closesocket(clientSocket);
  156. WSACleanup();
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement