How to forcibly disconnect a client from TCP Server

Hello everyone,

I have the following code that forcibly disconnects a client from a TCP Server based on the client's IP address. The connected clients are displayed in a listview. The code works fine but I find it rather inelegant because I had to assign 2 different values to LClientContext for it to work. First I set LClientContext: TClientContext then I set LClientContext := TClientContext(AContext) so that I could access the MyData class which is a pointer to AContext.Data.

{code}
procedure TfrmServeur.acBreakSingleConnectionExecute(AContext: TIdContext);
var
  // loop variable
  Index: Integer;
  // client list, holds TClientContext objects
  LClients: TList;
  // temporary client context reference
  LClientContext: TClientContext;
  // pointer to AContext.Data
  MyData: TMyData;
begin
  // Hard cast AContext to TClientContext
  LClientContext := TClientContext(AContext);
  //
  MyData := TMyData.Create;
  LClientContext.Data := MyData;
  //
  LClients := TCPServer.Contexts.LockList;
  //
  try
    // search for the target client by ID
    for Index := 0 to LClients.Count -1 do begin
      LClientContext := TClientContext(LClients[Index]);
      //
      if TMyData(LClientContext.Data).IP = Trim(lvuClients.Selected.Caption) then
      begin
        // IP has been found in the listview so disconnect the client
        if LClientContext.Connection.Connected then
        begin
          //
          LClientContext.Connection.IOHandler.InputBuffer.Clear;
          //
          LClientContext.Connection.IOHandler.Close;
          // Disconnect the selected client
          LClientContext.Connection.Disconnect;
        end
        else
          // Just remove the item in the listview since it is no longer connected
          lvuClients.Selected.Delete;
         // break loop, we've found our target client
        Break;
      end;
    end; // for Index := 0 to LClients.Count -1 do begin
  finally
    // unlock client list
    TCPServer.Contexts.UnlockList;
    //
    MyData.Free;
  end; // tryf
end;
{code}

 It works but I find it a bit odd. Is there a better way to do this? Thanks for your assistance.

Jay Dee
0
Jay
4/30/2015 8:28:07 AM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

0 Replies
901 Views

Similar Articles

[PageSpeed] 5

Reply:

Similar Artilces:

Disconnecting Component::Server::TCP when Component::Client::TCP disconnects
This code comes from the TCP port redirection with components in the poe.perl.org website cookbook (http://poe.perl.org/?POE_Cookbook/ TCP_Port_Redirection_With_Components). I swapped the client and server roles. Basically I want the client component to connect to a remote server first, then start up the server component to listen for incoming connections. It all seems to work fine except when the connection is lost to the remote server, the client server never shuts down. In other words...I can't figure out why the server component does not recieve the "shutdown" comm...

How to forcibly disconnect a client from TCP Server [Edit]
Hello everyone, I have the following code that forcibly disconnects a client from a TCP Server based on the client's IP address. The connected clients are displayed in a listview. The code works fine but I find it rather inelegant because I had to assign 2 different values to LClientContext for it to work. First I set LClientContext := TClientContext(AContext) then I set LClientContext := TClientContext(LClients[Index]) so that I could access the MyData class which is a pointer to AContext.Data and use the for...loop ind ex variable to locate the IP I want to forcibly disconnect. {c...

Force all PoCo::Server::TCP clients to disconnect
--001517741b06ddf99704ac354eab Content-Type: text/plain; charset=ISO-8859-1 Hi, I have a TCP server with persistent client connections which are not expected to disconnect. I want to be able to bring down the PoCo::Server::TCP, but sending "shutdown" makes a graceful client shutdown, waiting for persistent connections to close. I've not found any way to tell the TCP server PoCo to perform the disonnect on the server side, and the client sessions My application does not keep track of connected client sessions. According to POE::Kernel, doing: $_[KERNEL]->sig...

Starting TCP Server From within TCP Client
Hello experts is their any possibility of starting tcp server that is on remote machine from tcp client that is on another machine. if not then plz mention how can i start tcp server from remote location (i know remote machine's user name, password, ip address) plz dont forget to mention tutorials regarding this bcz i m new to socket programming. regards ali...

Novel Client sp1 disconnects file server disconnects
I have many windows XP Novell clients that I have set the tree to server's IP address and that seemed to fix many problems after migrating from Novell 4.11 and GroupWise 5.5 to Suse 9 and OES with GroupWise 6.5. I have one user in particular and several other users who occasionally loose there connection to the file server and loose there drive mappings. This occasionally happens at boot up or after they have been logged on for a while. The worst case person was loosing connections several times a day we replaced the nic card and it seemed to get better for a day or two...

automation server to client and client to server
I have to applications that need to communicate with each other. Is it practical for both of them to be servers and clients to each other? One of the applications will work primarily as a server and will launch the other application. The newly launched application needs to be able to send occasional information to the server to update information in the application that launched it. Also, if the user attempts to close the launched application it needs to tell the launcher application to close the appl ication that it launched. Does this sound practical? This is kind of like launching wo...

proxy with poco::server::tcp & poco::client::tcp?
Below is a shar file containing a tcp proxy together with a server and a client to test it. The proxy works just fine except that I can't figure out how to get the poco::server::tcp child session to drop its connection when the poco::client::tcp connection is closed by the remote server. Have I created a cyclic reference with my use of the $heap in a closure? Am I doing something else stupid? thanks -- Chris Fedde # This is a shell archive. Save it in a file, remove anything before # this line, and then unpack it by entering "sh file". Note, it may # create...

Tcp server disconnection
Hi, I have a TIdTcpClient and TIdThreadComponent to connect to a TCP server; When i close the server the Disconnect event is not fired also if the connected() return false; Is this normal? How to know the server disconnection? I have tryed with CheckForDisconnect() at the end of the Execute function and it seem to catch the disconnection. Is this the correct way? C++2009 latest indy snapshot Thanks! Mirco This the code in Execute event of Thread if( !TCP_client1->Connected() && !Sender->Terminated ) { flActive= false; try { TCP_client...

client and server disconnecting
I was on a site yesterday where they supposedly had networking problems in that the PC's were losing the server. (Netware 4.11) On close examination I found that the SQL Anywhere client was losing the SQL server and the undelying network was working fine. I know next to nothing about SQL but I do Know where to look for help. The Users on this site are at a loss as to why their implementation is behaving in this way! Any help would be very much appreciated cheers Clive How long does it take to lose the connection? There are timeout parameters that can be set on the clie...

Pass values from server to client and client to server?
Hi, i have a scenario, i want to  pass some values to an image and a label dynamically from database. i want to use an anchor(a href) tag and when the mouse goes over the anchor , an image url and label text must change with the values from database. Suppose that there are more than one anchor at the page.  One more thing, anchors must get the id values of the records from database at page_Load() and the queries use these these ids again to retreive imageurl and label text values from database when ...

Server Disconnecting Client
Hi, I'm running SQLA 5.05.04 build 1867 on a Netware 3.2 server. Lately we've begun experiencing problems with the server disconnecting from the client with this message on the server screen: Disconnecting USERNAME - 120 seconds since last contact This seems to have begun happening after installing UPS Online Shipping software on PC's throughout the building. UPS software uses ODBC 3.0 to read an Access database. I'm wondering if there is a database or client setting to increase this timeout period or decrease the contact period so I can perhaps futher isola...

Component::Server::TCP: how to get access to the server's HEAP from a client handler?
--0016e6475fc6f0d287046ad18a25 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi, I'm using POE for 3 months now with great pleasure. Thanks to all the developers! I'm now playing with TCP client and server. I've created a POE::Component::Server::TCP which has global state stored in its HEAP, initialized in the Started event. How can I get access to that HEAP from the Client* events (ClientConnected in particular) as those are called from a different session ? Maybe there is a parent-child relationship between the sessions I could us...

nw5.1 sp7 & client 4.9sp1a: client disconnects copying to server.
in a new netware 5.1 server with sp7, from a win Xp pro client with 4.90Sp1a (loged as admin), when i try to copy files, after few seconds and few files copieds, the client shows a message "can't access to directory" or something like this, and in server side (monitor) the client conection dissappears. We have checked wire connections and are ok. help please!! TIA. josepmaria.gomez Try removing the client and installing the sp 2 version of 4.9 and with custom IPX only. -- Dave Lunn NSC SysOp http://support-forums.novell.com > Try removing the ...

How to detect the client having disconnected to the server
hello, If the database server unexpectedly shuts down when the program is running,how could we timely detect it? Of course,we can know it after the query request fails 3 or more times because of timeout. However,do we not have other simple way to know it? In DBlib,we have the function DBDEAD() to do it easily. Now in CTlib, what shall we do? //thx Hello, Would this work for you.... ct_con_props(CS_GET, CS_CON_STATUS) - Check the CS_CONSTAT_DEAD bit in the returned value. sailinger wrote: > hello, > If the database server unexpectedly shuts down when...

Web resources about - How to forcibly disconnect a client from TCP Server - embarcadero.delphi.winsock

Disconnect (2012 film) - Wikipedia, the free encyclopedia
The film explores how people experience the negative sides of modern communication technology by following three stories. Up-and-coming reporter ...

Did Mark Zuckerberg Disconnect Facebook Phone Rumors Once And For All?
Facebook Co-Founder and CEO Mark Zuckerberg has attempted over and over to squash speculation about a Facebook phone, and he offered his strongest ...

Huff/Post50 - A question: When you disconnect from someone... - Facebook
A question: When you disconnect from someone on Facebook, are you unfriending them or defriending them? And does it matter? Well, why you DID ...

Disconnect - tracking and malvertising protection on the App Store
Read reviews, compare customer ratings, see screenshots, and learn more about Disconnect - tracking and malvertising protection. Download Disconnect ...

disconnect
if you can't unplug, just cut the cord

Disconnect - YouTube
Disconnect lets you visualize and block 2,000+ third-party tracking sites to make the web faster, more private, and more secure. As a Certified ...

‘Big Disconnect’ Offers Guidance for Parents in a Tech-Crazed World
Did you hear the one about the 7-year-old boy who opened a Wii on Christmas morning and when his parents finally checked on him, he’d played ...

Cruising Fiji: The ultimate disconnect
It took three years of negotiations over much kava before a cruise line could set sail for Fiji's remote Lau islands, writes Katrina Lobley. ...

Correcting an NBN Co disconnect
Behind the expected red numbers NBN Co revealed some encouraging trends, albeit off a low base. And Bill Morrow is moving decisively to clear ...

'Some disconnect': inquiry wants Premier Mike Baird's $300 billion power claim tested by experts
... NSW would be $300 billion better off if the state's electricity assets are privatised should be independently tested, amid "some disconnect" ...

Resources last updated: 1/8/2016 9:13:01 AM