// Tell the system what kinds of address we want
struct addrinfo addrCriteria;
memset(&addrCriteria, 0, sizeof(addrCriteria));
addrCriteria.ai_family = AF_INET;
addrCriteria.ai_socktype = SOCK_STREAM;
addrCriteria.ai_protocol = IPPROTO_TCP;
// Lookup a list of matching addresses
struct addrinfo *servAddr;
if ( getaddrinfo( argv[ 1 ], PORT_NUMBER, &addrCriteria, &servAddr) != 0 )
printf( "Can't get address info\n" );
// Try to just use the first one.
if ( servAddr == NULL )
printf( "Can't get address\n" );
// Make a socket, and connect it to the address.
int sock = socket( servAddr->ai_family, servAddr->ai_socktype, servAddr->ai_protocol);
if ( sock < 0 )
printf( "Can't create socket\n" );
// Establish the connection to the echo server
if ( connect( sock, servAddr->ai_addr, servAddr->ai_addrlen ) != 0 )
printf( "Can't connect to server\n" );
// We're done with the address info now.
freeaddrinfo(servAddr);
// Buffer to send from, don't really care what's in it.
char buffer[ BLOCK_SIZE ];
bytesToSend = 10;
// Try to write to the socket.
int count = write( sock, buffer, bytesToSend );
// Close the socket connection
close( sock );