Subdigger
Subdigger

Reputation: 2193

TTcpServer remote connection to service

i need win XP service with TTcpServer.

application was created by "File->New->Other->ServiceApplication"

TTcpServer.localport := 33000

server registered with exename.exe /install

everything looks good, even netstat -a shows that port 33000 - LISTENING

but i can`t access that port from outside of this machine. only local.

and when i make the standard application with same params - all ok. TTcpServer config

EDIT1 TTcpServe.OnAccept =

procedure TFlexorXL.tcpServerAccept(Sender: TObject;
  ClientSocket: TCustomIpClient);
  var str: string;
begin
 if ClientSocket.Connect then
 begin
    str := ClientSocket.Receiveln;

  ClientSocket.Sendln('test');
               //ClientSocket.Disconnect;
  end;
end;

Upvotes: 0

Views: 1159

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595897

TCP/IP works just fine in a service (I use it all the time), so you are likely just misusing the TTcpServer component (which is possible, because it is a horribly written component).

If the TTcpServer.LocalHost property is blank then the socket will bind to all available local IPv4 addresses, otherwise it will bind only to the particular IPv4 address that you specify (netstat will show you the actual IP that the server is actually bound to). That is the IP that you must have clients connect to. In the case of 0.0.0.0, you can connect to any IP that belongs to the server's machine.

With that said, in order to actually accept clients, you must either:

  1. set the TTcpServer.BlockMode property to bmThreadBlocking. The server will then use an internal worker thread to accept connections, and each client will run in its own worker thread. However, you must perform all of your client-related logic inside of the TTcpServer.OnAccept event, because a client will be disconnected immediately after that event handler exits.

  2. for any other value of BlockMode, you must call TTcpServer.Accept() yourself, such as in a timer or thread. If you call the overloaded version of Accept() that has no parameters, you must perform all of your client-related logic inside of the TTcpServer.OnAccept event, because the client will be disconnected immediately after that event handler exits. If you call the other overloaded version of Accept() that returns a TCustomIpClient object, then you control the lifetime of that object and can use it however you need.

With that said, if you are doing all of that, and still having problems, then you need to provide more information about your actual TTcpServer setup, show some actual TTcpServer code, etc. As it currently stands, you have not provides enough details to diagnose your problem.

Upvotes: 2

Related Questions