Recent

Author Topic: TClientSocket and TServerSocket  (Read 15079 times)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #15 on: January 08, 2019, 10:30:37 pm »
You are trying to run a server that listens on a TCP/IP port that it does not have access to.  For instance, on 'Nix systems, ports < 1024 are restricted to root users only.  Windows does not have that restriction.  Your example is using port 23.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #16 on: January 08, 2019, 10:43:40 pm »
I'm using Linux. Can you suggest a proper port number? Is there any way to detect the available ports (sorry, if it was a silly question, but I don't know it)?
To be honest, I don't know if ports can be any value, or they are restricted to certain numbers?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #17 on: January 08, 2019, 10:56:23 pm »
I'm using Linux. Can you suggest a proper port number?

Any port >= 1024 that is not already in use by another app.

Is there any way to detect the available ports

Not available ports, but you can detect in-use ports using netstat.

To be honest, I don't know if ports can be any value, or they are restricted to certain numbers?

Ports can be any value between 0..65535, but many ports (especially the ones < 1024) are registered for specific uses (like 21 for FTP, 23 for Telnet, 80 for HTTP, etc), so they MAY OR MAY NOT be in use on your system.  See List of TCP and UDP port numbers on Wikipedia.

Your app can use any port that is not in-use on the system, and that it has access to use.
« Last Edit: January 08, 2019, 10:58:08 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #18 on: January 08, 2019, 11:05:00 pm »
Thank you again. It seems it is not my day.
I gave 5000 as port number, ran the program, and newer error came:
RunError(232)
« Last Edit: January 10, 2019, 01:03:26 pm by justnewbie »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #19 on: January 09, 2019, 01:35:02 am »
Run-time error 232 is:

Quote
Threads not supported

Thread management relies on a separate driver on some operating systems (notably, Unixes). The unit with this driver needs to be specified on the uses clause of the program, preferably as the first unit (cthreads on unix).

Add -dUseCThreads to the usage options of indylaz.
« Last Edit: January 09, 2019, 01:37:16 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #20 on: January 09, 2019, 11:11:02 am »
Run-time error 232 is:

Quote
Threads not supported

Thread management relies on a separate driver on some operating systems (notably, Unixes). The unit with this driver needs to be specified on the uses clause of the program, preferably as the first unit (cthreads on unix).

Add -dUseCThreads to the usage options of indylaz.
OK, but how?
I think this is the proper place (my picture), but I don't know the exact step.

Update: I added it to the Custom field, compile and voilà!
Seems to work now!
Remy, thank you for your endless patience and help!
« Last Edit: January 09, 2019, 11:34:01 am by justnewbie »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #21 on: January 09, 2019, 02:23:21 pm »
The communication between server and client works well!
I would have a new question: if I close the client-side, I get a message: "Connection Closed Gracefully". I think it is not so good.
How can I resolve this, ie. how should I close the connection (on both side) properly?
« Last Edit: January 09, 2019, 02:26:59 pm by justnewbie »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #22 on: January 09, 2019, 08:17:31 pm »
if I close the client-side, I get a message: "Connection Closed Gracefully". I think it is not so good.

That is perfectly normal behavior:

Why do I keep getting EIdConnClosedGracefully exceptions?

Connection Closed Gracefully

Let it happen.  On the server side, TIdTCPServer will handle the exception for you and close down the client connection and its managing thread.  You will see the exception only if you are running your server app inside of the debugger, your code will not see it unless you catch it manually in a try/except block or in the server's OnException event.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #23 on: January 09, 2019, 09:36:08 pm »
Yes, I ran it within the IDE. Now, it is OK, understood. Thank you!  :)
One more silly question: after the connection/disconnection (based on this code), do I have to do anything with the port?
I mean, does it remain "open" or such thing doesn't exist?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #24 on: January 09, 2019, 09:42:03 pm »
after the connection/disconnection (based on this code), do I have to do anything with the port?
I mean, does it remain "open" or such thing doesn't exist?

A port is closed when all sockets that are using the port have been closed.  In a TCP connection, there are 2 ports involved - one on the client side, and one on the server side.  When a client wants to connect to a server, it opens and binds itself to a local port and then connects to the server's listening port.  When the client disconnects, the server closes the socket it created to accept the client, but the server-side port remains open while the server still has a listening socket open to accept connections.  The client-side port is closed when the client closes the socket it created to connect to the server.

I think you need to get yourself a good book/tutorial on how network socket programing actually works.
« Last Edit: January 09, 2019, 09:43:58 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #25 on: January 09, 2019, 10:09:10 pm »
I think you need to get yourself a good book/tutorial on how network socket programing actually works.
So true, agreed! :)

Thank you, Remy!

 

TinyPortal © 2005-2018