Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Actual client.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #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")
- #pragma comment(lib, "Mswsock.lib")
- #pragma comment(lib, "AdvApi32.lib")
- #define WIN32_LEAN_AND_MEAN
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "27015"
- #define DEFAULT_LOOPBACK "127.0.0.1"
- int _tmain(int argc, _TCHAR* argv[])
- {
- WSADATA WSAdata1;
- SOCKET connectSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL;
- struct addrinfo *ptr = NULL;
- struct addrinfo hints;
- char *sendbuf = "This is a test";
- char rcvbuf[DEFAULT_BUFLEN];
- int iResult;
- int recvbuflen = DEFAULT_BUFLEN;
- //validate the parameters
- if (argc != 2)
- {
- printf("Usage server-name %s\n", argv[0]);
- return 1;
- }
- //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;
- //Resolve server address and port
- iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result); //so default address seems to work? test with tyler
- //but if anything its a problem with cmd's input mroe than anything
- //right now
- if (iResult != 0)
- {
- std::cout << "Getaddrinfo failed with: " << WSAGetLastError() << '\n';
- WSACleanup();
- return 1;
- }
- //Connect to first address we fine
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
- {
- //create file descriptor
- connectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
- if (connectSocket == INVALID_SOCKET)
- {
- std::cout << "Error getting file descriptor with : " << WSAGetLastError() << '\n';
- WSACleanup();
- return 1;
- }
- //connect to address
- 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)
- {
- std::cout << "Unable to connect to server...\n";
- WSACleanup();
- return 1;
- }
- //send an initial buffer
- iResult = send(connectSocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR)
- {
- std::cout << "Error sending information with: " << WSAGetLastError() << '\n';
- closesocket(connectSocket);
- WSACleanup();
- return 1;
- }
- std::cout << "Bytes sent: " << iResult;
- //shutdown connection since no more data will be sent
- iResult = shutdown(connectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR)
- {
- std::cout << "Error shutting down socket with: " << WSAGetLastError() << '\n';
- closesocket(connectSocket);
- WSACleanup();
- return 1;
- }
- //recieve till peer closes connection
- do
- {
- iResult = recv(connectSocket, rcvbuf, recvbuflen, 0);
- if (iResult > 0)
- {
- std::cout << "Bytes recieved: " << iResult;
- }
- else if (iResult == 0)
- {
- std::cout << "Connection closed" << '\n';
- }
- else
- {
- std::cout << "Recievure failure with: " << WSAGetLastError();
- }
- } while (iResult > 0);
- //cleanup
- closesocket(connectSocket);
- WSACleanup();
- char a;
- std::cin >> a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement