Question

Indy Client Server freezing

Asked by: praxalia

Hi experts.

I am having a really hard time to solve an Indy10  communication problem. I have a client-server project which uses TIdTCPClient and TIdTCPServer to send files across the network (LAN). The application must be able to send files in both directions: client to server and server to client. At the moment I am testing server to client part. I send files using fixed length chunks.

Sometimes the communication loop will freeze at a random iteration in the line where the client confirms  reception of the chunk (it writes 1 byte response to the handler) . For testing I am using Windows XP SP3 file (313 MB) because with large files the problem is more likely to happen.

Some important facts you must know before answering:

- I have downloaded latest version of indy 10 available from SVN, few weeks ago.
- Server and Client sending loops are out from main VCL thread.
- I have IdAntifreeze components on both sides
- I have tried differents types of reading/writing to the IOHandler: ReadByte, ReadStream, ReadLn...
- I use MadExcept and exhaustive logging to find out things, so I am quite sure that the read instruction in the server does not exit, but the client does write the response byte.


Does anyone know a reason why the TIdTCPServer does not receive the client response while the TidTCPClient skips to the instruction after the write? They are suposed to use blocking sockets... It does not make any sense to me.

Any help will be greatly appreciated.

PS: I may not be able to read this post until monday, but i just could not wait.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-17 at 09:03:24ID24332262
Tags

Delphi

,

Indy 10

,

TIdTCPServer

,

TIdTCPClient

,

Freeze

,

Read

Topics

Delphi Programming

,

Delphi IDE

Participating Experts
4
Points
500
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. While not EOF... ReadLn... exhausts system memory
    I have exhausted my ability to debug this problem. Perhaps an 'expert' can pinpoint the programming flaw? All I am doing is reading a 380 megabyte text file, line by line. Somewhere around the 21 megabyte point, the Delphi 3 application starts exhausting all Windows physic...
  2. Indy - Making a game Protoptic
    Okie, I have a lil Web Game. It is a Flash - ASP - SQL game and now i want to migrate it to a Client - server - SQL game. All doing "fine" till now but... i have some questions and i will be glad if u guys help me a bit. Delphi 7 Indy 9 since its a game protoptic ...
  3. Problem with Indy components
    Hello All, Somebody can tell me link where I can download sample source code for 100% peer-to-peer chat which uses Indy components? Thanks.
  4. Indy threaded server (canvas does not allow drawing...)
    Hey i have a clienter server app created using indy. I need to be able to create a form at some point when the server get a command at the Onexecute event. But because its threaded i get this canvas message i have found out. I have tried to put the form in a dll, this makes...
  5. I need a TCPDemo for Indy 10
    I need a TCPDemo like in Indy 9 for Indy 10 or tell me where can I find it.
  6. Upload with Indy
    I need the code to upload with Indy. If possible I want to upload a TStringList without saving it in a temp file ? Thanks

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: JohnjcesPosted on 2009-04-17 at 11:24:05ID: 24170552

One thing you might try is using two different ports instead of one. Server receives on port 10001 and sends on 10002 and clients send on 10002 and receive on 10001.

"Sometimes" I have seen that transmission of TCP/IP packets on the same port number has caused some conflicts.

Might help, might not, but worth a try.

John

 

by: ThievingSixPosted on 2009-04-17 at 22:22:17ID: 24173927

I would highly recommend moving back to Indy 9 for all around stability. When using Indy 10 I have come across some problems that switching back never had.

 

by: praxaliaPosted on 2009-04-18 at 04:52:54ID: 24174913

Hi Johnjces.

Can server and client use two different ports for sending and receiving? Didn't know that, I thought each connection was binded to one specific port. Could you please send me an example? Thanks.

 

by: praxaliaPosted on 2009-04-18 at 05:03:00ID: 24174945

Hey ThievingSix, thanks for your comment.

Yes, in fact I have thought about going back to Indy 9 times, but I 'd rather prefer to stay in indy 10 as long as possible. I may want to ensure unicode compliance to my project later on or even move it to Delphi 2009 (now I am using Delphi 2006). In my opinion, going back to indy 9 (which I think is being barely maintained right now) could throw me into more problems in the short term.

 

by: JohnjcesPosted on 2009-04-18 at 11:07:05ID: 24176184

Try it! You have separate client and server components and it can work.

 

by: praxaliaPosted on 2009-04-19 at 02:42:24ID: 24178449

Hi Johnjces, I talked about using the same TIdTCPServer and TIdTCPClient, of course I know they are separate components ;).

The point is that I do not want to use more than one TIdTCPClient and TIdTCPServer in each module, that would be the last option. I need to keep the project as simple as possible, experience has showed me that debugging multithreaded projects can be absolutely discouraging and time consuming. Two different connections on both sides means lots of extra code: paired connection and disconnection events, two different timers to reconnect sockets in case of failure, two synchronized server threads for each client connection besides the main VCL thread... There's too much code attached to one single connection to think in handle two of them per client, at least at the moment. If there would be a way of using two ports in one connection that would be fine, but I understand that this is not possible, by definition. Thanks anyway for your suggestion.

What I really need to know is why does a blocking socket get data in the client side which does not come out in the server side.

I am working on a simplified project which will show the problem. Tomorrow I will have it ready. If you need the code, just ask me.




 

by: praxaliaPosted on 2009-04-21 at 09:36:48ID: 24196312

Hi experts. following the principle: no news = bad news, I have just finsihed a simple client-server version where the freezing arises. The problem is that EE won't allow me to upload the zip archive with the project files, and even though the project is small, the code is too long to fit as a comment.

How can I publish the code in a civilized manner? Didn't find the answer in EE help or FAQs. Thanks

 

by: TheRealLokiPosted on 2009-04-24 at 01:56:55ID: 24222947

use http://www.ee-stuff.com
you can put EE attachments there, and it will make a link that you can paste here
I do it all the time
If you are unable to do that, then email it to me (my address is in my profile) and I will put it on EE stuff for you, for all to see. This should still be within the EE rules.

As for your problem, I have various demos that do what you are asking, but we will modify your code if that is what you prefer.
FWIW, sending the "length of the stream", followed by the stream, is the way to go (imho)

Even though indy uses blocking sockets, TCP is still a slightly random beast
hardware and the like can change things...
not like UDP, but for example, you may send 1 "packet", but it may arrive as several packets, and even have large delays between packets.
Even though Indy 10 is a big change from Indy 9, I can only re-iterate what Chad Z Hower (author of Indy) told me.
I was having problems with Indy 10, and he suggested I download the latest "development snapshot", not the official release. This was a while ago now, but it certainly contained many fixes to the official release.
Give that a go.
Looking forward to seeing your code...

oh, and please tell me what version of Delphi you are using. That will make a big difference to us.

 

by: praxaliaPosted on 2009-04-24 at 01:57:34ID: 24222954

Hi experts. I just uploaded the project files so you can test them and see the problem. I have tried to make it easy to test: connection can be done on any port (it defaults to 9997), and to any host name, without having to retype the code. I have translated screen messages and made an effort to remove unimportant things. You should try to send a big file over the lan (I use a 300 Mb file), because it may not freeze with small ones.

http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=7612

Thanks

 

by: praxaliaPosted on 2009-04-24 at 02:16:07ID: 24223047

Hi TheRealLoki, thanks for your tips. I received an administrative comment telling me how to upload files to www.ee-stuff.com, so I have just uploaded them. Thanks for your offer, anyway.

As you say, I would rather prefer you having a look at my code. It could be that the origin of my problem was in a particular way I am using threads or indy components, so I prefer to get solutions which will apply straight to my project.

I do use the "length of stream followed by the stream", approach, to send the chinks. In fact I have tried several different ways, but this is the one that remains.

About having the latest snapshot available, I did install tortoise svn client to download latest snapshot of indy 10 available on the trunk folder (following instructions found in indy project's page). This was three weeks ago, more or less. If you can run the project without freezing, I assure you that I will download the latest snapshot straight away.

I am using Delphi 2006 Professional, BTW.

 

by: praxaliaPosted on 2009-04-28 at 01:02:35ID: 24248468

Hi there, TheRealLoki. While you were looking at the code I downloaded the latest snapshot from Indy 10 and installed it (after uninstalling current version completely). The problem remains the same, it still freezes. If you cannot find the time to look at my code, please send me one of the working examples you mentioned above of sending files in chunks using indy10, so I can compare them with my project. I will add differences one by one to see what is that makes it crash. I am really stucked now with this issue. Thanks.

 

by: TheRealLokiPosted on 2009-04-28 at 21:36:17ID: 24257364

sorry, I've been off work, so havent checked EE.
It's probably a thread issue, but I''ll see if I can reproduce it on my home dev pc
I'm concerned about the way you use the TIdContext.
Give me a few hours to try and get my dev pc up :-)

 

by: TheRealLokiPosted on 2009-04-29 at 18:20:30ID: 24266203

Your code locks up here also
My demos are up on
http://sourceforge.net/projects/internetdemos
use any of the indy 10 demos, and look for CMD_FILE or TFileStream

I generally send a "FILE" command followed by the filename and the size
then the actual data

 

by: praxaliaPosted on 2009-04-30 at 03:48:54ID: 24268498

Hi  Loki,

I downloaded Indy 10 client and server and I'm trying to compile them. In both server and client I get some strange "Incompatbile types integer and Int64" compiler errors, in the onConnect event, where you assign OnWork, and OnWorkBegin connection events. I am looking at it at the moment.

When I get to compile your projects I will try to apply differences with my project, one by one, so to see which one will lock things up. First things I see different:

- You create the TIdTCPclient at runtime
- You send the file in a single write instruction.
- TCP dialog is started by a client's command
- You use Synchronize in the client, to call VCL functions from the thread (I use windows messaging, like you do in the server).

Maybe I left someone out, it's just a first inspection.

I will tell you how I get on with this. If you discover the origin of the freezing problem meanwhile, please tell me.

Thanks.

 

by: praxaliaPosted on 2009-05-05 at 03:48:43ID: 24303229

Hi Loki. First of all, my apologies, i could not work on your demos until this morning. We had a 4 day weekend here.

It looks like I am close to isolating the problem, now.
Using your freezing-free client/server demo as a basis, I began to add changes pasting pieces of my code into your project.

1) Made server send the file to the client in the OnConnect event: Worked OK
2) Sent the file in chunks: OK
3) Added a button in the server to send the file at any time, using a custom created thread: FREEZED!!!

So, the problem arises when I try to use my own custom thread to send the file from the server, instead of using the thread spawned by the TIdTCPServer. The problem is that, as sending the file to the client will be the user's decission, I must be able to send the file at any time and I cannot do it without spawning a thread outside indy's. Also, I do not want to freeze users interface with lengthy file operations, so I cannot use the main VCL thread either.

The custom thread is the most simple thread I can imagine, I just create it, resume, and it does not even have a execution loop (I wanted to make it as low CPU time consuming as possible), just sends the file and terminates. The code to send the file is the same thread-safe procedure I use from the onconnect event.

I have uploaded the project to ee-stuff. You will see a checkbox and a button. With the checkbox enabled, it sends the file as soon as the client connects using indy thread (works OK). If you use the button, it creates a new thread and sends the file (Freezes).

If you see where is the problem, tell me. I am working in it right now.

Thanks.

 

by: praxaliaPosted on 2009-05-06 at 09:23:55ID: 24316693

I have been trying around things and this is what I have seen:

1) If I use threads spawned by TIdTCPServer in the OnConnect or the OnExecute events, the file is sent OK. If I use OnExecute, I can activate a boolean flag in the main form when I click a button and test it inside the event. This is a partial solution because I can respond to a user action to send the file.
2) Whenever I use a custom thread it might interfere with indy's own listener threads and at some point my thread gets blocked.

Using the onexecute thread implies that the onexecute event won't be triggered again for any other client while the file is being sent? This would be an important drawback, as the server can have several connected clients, all of them blocked until the operation with a single client is finished.

I think that the neatest solution would be using my own thread, but at this point I don't know what else I can do. I already tried different thread's priorities, changed the way I store the IdContext, tested TIdThread instead of TThread... with no results.

Any ideas?

 

by: TheRealLokiPosted on 2009-05-07 at 12:31:14ID: 24329638

Using the onexecute thread implies that the onexecute event won't be triggered again for any other client while the file is being sent? This would be an important drawback, as the server can have several connected clients, all of them blocked until the operation with a single client is finished.
This is incorrect. Indy's Execute event fires for EACH client connection. so you may in fact have multiple Execute threads running at the same time.

 

by: praxaliaPosted on 2009-05-15 at 04:28:15ID: 24394242

Sorry for the delay. I have been testing the demos...

First of all, you're absolutely right about OnExecute triggering. It triggers for each client independently, I made a bad reading of documentation. This helps me greatly because I can use Indy's own threads to control de TCP flow in a safe manner. I guess I will have to retype the code to move all TCP commands inside the OnExecute thread in the main form. I tested PostThreadMessage to notify server's thread about starting operations, and it works fine.

It would be great if I could just understand why using a different thread to access TIdTCPServer connection ends up in unpredictably freezing. I made it all really simple: I protect socket access with critical sections and it should be all thread-safe! Could it be a bug somewhere in indy's thread management? I couldn't find any example in the web which used a custom thread to read/write to a server. I even found a post somewhere that recommended (without further explanations) to use indy connections always from the same thread spawned in the OnConnect event, even though synchronizing objects could be used to protect access from other threads.

If no one comes with an explanation to this problem I will award you the points. Your demo code was useful to clarify things.

 

by: TheRealLokiPosted on 2009-05-17 at 01:23:05ID: 24405384

Indy uses a TidThread which has a few extra methods. I expect using your own TThread means that you can't inherently call some of the TiDThread's OnExecute code which probably handles some other syncing that isn't obvious before the published Execute method.
e.g.
Indy "execute"
 "do indy stuff"
 if assigned(fOnExecute) then fOnExecute(self, AThread);
 "do other indy stuff"
end;

I still do a lot of guess work when it comes to Indy

I have done inter thread communication on Indy using TCriticalSection, but it probably wouldn't have been a good match for your situation.

 

by: praxaliaPosted on 2009-05-18 at 06:22:20ID: 24411763

Yes, that makes sense. I even tried with my own TIdThread, just in case the origin of the problem was on synchronizing between a TThread and a TIdThread, but it didn't work either.

As I said, I award you the point for your response with the demo. Thanks for your help, guys.

 

by: DKropf3Posted on 2009-12-07 at 09:15:44ID: 25990745

Hi Praxalia,

i think I have the same problem as you did.
Did you ever solved this?

Thanks!

 

by: praxaliaPosted on 2009-12-08 at 08:26:16ID: 25999492

Hi DKropf.

Yes I did solve the freezing issue. What I discovered is that, for some reason, TIdTCPServer socket is only safe  to use inside the Onexecute thread of the TIdTCPServer. My problem arosed from the fact that I called socket operations (read and write) from my own custom thread. The program freezed randomly at these I/O operations no matter which blocking system I used (semaphores, Critical sections, signaled events...) to avoid conflicts with Indy's own thread. At last, everything worked fine when I moved all reading/writing to the socket inside the OnExecute loop.

Hope it helps,

praxalia

 

by: DKropf3Posted on 2009-12-08 at 12:56:11ID: 26002285

Hi Praxalia,

thanks for answer, but i solved it to. :)

My problem was bizarre! Calling .Connected from different threads without some blocking mechanism will interfere with buffers and on some ocassions add trash to buffer ... Spent a month searching ... :/

Thanks anyway!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...