Skip to content

Instantly share code, notes, and snippets.

@aneury1
Created July 4, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneury1/e1068b81338df19171da4406916ac3b9 to your computer and use it in GitHub Desktop.
Save aneury1/e1068b81338df19171da4406916ac3b9 to your computer and use it in GitHub Desktop.
/// another dummy tcpclient
///g++ client.cpp -oclient -lwsock32 -lws2_32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using std::string;
// 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 "22"
struct WinsockInitIO{
WinsockInitIO(){
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n\n", iResult);
return ;
}
}
~WinsockInitIO(){
WSACleanup();
}
};
WinsockInitIO object;
SOCKET connecto_to(const char *address, const char *port)
{
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char recvbuf[DEFAULT_BUFLEN]={0};
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
if (address ==nullptr) {
printf("usage: %s server-name\n");
return -1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(address, ((port==nullptr)?DEFAULT_PORT:port), &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
return -1;
}
int point = 1;
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
return -1;
}
///inet_ntop(ptr->ai_family,ptr->ai_addr,ip,32);
sockaddr_in* addr=(sockaddr_in*)ptr->ai_addr;
char *ip = inet_ntoa(addr->sin_addr);
printf("IP To connect : %s\n",ip);
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if(iResult == 0){
printf("Connected\n");
freeaddrinfo(result);
return ConnectSocket;
}
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
printf("Connect failed number=>%d\n\n", point++);
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
return -1;
}
return ConnectSocket;
}
string recv_all(SOCKET so)
{
int iResult=0;
char recvbuf[8196]={0};
string ret;
do {
iResult = recv(so, recvbuf, 8196, 0);
if ( iResult > 0 )
{
printf("Bytes received: %d\n", iResult);
ret+=recvbuf;
memset(recvbuf,0,8196);
}
else if ( iResult == 0 )
printf("\nConnection closed\n");
else
printf("\n\nrecv failed with error: %d\n\n", WSAGetLastError());
} while( iResult > 0 );
return ret;
}
string send_request_command(SOCKET so, const char *cmd)
{
int iResult = send( so, cmd, (int)strlen(cmd), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(so);
return "";
}
return recv_all(so);
}
int main(int argc, char **argv)
{
SOCKET ConnectSocket = connecto_to(argv[1], nullptr);
if(ConnectSocket==-1){
printf("Invalid socket");
return -1;
}
else
{
string send_buffer;
send_buffer += "Query here";
printf("Request %s", send_buffer.c_str());
string res = send_request_command(ConnectSocket,send_buffer.c_str());
printf("Responde %s", res.c_str());
}
closesocket(ConnectSocket);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment