[Solved] problem with socket program for p2p 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.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with socket program for p2p

Post by doublemax »

I'm not going to write the whole program for you. I didn't even try to understand your whole code, just the parts that i needed to answer your question.

So please explain what you're trying to achieve and what you don't understand. If you just rely on code snippets from other people, you won't get very far.
Use the source, Luke!
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hi doublemax


I badly explained myself...

I need a basic code example on the mysocket :: connect function that can put new connections in a socket list without losing the other already active connections

had this basic example I continue to implement the project


I don't ask you all the code of the whole program, but only some ideas on how to proceed



marco_84
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: problem with socket program for p2p

Post by Nunki »

Hi Marco,

I think it would be wise to do some reading on sockets and how to build a client-server. Luckily sockets is quite standard on all platforms so you could start with the following book:
UNIX Network Programming
Networking APIs: Sockets and XTI
Volume 1
by W. Richard Stevens

This will help you grasp the concepts and find the flaws in your design or what it is you are trying to achieve.

with regards,
Nunki
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: problem with socket program for p2p

Post by Nunki »

Hi Marco,

If I may reply on your last comment,
Basically a sochet server starts and listens on a socket port for incomming connection requests - by socket clients of course. Once a connection is made the server tells the client lets continue our conversation on another port xxxxx and the connection is rerouted to this port. That frees up the well-known port of the server for other clients to connect. So after a while you have the main server which is listening to incomming requests on port aaaa, while several clients are connected on different ports bbbb, cccc, dddd etc....

Now this is a very important concept to keep in mind and understand.

with regards,
Nunki
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hi Nunki

thanks for the suggestion, I have documented myself on peer to peer and I know how the client-server works.
But I need a code with a basic example written with wxWidgets that makes me understand how I can connect to two nodes or more without not disconnecting any of the nodes already connected on the network

I report below the example I want to get:

Is it possible to connect to multiple servers simultaneously from one clients?

for example: three pc connected with p2psocket.run starded

PC1: IP 192.168.1.83

PC2: IP 192.168.1.79

PC3: IP 192.168.1.85

connection established whit all three PCs:

PC1: connect to: 192.168.1.79 and 192.168.1.85
pc2: connect to: 192.168.1.85 and 192.168.1.83
pc3: connect to: 192.168.1.83 and 192.168.1.79

without lost the connection from one of the other server PCs connected simultaneously
thanks for your help

marco_84
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: problem with socket program for p2p

Post by Nunki »

Hi Marco,

Just to be sure... You want one PC to connect to multiple servers ?

so PC1 connects to SRV1 and SRV2 and SRV3 ..... up to SRVx ? right ?

If this is the case, that is perfectly possible. Let's say you have created your application to listen on port 2000 so the client tries to connect to SRV1 port 2000 negotiates and is redirected to a different port, all is ok. Then the client tries to connect to SRV2 also port 2000 (for it is the same software running on all servers) gets a connection and goes on to the third server etc.....

Of course you will need a class with all variables needed to build the connection and have buffers to put data into etc..

Trick will be not to use class ccSoComm, but to create multiple instances by using pointers like pSoComm = new ccSoComm() so that you get a variable per connection. You may of course store these pointers in a vector.

To my opinion a not so difficult problem.

Unless I'm wrong in my assumptions.

with regards,
Nunki
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hi Nunki

it's just what I want to create, I did as you suggested, however, when I connect it crash giving me the following error:

Code: Select all

Thread 7 "p2psocket.run" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe99f8700 (LWP 1701)]
0x000000000043e60b in mysocket::connect (this=0xbccc00, sock=0xe272f0, 
    m_parent=0xbccc00, address=...) at p2psocket.cpp:289
289	  socket->Connect(addr, false);

below placed the offending function!

Code: Select all

void mysocket :: connect(wxSocketClient * sock, wxEvtHandler* m_parent, wxString address)
{
wxIPV4address addr;
wxString message;

  wxString hostname = address;
  addr.Hostname(hostname);
  addr.Service(3000);    

wxSocketClient * pClient = sock;
m_socket_list_clients.push_back(pClient);
SocketListClient::iterator it = m_socket_list_clients.end();
wxSocketClient * socket = *it;
message = wxT("\n-= Trying to connect "+hostname+": " + "3000 (10 sec)...\n");

      // notify the main thread
      wxThreadEvent *evt = new wxThreadEvent(wxEVT_THREAD);
      evt->SetString(message);
      m_parent->QueueEvent(evt);

  socket->Connect(addr, false);
  socket->WaitOnConnect(10);

  if (socket->IsConnected()) {          //check if connected
    message = wxT("-= Succeeded! Connection established\n");
      // notify the main thread
      wxThreadEvent *evt2 = new wxThreadEvent(wxEVT_THREAD);
      evt2->SetString(message);
      m_parent->QueueEvent(evt2);

    socket->GetLocal(addr);        
    message = wxT("-= Your IP: "+addr.IPAddress());

      // notify the main thread
      wxThreadEvent *evt3 = new wxThreadEvent(wxEVT_THREAD);
      evt3->SetString(message);
      m_parent->QueueEvent(evt3);
  }
  else
  {
    socket->Close();
    message = wxT("-= Failed! Unable to connect\nCan't connect to the specified host");
      // notify the main thread
      wxThreadEvent *evt4 = new wxThreadEvent(wxEVT_THREAD);
      evt4->SetString(message);
      m_parent->QueueEvent(evt4);
  }    
m_socket_list_clients.push_back(socket);
}
can you help me


thank you

marco_84
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with socket program for p2p

Post by doublemax »

Code: Select all

wxSocketClient * pClient = sock;
m_socket_list_clients.push_back(pClient);
SocketListClient::iterator it = m_socket_list_clients.end();
wxSocketClient * socket = *it;
"m_socket_list_clients.end()" does not return the last item in the list, "socket" will be invalid.
http://www.cplusplus.com/reference/list/list/end/

Besides, it doesn't make sense to get the socket pointer from the list, you already have it in pClient/sock.
Use the source, Luke!
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hi doublemax


the "pclient/sock" if I use them directly it disconnects the connection already established for to connect the new connection


marco_84
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with socket program for p2p

Post by doublemax »

AFAIK: A socket connects exactly two endpoints. That's why a new socket is created each time a new client connects to a server.
Use the source, Luke!
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: problem with socket program for p2p

Post by Nunki »

Hi Marco,

A segmentation fault is most likely when you use an invalid variable or uninitialised pointer. The error message gives you the information. The call socket->Connect(addr,false); is faulty in some way. Two possibilities either socket is not initialised or addr is wrong. You put pClient into a vector and then you initialise the iterator it to the end of the vector. Not a smart move. Keep in mind this:

Code: Select all

for (it = vec.begin();   it != vec.end();   ++it)
this is the way you loop over a vector. This construct tells you that vec.end() is NOT the last element of the vector. It is more like an end of vector marker, beyond the last element. That's why you dont test for equality '==' but for inequality '!=' . To get the last element, for that you will need the method back(); which is the opposite of the method front();

http://www.cplusplus.com/reference/vector/vector/
is a good reference site for basic C++ stuff like vectors, strings, lists, queues etc...

with regards,
Nunki
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hello everybody

I have inserted vectors with stl <vector>, in these days I have made attempts, now the program will not crash
but the problem of multiple connection remains "if I connect a new IP it disconnects from the IP address already connected"

place the responses of p2p socket connected between this PC and another PC on the network:

Code: Select all

-= Trying to connect 192.168.1.83: 3000 (10 sec)...
-= Succeeded! Connection established
-= Your IP: 192.168.1.83
-=connected to the client192.168.1.83
-= Trying to connect 192.168.1.79: 3000 (10 sec)...
-= Deleted client 192.168.1.83 - connection lost
-= Succeeded! Connection established
-= Your IP: 192.168.1.83
I also attach the source program for your control to give me some help


thanks everybody

marco_84
Attachments
p2psocket.h
(2.93 KiB) Downloaded 82 times
p2psocket.cpp
(10.92 KiB) Downloaded 92 times
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with socket program for p2p

Post by doublemax »

In the mysocket constructor, you create one client socket:

Code: Select all

// Create the socket
m_client = new wxSocketClient();
You need a separate socket for each outgoing connection.
Use the source, Luke!
User avatar
marco_84
Experienced Solver
Experienced Solver
Posts: 71
Joined: Mon Jul 08, 2019 7:30 pm
Location: Italy, Tuscany

Re: problem with socket program for p2p

Post by marco_84 »

Hello everybody
I still need your help,
I managed to make the multiple connection but I get the following error:

Code: Select all

./src/common/socket.cpp(1984): assert "(flags & wxSOCKET_BLOCK) || wxIsMainThread()" failed in wxSocketClient(): Non-blocking sockets may only be created in the main thread [in thread 7fa8b3b90700]


Call stack:
[01] wxSocketClient::wxSocketClient(int)     
[02] mysocket::connect(wxEvtHandler*, wxString)	/home/aceraspiree1532/Scrivania/mysocket/p2psocket.cpp:321
[03] ThreadClientConnect::Entry()            	/home/aceraspiree1532/Scrivania/mysocket/p2psocket.h:94
[04] wxThread::CallEntry()                   
[05] wxThreadInternal::PthreadStart(wxThread*)
[06] 0x7fa8c1f2d6db
[07] clone  

I tried to insert the flags as the "wxsocket_block" how to error suggests, the program doesn't show the error but doesn't show the chat message that is returned from the server, I insert the flag: "wxsocket wait_all" the message is sent and received but the error continues to return

what can I write to make it work without error?

below i paste the correct function "connect"

Code: Select all

void mysocket :: connect(wxEvtHandler* m_parent, wxString address)
{
typedef std::vector<wxIPV4address> IPClients;
IPClients list_ip_clients;
wxIPV4address addr;
wxIPV4address address_verifing;
wxString message;

  wxString hostname = address;
  addr.Hostname(hostname);
  addr.Service(3000);    

for (int i = 0; i<= lastclient; i++)
{
m_socket_list_clients[i]->GetPeer(address_verifing);
list_ip_clients.push_back(address_verifing);
if (list_ip_clients[i].IPAddress() == addr.IPAddress())
{
message = wxT("\nyou are already connected to the client: " + addr.IPAddress() + "\n");
      // notify the main thread
      wxThreadEvent *evt7 = new wxThreadEvent(wxEVT_THREAD);
      evt7->SetString(message);
      m_parent->QueueEvent(evt7);

return;
}
}

m_socket_list_clients.push_back(new wxSocketClient(wxSOCKET_WAITALL));
lastclient++;

message = wxT("\n-= Trying to connect "+hostname+": " + "3000 (10 sec)...\n");

      // notify the main thread
      wxThreadEvent *evt = new wxThreadEvent(wxEVT_THREAD);
      evt->SetString(message);
      m_parent->QueueEvent(evt);

  m_socket_list_clients[lastclient]->Connect(addr, false);
  m_socket_list_clients[lastclient]->WaitOnConnect(10);

  wxMySetupClient(m_socket_list_clients[lastclient], NULL);

  if (m_socket_list_clients[lastclient]->IsConnected())           //check if connected
{
    message = wxT("-= Succeeded! Connection established\n");
      // notify the main thread
      wxThreadEvent *evt2 = new wxThreadEvent(wxEVT_THREAD);
      evt2->SetString(message);
      m_parent->QueueEvent(evt2);

    m_socket_list_clients[lastclient]->GetLocal(addr);        
    message = wxT("-= Your IP: "+addr.IPAddress());

      // notify the main thread
      wxThreadEvent *evt3 = new wxThreadEvent(wxEVT_THREAD);
      evt3->SetString(message);
      m_parent->QueueEvent(evt3);
  }
  else
  {
    m_socket_list_clients[lastclient]->Close();
    message = wxT("-= Failed! Unable to connect\nCan't connect to the specified host");
      // notify the main thread
      wxThreadEvent *evt4 = new wxThreadEvent(wxEVT_THREAD);
      evt4->SetString(message);
      m_parent->QueueEvent(evt4);
}
}

thanks everybody

marco_84
I'm using: Ubuntu and Windows platform with wxwidgets 3.1.4
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with socket program for p2p

Post by doublemax »

The error message is pretty clear, you should not create the socket in a secondary thread. As your whole application uses sockets in event-driven mode, you can't just change the socket to blocking mode and expect it to work.

Besides, as far as i can see, all threads in your code are pointless at the moment as you're using events with sockets. Maybe you should get rid of all threads for a start, that should make the code much cleaner, too.
Use the source, Luke!
Post Reply