Thread: echo client problems

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    echo client problems

    Hi,

    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.

    Could not connect to remote host.
    connect: Socket operation on non-socket
    Here is the code for my client:

    Code:
    #include<iostream>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    #define DEST_IP "127.0.0.1"
    #define MAX 1024
    
    using namespace std;
    
    int main()
    {
            char buf[MAX];
    
            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";
                    perror("connect");
            }
    
            cin.getline( buf, MAX, '\n' );
    
            len = sizeof(buf);
    
            while(1)
            {
    
                    check_send=send( sock, buf, len, 0 );
                    switch ( check_send )
                    {
                            case -1:
                                    cout<<"Error sending, bailing out...\n";
                                    break;
                            default:
                                    cout<<check_send<<"Bytes sent\n";
                                    break;
                    }
                    check_recv=recv( sock, buf, MAX, 0 );
                    switch ( check_recv )
                    {
                            case 0:
                                    cout<<"Remote host closed connection.\n";
                                    break;
                            case -1:
                                    cout<<"Error receiving data.\n";
                                    break;
                            default:
                                    cout<<check_recv<<"Bytes received.\n";
                                    break;
                    }
            }
            close(sock);
    }
    Calef13

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    refer to this table
    http://www.cppreference.com/operator_precedence.html

    Code:
    if ( sock=socket(PF_INET, SOCK_STREAM, 0 ) == -1 )
    the part in bold is evaluated first

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    if ( ( sock=socket(PF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET  )
    never assume -1 is the returned error value on failure;
    The same goes for the switch statement.
    Last edited by abachler; 06-27-2007 at 02:24 PM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Thanks for all the help guys, it connects now, just shows how much I still have to learn if I am still messing up operator precedence :P. Thanks about the heads up on the returning -1 but I didn't assume, beej's guide says it returns -1 on error. Is your method more generic so I can use that always instead of -1? If so then I will certainly be using that from now on

    Once again, thanks for all the help guys.

    Calef13

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The method I showed (I wont claim its 'my' method) is more portable. AFAIK all sockets implementations do return -1 on failure, but they arent required to return a specific value. They are however required to define INVALID_SOCKET.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP/IP client & echo server
    By Jordban in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 06:39 PM
  2. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  3. receiving problems in network chat client
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2005, 05:10 AM
  4. Server Client Messaging Program
    By X PaYnE X in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-04-2004, 05:20 PM
  5. Socket Problems
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 08-18-2001, 06:59 AM