Recent

Author Topic: LazWebsocket How to identify IP adress during connect  (Read 3769 times)

hgbk

  • New member
  • *
  • Posts: 9
LazWebsocket How to identify IP adress during connect
« on: April 20, 2021, 03:47:09 pm »
I have implemented a game as client server solution using LazWebsockets. It may happen that during the game the connection from a client breaks. In order to implement the possibility for this specific client to reconnect I need to identify the IP address during accept and compare it with ACommunication.SocketStream.RemoteAddress.Address of this client  which I have stored before failure. In accept I have TRequestData as parameter which delivers Host (Domain but not IP address). Any idea how to solve this?

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: LazWebsocket How to identify IP adress during connect
« Reply #1 on: April 21, 2021, 03:02:55 am »
There is currently no way implemented to identify the client during the handshake. This is defenetly something I will consider adding to the library.

But, I should point out that identifying clients based on their IPs is bad style, as due to the use of NAT networks, multiple devices usually share an external IP. While in Europe and the US it is usually something like one per household, for some (cheaper) internet providers or providers in other countries that haven't been so lucky with getting large shares of the IP address space, multiple homes or even larger parts like city blocks can share the same IP address. So there could be potentially hundreds of people with the same IP.

Therefore you should give the client a unique ID to reconnect. Like when a game starts, you could generate a game ID, which every client gets. If a client looses it's connection it can reconnect by sending this game ID to the server and the server can recognize this and put it back into the right game

hgbk

  • New member
  • *
  • Posts: 9
Re: LazWebsocket How to identify IP adress during connect
« Reply #2 on: April 21, 2021, 01:17:49 pm »
Understand - I will implement the "reconnect" according to your proposal. Thanks a lot for the quick and helpful answer!

r.lukasiak

  • New member
  • *
  • Posts: 7
Re: LazWebsocket How to identify IP adress during connect
« Reply #3 on: September 28, 2021, 08:38:38 pm »
There is currently no way implemented to identify the client during the handshake. This is defenetly something I will consider adding to the library.

But, I should point out that identifying clients based on their IPs is bad style, as due to the use of NAT networks, multiple devices usually share an external IP. While in Europe and the US it is usually something like one per household, for some (cheaper) internet providers or providers in other countries that haven't been so lucky with getting large shares of the IP address space, multiple homes or even larger parts like city blocks can share the same IP address. So there could be potentially hundreds of people with the same IP.

Therefore you should give the client a unique ID to reconnect. Like when a game starts, you could generate a game ID, which every client gets. If a client looses it's connection it can reconnect by sending this game ID to the server and the server can recognize this and put it back into the right game

Hi everyone!
I've been trying to figure out how to use LazWebSocket for some 3 days. I'm analyzing the example  server and client files. I managed to figure out how the client works (at least some basic functions), I created some basic GUI and applied the "client" code to it, I was able to run it iin StartReceiveMessageThread mode. My app is able to sent and receive the message (OnReceiveMessage). But when I run more than 1 instance, messages from the server are sent randomly. I even tried to start my client app from different IPs, local 192.168.x.x and through HamachiLogMeIn 25.x.x.x. Both instances can send messages to the server but the server answers randomly. I understand that it may happen when there are more than 1 client with the same IP but if 2 clients have a different IP address?
How can I make the server answer to a specific client?
Quote
you could generate a game ID, which every client gets
how could I use a client ID to make the server contact the specific client by his ID, not IP?

thanks for any hint.

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: LazWebsocket How to identify IP adress during connect
« Reply #4 on: September 29, 2021, 02:27:17 pm »
In LazWebsockets the connection is handled by the TWebsocketCommunicator instance given as Parameter to the handler.

If you want to handle multiple connections, you need to identify these. The (threaded) base handler already provides a list of all connections, you could use the index to identify these:
Code: Pascal  [Select][+][-]
  1.   procedure TSocketHandler.IdentifyClients;
  2.   var
  3.     ConnectionList: TConnectionList;
  4.     i: Integer;
  5.   begin
  6.     ConnectionList := Connections.Lock;
  7.     try
  8.       for i := 0 to ConnectionList.Count - 1 do
  9.         ConnectionList[i].WriteStringMessage('You are client number ' + i.ToString);
  10.     finally
  11.       Connections.Unlock;
  12.     end;
  13.   end;

The problem here is, when a client disconnects, it changes the indices of all clients connected after that one (as all move by one). So you could rather than that generate an ID (e.g. by simply having a counter counting up) and use a TDictionary or TFPGMap or similar to manage the connections. You might also need a reverse lookup to map the communicator to it's ID (some extendibility option for the communicator might be also useful here, to store such data right in the communicator).

Another solution is, each handler call gets executed in their own Thread (assuming you use a threaded handler). The best way is to have all the communication happen within that handler and you interact though messages in a message queue. When you receive something you can send a message to the GUI to handle the data. When the GUI wants to send something it sends a message to the handler.

But: no matter what approach you choose, you must always make sure to use sufficient thread locks. All the default  datastructures (arrays, Lists, Maps, Dictionaries, etc.) are not thread safe, so when dealing with the threaded websockets, you should always use locks

gucao

  • New member
  • *
  • Posts: 5
Re: LazWebsocket How to identify IP adress during connect
« Reply #5 on: October 06, 2021, 12:46:38 pm »
The accepted socket is the SocketStream.handle?
can get the ip and port from the accepted  socket
The code need test
Code: Pascal  [Select][+][-]
  1. uses sockets
  2.  
  3. var
  4.   VRtInt,VL:Integer;
  5.   VAddr:TSockAddr;
  6.   VSocketErr: cint;  
  7.   VIP:string;
  8.   VPort:integer;
  9. begin
  10.   VL:=SizeOf(VAddr);  
  11.   VRtInt:=fpgetpeername(ASocket,@VAddr,@VL);
  12.   if VRtInt=0 then begin
  13.      VIp:=NetAddrToStr(VAddr.sin_addr);
  14.      VPort:=NToHs(VAddr.sin_port);
  15.   end else begin
  16.      Result:='faild`````';
  17.      Exit;
  18.   end;  
  19. end;
  20.  
  21.  
  22.  


r.lukasiak

  • New member
  • *
  • Posts: 7
Re: LazWebsocket How to identify IP adress during connect
« Reply #6 on: October 14, 2021, 10:35:19 am »
Hi there!
I'm sorry for the delay in answering.

Warfley, thanks for the explanation, it seems pretty clear, however I'd have several questions:
1.
Quote
"The (threaded) base handler"
socket.AcceptingMethod:=samThreaded?

2. Does a new client always get the highest number in ConnectionList? If there are 5 connected clients, I suppose they are numbered 0..4 so the new one will get 5, right?
Clients have their user names so basing on ConnectionList, I could have some list linking ConnectionList's client number to the user name but...

3.
Quote
"when a client disconnects, it changes the indices of all clients connected"
Is there any event going on in this situation? how can I know that someone disconnected to update my list?

gucao, I haven't tested it it but quickly analyzing the code, I understand that it's extracting IP and port from the handler?
« Last Edit: October 14, 2021, 10:37:40 am by r.lukasiak »

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: LazWebsocket How to identify IP adress during connect
« Reply #7 on: October 14, 2021, 02:47:10 pm »
Warfley, thanks for the explanation, it seems pretty clear, however I'd have several questions:
1.
Quote
"The (threaded) base handler"
socket.AcceptingMethod:=samThreaded?
Nope, that makes the accepting handshake threaded (basically before establishing a connection a HTTP request is made, with samThreaded or samThreadPool this will be threaded).

What I meant is when implementing the handler you create a class inheriting from TWebsocketHandler. If you inherit from  TThreadedWebsocketHandler you make use of the threaded handler.
In the ChatServer example this would be this line:
Code: Pascal  [Select][+][-]
  1.   TSocketHandler = class(TThreadedWebsocketHandler) // inherit from TThreadedWebsocketHandler

2. Does a new client always get the highest number in ConnectionList? If there are 5 connected clients, I suppose they are numbered 0..4 so the new one will get 5, right?
Clients have their user names so basing on ConnectionList, I could have some list linking ConnectionList's client number to the user name but...
Yes it is a simple TFPGList used. When a client connects it is added to the list via .Add at the end of the list and if it disconnects it is removed via .Delete.

3.
Quote
"when a client disconnects, it changes the indices of all clients connected"
Is there any event going on in this situation? how can I know that someone disconnected to update my list?
The list is automatically updated. To be notified on a disconnect you can register an event handler. In the ChatServer example this is done in:
Code: Pascal  [Select][+][-]
  1.     ACommunication.OnClose := @ConnectionClosed;

r.lukasiak

  • New member
  • *
  • Posts: 7
Re: LazWebsocket How to identify IP adress during connect
« Reply #8 on: October 15, 2021, 12:30:26 am »
Quote
TSocketHandler = class(TThreadedWebsocketHandler)
ok, got it.

Quote
The list is automatically updated. To be notified on a disconnect you can register an event handler. In the ChatServer example this is done in:
of course... :| I don't know what I was thinking of when asking this question. I even use that event to do some stuff. What I really wanted to know is how can I figure out which client disconnected, is there any way get its number in ConnectionsList?

And there is another question about OnConnectionClosed. I testing the server using my gui and the HTML example. The server notices when the HTML client disconnects, whether I use the disconnect button, refresh the page or even kill the browser process, the server always triggers the OnConnectionClosed event but it doesn't happen with my GUI, whether I close it or just kill the process, the server doesn't notice it. How could I notify the server I disconnect?

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: LazWebsocket How to identify IP adress during connect
« Reply #9 on: October 15, 2021, 05:21:07 pm »
of course... :| I don't know what I was thinking of when asking this question. I even use that event to do some stuff. What I really wanted to know is how can I figure out which client disconnected, is there any way get its number in ConnectionsList?
Currently there is no nice way of getting it, you basically need to iterate that list and search for it

And there is another question about OnConnectionClosed. I testing the server using my gui and the HTML example. The server notices when the HTML client disconnects, whether I use the disconnect button, refresh the page or even kill the browser process, the server always triggers the OnConnectionClosed event but it doesn't happen with my GUI, whether I close it or just kill the process, the server doesn't notice it. How could I notify the server I disconnect?
Thats because you don't perform the closing handshake of websockets, but rather break the TCP connection (which will result in an exception).

To close the connection gracefully just call "TWebsocketCommunicator.Close" and wait until TWebsocketCommunicator.Open is false

r.lukasiak

  • New member
  • *
  • Posts: 7
Re: LazWebsocket How to identify IP adress during connect
« Reply #10 on: October 20, 2021, 06:18:01 pm »
Quote
To close the connection gracefully just call "TWebsocketCommunicator.Close" and wait until TWebsocketCommunicator.Open is false
worked perfectly!

Quote
Currently there is no nice way of getting it, you basically need to iterate that list and search for it
I see... anyway I managed to somehow solve it.  I dug through your code to find out how you implemented ConnectionList and what info it exactly stores.
I created something like:
Code: Pascal  [Select][+][-]
  1. ConnectedUser = record
  2.   UserName: string;
  3.   Handler: TWebsocketCommunicator;
  4. end;
  5. .
  6. .
  7. .
  8. var
  9. ConnectedUserList = array of ConnectedUser;
  10.  
OnConnect I add the client to the list and the client after connecting automatically sends some kind of "hello message" with its username so the server adds it to the list.
So far so good, thanks a lot!

I tried to use these libs with LAMW for Android and I came across a problem with:
Code: Pascal  [Select][+][-]
  1. FCommunicator.OnClose:=@StreamClosed;
  2. FCommunicator.OnReceiveMessage:=@ReceiveMessage;
The compiler (for aarch64) is giving me:
Code: Pascal  [Select][+][-]
  1. unit1.pas(138,26) Error: Variable identifier expected
changing it to:
Code: Pascal  [Select][+][-]
  1. FCommunicator.OnClose:=StreamClosed;
  2. FCommunicator.OnReceiveMessage:=ReceiveMessage;
helps to compile but it's obviously causing some problems.
I ran the app on my phone and it connected to the server, server showed info about the connected client but the app crashed immediately.

Have you tried it with LAMW? any hint?

« Last Edit: October 20, 2021, 10:39:16 pm by r.lukasiak »

 

TinyPortal © 2005-2018