Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TCP-Client.cpp : Defines the entry point for the console application.
- //
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "7777"
- int __cdecl main(int argc, char **argv)
- {
- printf("TCP-Client\n");
- WSADATA wsaData;
- SOCKET ConnectSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- const char *sendbuf = "this is a test";
- char recvbuf[DEFAULT_BUFLEN];
- int iResult;
- int recvbuflen = DEFAULT_BUFLEN;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed with error: %d\n", iResult);
- getchar(); return 1;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- // Resolve the server address and port
- iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- getchar(); return 1;
- }
- // Attempt to connect to an address until one succeeds
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- getchar(); return 1;
- }
- // Connect to server.
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Unable to connect to server!\n");
- WSACleanup();
- getchar(); return 1;
- }
- int randomSocket;
- recv(ConnectSocket, (char *)&randomSocket, sizeof(int), 0);
- randomSocket = (int)randomSocket;
- std::cout << "odebrano: " << randomSocket << "\n";
- closesocket(ConnectSocket);
- // Resolve the server address and port
- iResult = getaddrinfo(argv[1], (char*)randomSocket, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- getchar(); return 1;
- }
- // Attempt to connect to an address until one succeeds
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- getchar(); return 1;
- }
- // Connect to server.
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Unable to connect to server!\n");
- WSACleanup();
- getchar(); return 1;
- }
- printf("Bytes Sent: %ld\n", iResult);
- recv(ConnectSocket, (char *)&randomSocket, sizeof(int), 0);
- randomSocket = (int)randomSocket;
- std::cout << "odebrano: " << randomSocket << "\n";
- // cleanup
- closesocket(ConnectSocket);
- WSACleanup();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement