Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Need to synchronize two sockets!

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
dosware
Asked:
dosware
  • 16
  • 12
2 Solutions
 
sftwengCommented:
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
 
sftwengCommented:
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
 
doswareAuthor Commented:
I'm using TIdTcpServers and TIdTcpClients. It means that I need solution for code which I wrote below.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
doswareAuthor Commented:
Dear Sir,

I need to transfer 6 stream with different types of data by this sockets.
0
 
sftwengCommented:
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
 
sftwengCommented:
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
 
doswareAuthor Commented:
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
 
doswareAuthor Commented:
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
 
sftwengCommented:
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
 
sftwengCommented:
Just a small point of clarification, please - do all of the data sockets originate from the same place?
0
 
doswareAuthor Commented:
Dear Sir,

Real help which you can give me is a working example.
0
 
sftwengCommented:
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
 
doswareAuthor Commented:
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
 
sftwengCommented:
Is there a one-to-one mapping? I.e., for every A connection there is a single B connection, and vice-versa?
0
 
doswareAuthor Commented:
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
 
sftwengCommented:
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
 
sftwengCommented:
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
 
sftwengCommented:
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
 
doswareAuthor Commented:
Dear Sir,

Thank you very much for your comment, I have fixed this problem.
0
 
doswareAuthor Commented:
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
 
sftwengCommented:
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
 
doswareAuthor Commented:
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
 
sftwengCommented:
You could starte here: http://turbo.gamedev.net/delphix.asp
0
 
sftwengCommented:
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
 
doswareAuthor Commented:
Thank you bud, also probably you have some URLs where I can read more about Indy, about TIdThreadMgr and etc...
0
 
sftwengCommented:
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
 
doswareAuthor Commented:
Good night man! Thank you!
0
 
Lee_NoverCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 16
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now