Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <iostream>
- #include <fstream>
- #include <Windows.h>
- #include <WinSock2.h>
- #include <WS2tcpip.h>
- #include <iphlpapi.h>
- #include <stdio.h>
- using namespace std;
- #pragma comment(lib, "Ws2_32.lib")
- #define PORT_NUMBER 1337
- #define SERVER_ADDRESS "localhost"
- #define BUFFER_LENGTH 500
- int ShowError();
- int main()
- {
- WSADATA wsaData;
- int Result;
- Result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (Result != 0)
- {
- WSACleanup();
- return ShowError();
- }
- SOCKET ClientSocket = INVALID_SOCKET;
- ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (ClientSocket == INVALID_SOCKET)
- {
- WSACleanup();
- return ShowError();
- }
- struct hostent* host;
- host = gethostbyname(SERVER_ADDRESS);
- if (host == 0)
- {
- WSACleanup();
- return ShowError();
- }
- SOCKADDR_IN SockAddr;
- SockAddr.sin_family = AF_INET;
- SockAddr.sin_port = htons(PORT_NUMBER);
- unsigned long* address = (unsigned long*)host->h_addr;
- SockAddr.sin_addr.s_addr = *address;
- Result = connect(ClientSocket, (SOCKADDR*)&SockAddr, sizeof(SockAddr));
- if (Result == SOCKET_ERROR)
- {
- WSACleanup();
- return ShowError();
- }
- char ReceiveBuffer[BUFFER_LENGTH];
- memset(ReceiveBuffer, 0, BUFFER_LENGTH-1);
- int length = BUFFER_LENGTH;
- int bReceived = 0; //bytes received
- do
- {
- bReceived = recv(ClientSocket, ReceiveBuffer, length, 0);
- if (bReceived > 0)
- {
- printf("\n\nBytes Received: %d\n", bReceived);
- printf("Received Buffer: %s\n", ReceiveBuffer);
- }
- } while (bReceived > 0);
- printf("Closing connection...\n\n");
- shutdown(ClientSocket, SD_SEND);
- closesocket(ClientSocket);
- WSACleanup();
- system("PAUSE > NUL");
- return 0;
- }
- int ShowError()
- {
- MessageBox(NULL, "An error occured.", 0, 0);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement