Solved

Need to synchronize two sockets!

Posted on 2004-04-30
29
406 Views
Last Modified: 2010-05-18
Hi All,

Problem is:

I have two server sockets, some clients is connected to these sockets. When first sockets should be close I need to close second socket also. I'm using this code for it:

procedure TMainForm.FirstSocketDisconnect(AThread: TIdPeerThread);
begin
  FSection.Enter;
  try
    StopSecondSockets;
  finally
    FSection.Leave;
  end;
end;

procedure TMainForm.StopSecondSockets;
begin
  if(FUseSeats=0)then begin
    CpWatcher.DoProcess(False);
    try
      SecondSocket.Active:=False;
    except
    end;
  end;
end;


But problem is: I always gets error message 'Terminate thread timeout' when first socket is trying to close second socket.

As I understand it happens because second socket has some connections.

What should I do? Should I manually disconnected all active connection or what?


Also, this code works in the thread and for closing second socket software needs to get some time, but in the same time first socket is already disconnected becuase first thread is still working.

Need to get solution how can I synchronize two or more sockets or some parts of source code with it.

Please does not provide me Indy examples, I know it very well.

Thanks.
0
Comment
Question by:dosware
  • 16
  • 12
29 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 10956919
You ask that we not give Indy examples yet you use (AThread: TIdPeerThread) as a parameter. Does this mean that the solution MUST work within the Indy sockets framework? There could be other and possible better solutions.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10956930
Rather than using two socket connections, perhaps you should use one with an underlying application-layer message protocol. Then you have only one connection to manage.
0
 

Author Comment

by:dosware
ID: 10956931
I'm using TIdTcpServers and TIdTcpClients. It means that I need solution for code which I wrote below.
0
 

Author Comment

by:dosware
ID: 10956942
Dear Sir,

I need to transfer 6 stream with different types of data by this sockets.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10956958
I don't know enough about your context to make a strong statement, but if you are making a TCP connection for each type of data then you are proably introducint too much complexity. You might better pass a message on one connection which defines which of the 6 protocols is contained in the message, perhaps with a Pascal record-case variant, with a field defining what follows.

e.g.,
type
  message = record
                      msgType : Integer;
                      case x : msgtype of
                         0 : (); ...
                         1 : (); ...

etc.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10956962
If you need to process these message asynchronously at an application layer, you could use local concurrency mechanisms to pass messages to multiple local threads.
0
 

Author Comment

by:dosware
ID: 10956968
Dear Sir,

I can not use this type of code because I need to transfer huge streams. And in your variant of solution I will get many problems than now.
0
 

Author Comment

by:dosware
ID: 10956971
Dear Sir,

I need to synchronize two or more sockets. One control socket should close all data sockets. And all. I do not need other solutions.

Thanks.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10956981
Putting the multiplexing under application-level control seems to me to be a better idea but I defer to your constraints. Seek the more complicated answer if you wish.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10956992
Just a small point of clarification, please - do all of the data sockets originate from the same place?
0
 

Author Comment

by:dosware
ID: 10956995
Dear Sir,

Real help which you can give me is a working example.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957016
dosware, in order to provide a working example, I would prefer to understand the underlying problem rather than the local symptom. I think you may have introduced too much complexity into the solution, but that depends, of course, on the physical constraints. If the data sources are physically separate, then multiple sockets are needed but otherwise a simpler solution would be to introduce multitasking at an application layer rather than a connection layer.
0
 

Author Comment

by:dosware
ID: 10957046
No problem.

Step-By-Step:
1. I have two server socket and two thread managers in the server side;
2. I have two client socket and two threads in the client side;
3. Client socket A is connected to server socket A;
4. Client socket B is connected to server socket B;
5. Server socket A and server socket B can have unlimited count of connections;
6. I need to close server socket A and B in same time properly.
7. Close process for server sockets A and B should initiate disconnect process for client sockets A and B;
8. It should not generate exception 'Terminate Thread Timeout' for server socket B.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957061
Is there a one-to-one mapping? I.e., for every A connection there is a single B connection, and vice-versa?
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!

 

Author Comment

by:dosware
ID: 10957075
Server Socket A
Thread Manager
  |
  |
  +--- Thread for client socket B (Software #1)
  +--- Thread for client socket B (Software #2)
  +--- Thread for client socket B (Software #3)
  +--- Thread for client socket B (Software #4)
  +--- Thread for client socket B (Software #5)
  + ...
  ...

Are you understand client/server technology?
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957164
Yes, dosware, I understand client-server technology and I have been doing parallel communicating systems (including thousands of concurrent threads of communicating processes for at least 20 years). Click on "sftweng" above to get some background if you wish.

I am very fond of the CSP architecture originally proposed by Tony Hoare (Oxford). It is simple. It is robust. It can be proven to be deadlock-free.

That is why I am asking fundamental questions rather than concentrating on the specifics about Indy implementations.

I would still like to know whether your clients are physically separated and whether "A" and"B" could communicate over the same socket.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957179
Looking closer, do I understand correctly that you want to close all client sockets given some global error condition? Or just close one client connection?
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957192
dosware, I need to read your earlier comments more carefully. I also need to get some sleep and I won't be able to get back to this until much later today. I hope that others in this forum will contribute in the meantime. Our interest is to solve your problem!
0
 

Author Comment

by:dosware
ID: 10957337
Dear Sir,

Thank you very much for your comment, I have fixed this problem.
0
 

Author Comment

by:dosware
ID: 10957428
Dear sftweng,

Maybe you can help me in other way...

See:

I have N video cams with hight resolution (11Mp), one picture has size more than 1Mb. And it works in REAL TIME. I need to get streams from all video cams and send it to ONE server where I need to use two sockets: 1-control socket, 2-streams socket.

Also I have N audio devices with high quality which send me audio streams in REAL TIME also.

How can I get video and audio streams thru one socket? How fast it will work? I'm living in Ukraine but I should get streams from USA, i.e. sending will get some time for reading data.

Are you understand me? What solution can you tell me? Real example?
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957511
Thanks, dosware, for describing the problem. BTW, I am a half-Ukrainian Canadian so I am pleased to be able to help.

Your description opens up many possibilities. One of the first that I would recommend is to look at the Direct-X components, which provide many interfaces for video and audio streams. There are a nmber of Delphi APIs that can hook in.

I can understand that packet latency would be an issue for you. Image and audio compression certainly come into consideration.

I hope your server has a lot of processing speed and the ability to handle high communications bandwidth. Even with compression, you face a challenge. Audio should be easy but video not.

Elsewhere in this topic, I examined frame-by-frame difference calulations to consider movement. Is that an important issue for you?
0
 

Author Comment

by:dosware
ID: 10957534
Yes, server has T3 line and video cams gives me JPEGs, i.e. I can not pack it :) By the way, what is it Direct-X, where I can found it?
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957586
You could starte here: http://turbo.gamedev.net/delphix.asp
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957613
If you do not get upset at speaking with your neigbors to the north, you might like to see http://www.gamedev.narod.ru/
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10957637
0
 

Author Comment

by:dosware
ID: 10957649
Thank you bud, also probably you have some URLs where I can read more about Indy, about TIdThreadMgr and etc...
0
 
LVL 7

Assisted Solution

by:sftweng
sftweng earned 200 total points
ID: 10957681
Hey, dosware, I need some sleep! My business day starts in 3 hours and I have had only 2 hours of sleep so far.

Perhaps later. :-)
0
 

Author Comment

by:dosware
ID: 10957690
Good night man! Thank you!
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 300 total points
ID: 10957751
if it's an option would you rather use some codec ?
if so I have a videostreaming example
it uses VCM for compressing/decompressing and DSPack for videocapture - you can change that with anything you like - just feed some bitmaps to the VideoCoDec class to convert :-)
http://files.delphi-si.com/lee/VideoCoDecDemoBin.zip - compiled exe
http://files.delphi-si.com/lee/VideoCoDecDemoSrc.zip - source code
also audio stuff
http://files.delphi-si.com/lee/VoIP.zip - compiled
http://files.delphi-si.com/lee/VoIP_src.zip - sources
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

15 Experts available now in Live!

Get 1:1 Help Now