Solved

Indy TcpClient and TcpServer

Posted on 2002-07-19
17
2,471 Views
Last Modified: 2008-03-10
I have problems because i would really like an event on the TcpClient that would get fired when a new message arived. But there is not a event for that, The Server has the onExecute event wich works well, but when i send message to client there need to be an timer that checks like:
Msg := IdTCPClient1.ReadLn;

But then i thought that the events that client have, OnWork, OnBeginWork, OnEndWork could be used, my plan was to send a stringstream from server, the help also says that the OnBeginWork will get fired if u use SendStream(), but it just isn't.

I gues the only way is to do the stream stuff, but i am just not sure how. Someone said that the client events need to be assigned first? hmm

Well hope someone can help me on this, i am requesting a normal event on the tcpclient like the OnRead that the TClientsocket have.

Thanks.
Dennis
0
Comment
Question by:Dennis9
  • 9
  • 8
17 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
you need to check for new data constantly
you could create a new thread that checks for new data periodically
here's my implementation of a thread that checks for new data with TidTCPClient
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/MiscFiles/vn_common/lnVidTypes.pas

find the TCommThread implementation (at the end of the page)
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
Lee_Nover -> can u not explain a bit more on how you would do so? I don't totally understand you.

Ask if u need more information on my problem.

Dennis
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
well if you take a look at my TCommThread  Class then there is not much to say

you could simply make a descendant of TlnThread, override the Run method and in it do smth like:
Msg:=TCP.ReadLn;
if Msg <> '' then
begin
  // handle the message
end;

it would be best if Msg was a global var in the new thread
for synchronizing I use SendMessage
if you still need more explaining I'll write an example :)
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
well for 200 points i almost expect a sample :)

Dennis
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
:)
the thing is you got the whole code .. almost
well I'll make that example for you :)
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
ok I've made an example for you
here's the complete project :
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/projects/TlnThreadSubclassing

this is the basic idea how to use the TlnThread class
it can be very usefull for things like communication
if you need more help just ask
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
it seems like your server is down at the moment but i will just wait then :)
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
sorry bout that
some kids been playing arround with dosnets !!!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Dennis9
Comment Utility
i am very impressed about your code, it also works pretty well :)

But i am having problems receveing to the that the server sends in the connect event. Ok let me explain it a bit more.

Client connect.
Server send some text back in the OnConnect event.

But the client app does not receive it all, first when i send back a line using client it receive the text send from the servers onceonnect event. I don't know if its possible to fix this, if not i am happy enough about the code i have now.

Thanks Lee_Nover
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 200 total points
Comment Utility
aha I know this problem
I have it also !!!
it's due to the buffer not being full yet and Indy TCP won't send the message until the buffer is full
I really havne't overcome this problem though it seems very easy too
I tried setting the buffer size to the length of the message, i does help a bit but not always
I also use WriteBuffer with WriteNow set to TRUE
you could try sending the text like this:

uses IdTCPConnection, IdGlobal;
function SendLineNow(TCP: TIdTCPConnection; const Text: string): Boolean;
begin
     try
        TCP.WriteBuffer(PChar(Text + EOL)^, Length(Text + EOL), true); // true to send the buffer immediatelly
        Result:=true;
     except
        Result:=false;
     end;
end;

usage:

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
     SendLineNow(AThread.Connection, 'Hello user from ' + AThread.Connection.Socket.Binding.IP);
end;


about the code .. hehe tnx :)

hope it helps

0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
yup your SendLineNow procedure seemed to fix my problem.
Thank you very mutch again, u been a big help :)

If i get into more problems wih this socket i hope u will be there.

See u around Lee_Nover
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
Lee_Nover -> have u found a way to fix the send crap?

If i put a button on client that sends some text to server, and press it 5 times. server will only receive half of it why?

Maybe u can help.

Thanks

dennis
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
don't know how you implemented it
did you use the Queue method as in my TCommThread ?
if you have all the text should go through
to queue the text takes a fraction of the time it takes to send it
that's why I queue the commands, and send them 'later'
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
I just used your client thread code. Does more need to be done
?
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
hum .. I don't have such problems
do you use QueueCommand method ?
0
 
LVL 1

Author Comment

by:Dennis9
Comment Utility
what do u mean ? use the QueueCommand ?
I have not seen anything named QueueCommand. Can u not tell more about it?
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now