cannot connect to accepting sockets...

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default cannot connect to accepting sockets...

    hello all,

    I am writing a Gtk (well Gtkmm actually) version of othello that has
    aspirations of being networkable, but I am having a little problem.
    The program is written in
    C++ and therefore has a networking object that is created when a
    network game is selected from the dropdown menus. The Constructor of
    my networking object gets the remote IP and port and then creates a
    socket, binds to a port, and then listens.

    Then i call a function of the networking object that tests the
    connection. This is where the problem occurs...
    This function first attempts to connect to the remote ip on the
    specified port but if that returns an error, then it tries to accept.
    However, when i run two instances of this on seperate machines on a
    LAN, then both connects fail and they both accept and therefore
    nothing ever happens. Does anybody have any idea why this is
    happening? all source that I think is relevant is posted below, any
    help would be greatly appreciated...


    /* My Constructor, all variables that are not declared are in header
    */

    Networking::Networking(int p, string IP)
    {
    port = p;
    remoteIP = IP;
    int yes=1;

    if( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
    {
    cout << "socket() error\n";
    perror("socket");
    exit(1);
    }

    my_addr.sin_family = AF_INET; // host byte order
    my_addr.sin_port = htons( port ); // short, network byte order
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

    their_addr.sin_family = AF_INET; // host byte order
    their_addr.sin_port = htons( port ); // short, network byte order
    inet_aton( remoteIP.c_str() ,(&their_addr.sin_addr) );
    memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the
    struct

    if( bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct
    sockaddr)) == -1 )
    {
    cout << "bind() error\n";
    perror("bind");
    exit(1);
    }

    if( setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&yes,si zeof(int)) ==
    -1 )
    {
    cout << "setsockopt() error\n";
    perror("setsockopt");
    exit(1);
    }

    if( listen(sock_fd, BACKLOG) == -1 )
    {
    cout << "listen() error\n";
    perror("listen");
    exit(1);
    }

    }

    /* my test connection function, this function never exits and waits at
    the accept call indefinitely (until i kill it that is) */

    bool Networking::TestConnection()
    {
    if( connect(sock_fd, (struct sockaddr *) &their_addr,
    sizeof(their_addr)) == -1 )
    {
    cout << "couldn't connect, trying to accept...\n";

    if( (accept_fd = accept(sock_fd, (struct sockaddr *)&their_addr,
    &sin_size)) == -1 )
    {
    cout << "server_accept() error\n";
    return false;
    }
    }
    return true;
    }
    Doran Xenos Guest

  2. Similar Questions and Discussions

    1. Not accepting connections
      My problem is exactly the same as what has been described here except that once a page is being edited, they get the "Contribute could not connect...
    2. Sockets - client unable to connect
      Hi All, I am new to Perl world. I wrote a simple client and server program using sockets. Please see code below. Server accepts connection...
    3. Webservice Accepting XML
      Can anyone point me to any resources/examples on how to build a webservice that accepts an xml file. I have customers who need to update a...
    4. Accepting data from URL Parameters
      I appologize for what may be a newbie-like request, but I have not been able to find this information in the PHP documentation. If I were to have...
    5. accepting keboard entries
      I am new to director and was hoping that someone could explain how how to enable a user to answer questions for a game I want to make. (The...
  3. #2

    Default Re: cannot connect to accepting sockets...

    Hello, Doran Xenos
    > I am writing a Gtk (well Gtkmm actually) version of othello that has
    > aspirations of being networkable, but I am having a little problem.
    > The program is written in
    > C++ and therefore has a networking object that is created when a
    > network game is selected from the dropdown menus. The Constructor of
    > my networking object gets the remote IP and port and then creates a
    > socket, binds to a port, and then listens.
    >
    > Then i call a function of the networking object that tests the
    > connection. This is where the problem occurs...
    > This function first attempts to connect to the remote ip on the
    > specified port but if that returns an error, then it tries to accept.
    > However, when i run two instances of this on seperate machines on a
    > LAN, then both connects fail and they both accept and therefore
    > nothing ever happens. Does anybody have any idea why this is
    > happening? all source that I think is relevant is posted below, any
    > help would be greatly appreciated...
    >
    >
    > /* My Constructor, all variables that are not declared are in header
    > */
    >
    > Networking::Networking(int p, string IP)
    > {
    > port = p;
    > remoteIP = IP;
    > int yes=1;
    >
    > if( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
    >
    > cout << "socket() error\n";
    > perror("socket");
    > exit(1);
    > }
    >
    > my_addr.sin_family = AF_INET; // host byte order
    > my_addr.sin_port = htons( port ); // short, network byte order
    > my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    > memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    >
    > their_addr.sin_family = AF_INET; // host byte order
    > their_addr.sin_port = htons( port ); // short, network byte order
    > inet_aton( remoteIP.c_str() ,(&their_addr.sin_addr) );
    > memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the
    > struct
    >
    > if( bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct
    > sockaddr)) == -1 )
    > {
    > cout << "bind() error\n";
    > perror("bind");
    > exit(1);
    > }
    >
    > if( setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&yes,si zeof(int)) ==
    > -1 )
    > {
    > cout << "setsockopt() error\n";
    > perror("setsockopt");
    > exit(1);
    > }
    >
    > if( listen(sock_fd, BACKLOG) == -1 )
    > {
    > cout << "listen() error\n";
    > perror("listen");
    > exit(1);
    > }
    >
    > }
    >
    > /* my test connection function, this function never exits and waits at
    > the accept call indefinitely (until i kill it that is) */
    >
    > bool Networking::TestConnection()
    > {
    > if( connect(sock_fd, (struct sockaddr *) &their_addr,
    > sizeof(their_addr)) == -1 )
    > {
    > cout << "couldn't connect, trying to accept...\n";
    >
    > if( (accept_fd = accept(sock_fd, (struct sockaddr *)&their_addr,
    > &sin_size)) == -1 )
    > {
    > cout << "server_accept() error\n";
    > return false;
    > }
    > }
    > return true;
    > }
    sequence of calls for server is:

    socket()
    bind()
    listen()
    accept()
    while()
    { send()/recv{}
    }
    close()

    sequence of calls for client is:

    socket()
    connect()
    while()
    { send()/recv{}
    }
    close()

    You need design your application as client or as server, otherwise
    you need to specify your program behavior at runtime (and implement
    both of them).

    see "UNIX System Programming Using C++" by Terrence Chan.
    --
    kris



    Alexander Krisak Guest

  4. #3

    Default Re: cannot connect to accepting sockets...


    "Alexander Krisak" <chris@imp.lg.ua> wrote in message news:bfo367$h5mnh$1@ID-200876.news.uni-berlin.de...
    [snip]
    >
    ====== TCP (Stream Sockets) ======
    > sequence of calls for server is:
    >
    > socket()
    > bind()
    > listen()
    > accept()
    > while()
    --------------------
    > { send()/recv{}
    {
    recv()/send();
    --------------------
    > }
    > close()
    >
    > sequence of calls for client is:
    >
    > socket()
    > connect()
    > while()
    > { send()/recv{}
    > }
    > close()
    >
    ============================

    ====== UDP (Datagrams) ======
    sequence of calls for server is:

    socket()
    bind()
    while()
    {
    recv()
    }
    close()

    sequence of calls for client is:

    socket()
    while()
    {
    send()
    }
    close()
    ============================

    [snip]
    >
    > see "UNIX System Programming Using C++" by Terrence Chan.
    [snip]


    Is it possible to see samples of C++ socket classes from that book ?
    (I don't have the Terrence Chan' book).

    Does anybody work with these classes?

    =====================================
    Alex Vinokur
    mailto:alexvn@connect.to
    [url]http://mathforum.org/library/view/10978.html[/url]
    =====================================


    Alex Vinokur Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139