Ask a Question related to UNIX Programming, Design and Development.
-
Doran Xenos #1
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
-
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... -
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... -
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... -
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... -
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... -
Alexander Krisak #2
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 )
>sequence of calls for server is:> 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;
> }
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
-
Alex Vinokur #3
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][snip]>
> see "UNIX System Programming Using C++" by Terrence Chan.
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



Reply With Quote

