Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie console app question

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
I am trying to develop a console app which listens for information on a network (using synapse) and put's it into a database. I am having difficulty get the app to stay open and listen for traffic. I have tried using the echo demo. I have tried adding 'readln' but I still can not get 'echo' to listen.
What should I do?
Find the following snippets of code:
collex.lpr
==========================
uses main;
begin
TTCPEchoDaemon.create; //readln;
end.
=========================

main.pas
========================
Constructor TTCPEchoDaemon.Create;
begin
sock:=TTCPBlockSocket.create;
FreeOnTerminate:=true;
inherited create(false);
end;

Destructor TTCPEchoDaemon.Destroy;
begin
Sock.free;
end;

procedure TTCPEchoDaemon.Execute;
var
ClientSock:TSocket;
begin
with sock do
begin
CreateSocket;
setLinger(true,10);
bind('0.0.0.0','4515');
listen;
repeat
if terminated then break;
if canread(1000) then
begin
ClientSock:=accept;
if lastError=0 then TTCPEchoThrd.create(ClientSock);
end;
until false;
end;
end;
==============================
Any Ideas

 
So you're wanting the console to stay open and do something until you want it to quit?

Basically as I understand it, you'd need to create a child thread with the code like you have (hopefully looping, and listening for packets), and then loop in the main thread (with Application.ProcessMessages) until there's a terminate signal either from your child thread or from your main thread (a cancel signal).

You might want to clarify what you're doing, but if that's similar, you might try just creating an application like I describe that just runs until you press a key, or the like.
 
OK I got the following working in a fashion. I get a connection the data is
processed, but if I disconnect and try and connect again a socket is not
available. What do I do now?
========================
begin
sock:=TTCPBlockSocket.create;
with sock do
begin
CreateSocket;
setLinger(true,10);
bind('0.0.0.0','4515');
listen;
ClientSock:=accept;
socket:=ClientSock;
GetSins;
writeln('Connected');
repeat
s := Recvstring(200);
SendString(s);
DataParse1(s);
until false;
end;
Sock.free;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top