Article View: borland.public.cppbuilder.internet.socket
Article #73Trouble with TcpClient component
From: Carlos Moreno
Date: Mon, 21 Apr 2008 15:54
Date: Mon, 21 Apr 2008 15:54
81 lines
4158 bytes
4158 bytes
Hi, I'm having major trouble and major frustration when trying to migrate an application from C++ Builder 6 to Builder 2007 (I have the trial version, so it may not be the latest release 2). The short story / bottom line: how can I emulate the non- blocking functionality of TClientSocket, where I transmit whenever I need to transmit, and get an event whenever the socket received data from the server? The long / detailed story: My existing application uses sockets (TClientSocket) in non-blocking mode for *fully asynchronous* communications. TClientSocket is not supported in C++B2007, and the closest thing seems to be TcpClient (which also exists in C++B6, so this could help with the transition). With TClientSocket, I set it in non-blocking mode, and I do socket->Open() when the user activates the connection. Works like a charm: whenever the user transmits something, I receive an OnReceive (or was it OnRead?) event, to which I respond by calling socket->ReceiveBuf and read the received data. I pretty much ignore the OnSend event, as that one simply tells me that I *can* transmit --- I simply transmit when the user triggers an action that requires transmitting. Ok, I'm trying to emulate exactly the above --- the docs in C++B6 for this class/component are utterly useless; and so are C++B2007's --- The entry TcpClient takes me to the documentation for TIdTcpClient --- Indy's tcp client socket, which does not look even remotely similar to TcpClient. The little it says suggests that it works exactly the same; however, they don't. When I call socket->Open(), I immediately receive an error (an OnError event, with error code WOULDBLOCK --- meaning that the call to connect returned immediately but the operation has not been completed.... Well DUH! It's in non-blocking mode !!! How could it be completed? And how why would it tell the application that there was an error? (needless to say that with TClientSocket, I get no error, and instead, I get an OnConnect event a fraction of a second after calling socket->Open() .... Seems like the obviously correct thing to do) The connection *does* succeed, but I never get an OnConnected event (I know it succeeds for two reasons: 1) if I call socket->Open() again, I get an error ALREADYCONNECTED; and 2) if I now call socket->ReceiveBuf, I do read the server's greeting message --- the server transmits immediately upon the connection being accepted) The thing is, I never get an OnReceive event (well, I do, but I do get it in response to my call to ReceiveBuf --- clearly, I need it to be the other way around; I would only call ReceveiBuf when I'm notified, via an event, that the socket just received data); I really really need this feature, since my application has other things to do and can not afford to do polling on the socket to see if there is data, nor set it in blocking mode. Sure, I could make a separate thread and do polling or use the socket in blocking mode, but that would be a major restructuring of the application, so I'd *really* like to stay away from that option. Any tips on what's going on or how to make this work? Am I doing something wrong? Is the functionality of these sockets hopelessly broken? (in which case, suggestions?) Thanks! Carlos --
Technical Details
Message-ID:
<480cb8b5$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.isoc.lu!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,9f8efd9a63590eec,start X-Google-Attributes: gid11508e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Carlos Moreno" <moreno_h3gd@mochima.com> Sender: "Carlos Moreno" <moreno_h3gd@mochima.com> Reply-To: "Carlos Moreno" <moreno_h3gd@mochima.com> Subject: Trouble with TcpClient component Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 67.142.130.24 NNTP-Posting-Host: 207.105.83.66 Message-ID: <480cb8b5$1@newsgroups.borland.com> Date: 21 Apr 2008 08:54:29 -0700 X-Trace: newsgroups.borland.com 1208793269 207.105.83.66 (21 Apr 2008 08:54:29 -0700) Lines: 82 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.isoc.lu!newsgroups.borland.com!not-for-mail