Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #undef UNICODE
- #define WIN32_LEAN_AND_MEAN
- #include <sdkddkver.h>
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <Ws2tcpip.h>
- #include <WinSock2.h>
- #include <Windows.h>
- #include <iostream>
- #include <signal.h>
- #include <sys\types.h>
- #pragma comment(lib, "Ws2_32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "27015"
- int main()
- {
- WSADATA WSAdata1;
- int iResult;
- SOCKET listenSocket = INVALID_SOCKET;
- SOCKET clientSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL;
- struct addrinfo hints;
- int iSendresult;
- char recvbuf[DEFAULT_BUFLEN];
- int recvbuflen = DEFAULT_BUFLEN;
- //initialize winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &WSAdata1);
- if (iResult != 0)
- {
- std::cout << "Error initializing Winsock\n";
- }
- 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 server address and port
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
- if (iResult != 0)
- {
- std::cout << "Getaddrinfo failed with: " << WSAGetLastError() << '\n';
- WSACleanup();
- return 1;
- }
- //Create a socket for connecting to server
- listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (listenSocket == INVALID_SOCKET)
- {
- std::cout << "Error getting file descriptor with: " << WSAGetLastError() << '\n';
- WSACleanup();
- return 1;
- }
- // Setup the TCP listening socket
- iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR)
- {
- std::cout << "Bind failed with: " << WSAGetLastError();
- freeaddrinfo(result);
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- freeaddrinfo(result);
- iResult = listen(listenSocket, SOMAXCONN);
- if (iResult == SOCKET_ERROR)
- {
- std::cout << "Error listening with: " << WSAGetLastError();
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- clientSocket = accept(listenSocket, NULL, NULL); //program waits here
- if (clientSocket == INVALID_SOCKET)
- {
- std::cout << "Accept error with: " << WSAGetLastError() << '\n';
- WSACleanup();
- closesocket(listenSocket);
- return 1;
- }
- closesocket(listenSocket);
- //Recieve until peer shutsdown connection
- do
- {
- iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- {
- std::cout << "Bytes recieved: " << iResult << '\n';
- iSendresult = send(clientSocket, recvbuf, iResult, 0);
- if (iSendresult == SOCKET_ERROR)
- {
- std::cout << "Eerror with sending information with: " << WSAGetLastError();
- closesocket(clientSocket);
- WSACleanup();
- return 1;
- }
- std::cout << "Bytes sent: " << iSendresult << '\n';
- }
- else if (iResult == 0)
- {
- std::cout << "Connection closing...\n";
- }
- else
- {
- std::cout << "Recievure failed with error with: " << WSAGetLastError << '\n';
- closesocket(clientSocket);
- WSACleanup();
- return 1;
- }
- } while (iResult > 0);
- iResult = shutdown(clientSocket, SD_SEND);
- if (iResult == SOCKET_ERROR)
- {
- std::cout << "Shutdown error with: " << WSAGetLastError() << '\n';
- WSACleanup();
- closesocket(clientSocket);
- return 1;
- }
- //cleanup
- closesocket(clientSocket);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement