Problem binding a TCP Client to a specified port Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Guido
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Apr 23, 2007 6:52 pm
Location: Italy

Problem binding a TCP Client to a specified port

Post by Guido »

I have this problem trying to make a TCP Client (it's a wxSocketClient) that binds itself to a specified port with:

Code: Select all

sock_cl->Connect(addr_server,addr_client,false);
The first connection works perfectly. But if the Client disconnects and then tries to reconnect using the same port the connection fails (the Server doesn't even get the connection request).
Trying to correct this error I found a lot of strange things:
This is the function that close the connection with the server:

Code: Select all

void ClientTCPFrm::DisconnectClick(wxCommandEvent& event)
{
  sock_cl->Close();
}
After sock_cl->Close(); if i put sock_cl->Ok(); it returns a true value, but that should mean that the client is still connected (the documentation here says that For wxSocketClient, Ok won't return true unless the client is connected to a server), right?
But if in the same place I put sock_cl->IsDisconnect(); that returns true!!!
So at the same time for Ok() the client is connected to a server but for IsDisconnect() that's not true ... very confusing

I tried to destroy the socket and rebuild it after the connection is closed:

Code: Select all

void ClientTCPFrm::DisconnectClick(wxCommandEvent& event)
{
  sock_cl->Close();
  sock_cl->Destroy();
  sock_cl = new wxSocketClient();
  sock_cl->SetEventHandler(*this, ID_CLIENT);
  sock_cl->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
  sock_cl->Notify(true); 
}
But even like this the successive connection attempt fails.

I tried to save the socket state before the first connection and then restoring it after the disconnection like this:

Code: Select all

...
  sock_cl->SaveState()
  sock_cl->Connect(addr_server,addr_client,false);
...

void ClientTCPFrm::DisconnectClick(wxCommandEvent& event)
{
  sock_cl->Close();
  sock_cl->RestoreState();
}
But nothing changes

I looked for errors with sock_cl->Error(); after sock_cl->Connect(addr_server,addr_client,false); and after
sock_cl->Close();, but in both cases it returns false.

Is it possible that it's a bug, that when it close the connection is closed the port binded to the client is not made available again?
Please help me beacuse this thing is driving me mad ^^'
Last edited by Guido on Fri May 04, 2007 12:40 pm, edited 1 time in total.
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
i don't sure exactly, but try use flag wxSOCKET_REUSEADDR in the client ctor.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Guido
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Apr 23, 2007 6:52 pm
Location: Italy

Post by Guido »

tan wrote:i don't sure exactly, but try use flag wxSOCKET_REUSEADDR in the client ctor
I just tried it, but it doesn't work :cry:
Anyway, thank you for your suggestion :)
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
did you look at wxDIR/samples/sockets ?
It works just fine with client connect/disconnect.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Guido
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Apr 23, 2007 6:52 pm
Location: Italy

Post by Guido »

tan wrote:Hi,
did you look at wxDIR/samples/sockets ?
It works just fine with client connect/disconnect.
Yes I looked at it. That Client works but it doesn't binds itself to a specified port, it connects with:

Code: Select all

sock_cl->Connect(addr_server,false);
So it lets the OS choose the port.
In my case I want to be able to choose a specified port for the Client.
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Guido wrote:Yes I looked at it. That Client works but it doesn't binds itself to a specified port, it connects with:

Code: Select all

sock_cl->Connect(addr_server,false);
So it lets the OS choose the port.
In my case I want to be able to choose a specified port for the Client.
Does it make any sense for the client side???
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Guido
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Apr 23, 2007 6:52 pm
Location: Italy

Post by Guido »

tan wrote:
Guido wrote:Yes I looked at it. That Client works but it doesn't binds itself to a specified port, it connects with:

Code: Select all

sock_cl->Connect(addr_server,false);
So it lets the OS choose the port.
In my case I want to be able to choose a specified port for the Client.
Does it make any sense for the client side???
Well instead of letting the OS decide the port in my case the user can set a specified port for the Client. I don't think that's a problem for the Client. And also that's made possible by the Connect(addr_server,addr_client,boolean); function of wxClientSocket so if there's an appropriate function for it I think it makes sense ^^'
Guido
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Apr 23, 2007 6:52 pm
Location: Italy

Post by Guido »

Problem solved: it's possible for the Client to reconnect using the same port but to do that it must wait at least 120 seconds after closing the previous connection on that port
Post Reply