I tried to post this problem in my other thread, but although I keep seeing the 'new posts' icon there are none, so I am going to try and repost this. Also I think since this is a different problem I should make a new thread, I'm sorry if I shouldn't do this but as far as I know I am following the rules.
I wrote a simple echo client in c++, but it keeps crashing whilst calling connect(). I have checked a few examples of other echo clients online, but cannot see what I am missing that theirs has. I am pretty lost so any hints or help would be very appreciated.
Here is the code for my client:
Could not connect to remote host.
connect: Socket operation on non-socket
#define DEST_IP "127.0.0.1"
#define MAX 1024
using namespace std;
struct sockaddr_in dest_addr;
int sock, len, check_recv, check_send;
if ( sock=socket(PF_INET, SOCK_STREAM, 0 ) == -1 )
cout<<"Error, could not open socket\n";
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(31337);
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
memset( dest_addr.sin_zero, '\0', sizeof( dest_addr.sin_zero ) );
if (( connect( sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in))) == -1 )
cout<<"Could not connect to remote host.\n";
cin.getline( buf, MAX, '\n' );
len = sizeof(buf);
check_send=send( sock, buf, len, 0 );
switch ( check_send )
cout<<"Error sending, bailing out...\n";
check_recv=recv( sock, buf, MAX, 0 );
switch ( check_recv )
cout<<"Remote host closed connection.\n";
cout<<"Error receiving data.\n";