Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This should be placed before #include <Windows.h> if the windows include is used
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <cstdio>
- #include <Windows.h>
- #include <WinSock2.h>
- #include <WS2tcpip.h>
- /// TEST CODE /// REMOVE AFTER TESTING WAS DONE ///
- int main()
- {
- WSADATA wsaData;
- int iResult;
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0)
- {
- printf("WSAStartup failed: %d\n", iResult);
- return 1;
- }
- struct addrinfo* result = NULL, * ptr = NULL, hints;
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- // Resolve the local address and port to be used by the server
- iResult = getaddrinfo("0.0.0.0", "1337", &hints, &result);
- if (iResult != 0)
- {
- printf("getaddrinfo failed: %d\n", iResult);
- WSACleanup();
- return 1;
- }
- SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (ListenSocket == INVALID_SOCKET)
- {
- printf("Error at socket(): %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- }
- iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR)
- {
- printf("bind failed with error: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- freeaddrinfo(result);
- if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
- {
- printf("Listen failed with error: %ld\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- // Handling pending connections
- puts("Waiting for a connection..");
- SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
- if (ClientSocket == INVALID_SOCKET)
- {
- printf("accept failed: %d\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- #define DEFAULT_BUFLEN 512
- char recvbuf[DEFAULT_BUFLEN];
- int iSendResult;
- int recvbuflen = DEFAULT_BUFLEN;
- int size =0 ;
- // Receive until the peer shuts down the connection
- do
- {
- iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- {
- printf("Bytes received: %d\n", iResult);
- size += iResult;
- // I thought this was the problem but after I removed it from the code it still was the same.
- //
- // Echo the buffer back to the sender
- //iSendResult = send(ClientSocket, recvbuf, iResult, 0);
- /*if (iSendResult == SOCKET_ERROR)
- {
- printf("send failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }*/
- //printf("Bytes sent: %d\n", iSendResult);
- }
- else if (iResult == 0)
- printf("Connection closing...\n");
- else
- {
- printf("recv failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- }
- while (iResult > 0);
- recvbuf[size] = 0;
- printf("Data received: %s\n", recvbuf);
- // Stops responding to requests, still can accept tho
- iResult = shutdown(ClientSocket, SD_SEND);
- if (iResult == SOCKET_ERROR)
- {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- // Close socket, no receiving/sending
- closesocket(ClientSocket);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement