cannot connect to accepting sockets...

  • Follow


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,sizeof(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;
}
0
Reply blr70 7/24/2003 6:09:12 AM

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,sizeof(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



0
Reply Alexander 7/24/2003 7:56:04 AM


"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
     http://mathforum.org/library/view/10978.html
   =====================================


0
Reply Alex 8/2/2003 1:21:45 PM

2 Replies
431 Views

(page loaded in 0.003 seconds)


Reply: