Reference Information for Remote Control
R&S
®
SMA100B
531
User Manual 1178.3834.02 ─ 03
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include "TcpClient.h"
TcpClient::TcpClient()
: currentHostName( "" )
, currentPort( 0 )
, currentSocketDescr( 0 )
, serverAddress ( )
, currentHostInfo( NULL )
, clientIsConnected( false )
, receiveBufferSize( 1024 )
{
}
TcpClient::~TcpClient()
{
currentHostInfo = NULL;
}
void TcpClient::connectToServer( string &hostname, int port )
{
currentHostInfo = gethostbyname( hostname.c_str( ) );
if( currentHostInfo == NULL )
{
currentHostName = "";
currentPort = 0;
currentHostInfo = NULL;
clientIsConnected = false;
printf("error connecting host\n" );
}
currentHostName = hostname;
currentPort = port;
currentSocketDescr = socket(AF_INET, SOCK_STREAM, 0);
if( currentSocketDescr == 0 )
{
currentHostName = "";
currentPort = 0;
currentHostInfo = NULL;
clientIsConnected = false;
printf("can't create socket\n" );
}
serverAddress.sin_family = currentHostInfo->h_addrtype;
serverAddress.sin_port = htons( currentPort );
memcpy( (char *) &serverAddress.sin_addr.s_addr,
currentHostInfo->h_addr_list[0], currentHostInfo->h_length );
if( connect( currentSocketDescr, ( struct sockaddr *) &serverAddress,
sizeof( serverAddress ) ) < 0 )
{
throw string("can't connect server\n" );
}
Telnet program examples