Hello David, Will try to check your code this evening. I cannot do real debug because I only have Delphi, but I will check it. Some quick comments for now:
> co_ServerWinsockPort->ClientClass = __classid(TTcpSrvClient); Did you did this line of code "before" you call Listen ? If yes then OK. > Edit: I have just noticed that the reply received by the Client is a > text string "Welcome to TcpSrv". Ok that explains the 19 bytes (the banner + \r\n). > And where is my data? Good question :( You send binary data I think. You dont have LineMode set to True at client ? Eventually you download SocketSpy from user made, set it to 'debugstring' and you can watch what is transmitted. > I am using ->>Send, not ->SendStr. Is same. SendStr call Send. > Another thing I currently have is that the Client/Server connection > works first time every time. However, after the first transaction, the > client cannot connect again event though I finished the transaction with > Close. You did not used Close from one of the TWSocket events ? If you do then call CloseDelayed. --- Rgds, Wilfried http://www.mestdagh.biz Wednesday, September 7, 2005, 10:58, David Lewis wrote: > Thanks for your reply, but it hasn't made any progress for me. > Adding: > co_ServerWinsockPort->ClientClass = __classid(TTcpSrvClient); > Had no effect at all(!) to what I was seeing. > I have now seperated my code into client and server to try and make > it easier for me to find out what is happening and added event logs as > a means to debug it. > What I have found so far (for events) is: > ---------------------------------------------------------------- > Client Server Remote Client > ---------------------------------------------------------------- ->>Connect > OnClientConnect > ->Create Socket > OnDataSent > OnDataSent > OnSessionConnected ->>Send > me_ClientDataSent > OnDataAvailable > ->Receive > ->Send (reply) > OnDataAvailable ->>Close > OnClientDisconnect > OnSessionClosed > ---------------------------------------------------------------- > Note: The 'remote client' is the socket created by the server when the > client connects. > Currently, I have it so that the client sends 10 data bytes, and the > server will reply with 8 data bytes. > The server picks up the 10 bytes correctly, and claims to reply with the 8. > However, the client apparently receives 19 bytes in reply, none of which > seem to be the 8 that the server has sent! > Edit: I have just noticed that the reply received by the Client is a > text string "Welcome to TcpSrv". Is this normal? And where is my data? I am using ->>Send, not ->SendStr. > Another thing I currently have is that the Client/Server connection > works first time every time. However, after the first transaction, the > client cannot connect again event though I finished the transaction with ->>Close. > Any ideas? > Source code attached (Borland C++ Builder). > Dave > Wilfried Mestdagh wrote: >>Hello David, >> >>You have a class TTcpSrvClient, whitch is derrived from TWSocketClient >>class, but I dont see you assigning this class to the server component: >> >> co_ServerWinsockPort->ClientClass = TTcpSrvClient; >> >>You assign event handler to OnError. This is not good, you better work >>with your own try / catch handling to find out if and where some >>exception should occure. For example you should have one in the Listen >>method, and whilst developping at least in all events. >> >>You send buffer, that's OK, but be aware that TCP is not boundary safe, >>you can send 1000 bytes and receive 2 chunck of 500, or you can send 2 >>times 1000 bytes and it is possible you receive as first packet 1500 >>bytes. So you have to be prepared for that. >> >>--- >>Rgds, Wilfried >>http://www.mestdagh.biz >> >>Tuesday, September 6, 2005, 13:39, David Lewis wrote: >> >> >>>Ok, so expanding a little on what I have: >>> >> >>>The components are dynamically created: >>> >> >>> TWSocket *co_ClientWinsockPort; >>> TWSocketServer *co_ServerWinsockPort; >>> >> >>>at_TargetIpAddress = "192.168.0.42"; >>>// Client TCP Comms Component >>> co_ClientWinsockPort = new TWSocket(NULL); >>> co_ClientWinsockPort->Port = 1001; >>> co_ClientWinsockPort->Proto = "tcp"; >>> co_ClientWinsockPort->Addr = at_TargetIpAddress; >>> co_ClientWinsockPort->OnSessionConnected= me_ClientSessionConnected; >>> co_ClientWinsockPort->OnDataAvailable = me_ClientDataAvailable; >>> co_ClientWinsockPort->OnError = me_ClientError; >>> >> >>>// Server TCP Comms Component >>> co_ServerWinsockPort = new TWSocketServer(NULL); >>> co_ServerWinsockPort->Port = 1002; >>> co_ServerWinsockPort->Proto = "tcp"; >>> co_ServerWinsockPort->Addr = "0.0.0.0"; // Server - >>>Accept All Connections >>> co_ServerWinsockPort->OnClientConnect = me_ServerClientConnect; >>> co_ServerWinsockPort->OnError = me_ServerError; >>> co_ServerWinsockPort->Listen( ); >>> >> >>>----------------------------------------------------------- >>> >> >>>For the client, to initiate a message, I use: >>> co_ClientWinsockPort->Addr = at_TargetIpAddress; >>> co_ClientWinsockPort->Port = at_ClientPort; >>> co_ClientWinsockPort->Connect( ); >>> >> >>>Then wait for OnSessionConnected and send: >>> co_ClientWinsockPort->Send(at_TxBuffer, at_TxPacketIndex); >>> >> >>>Any response from the server would be received during >>>ClientDataAvailable and gathered with: >>> at_RxPacketIndex = >>>co_ClientWinsockPort->Receive(&at_RxBuffer[at_RxPacketIndex], >>>sizeof(at_RxBuffer) - 1); >>> >> >>>If OnError fires, or the comms transaction is complete, I call: >>> co_ClientWinsockPort->Close( ); >>> >> >>>----------------------------------------------------------- >>> >> >>>On ther server side, I wait for OnClientConnect, then create a new >>>socket using: >>> ((TTcpSrvClient *)Client)->LineMode = TRUE; >>> ((TTcpSrvClient *)Client)->LineEdit = TRUE; >>> ((TTcpSrvClient *)Client)->OnDataAvailable = me_RemoteDataAvailable; >>> ((TTcpSrvClient *)Client)->OnBgException = me_RemoteBgException; >>> >> >>>I then wait for me_RemoteDataAvailable and collect with: >>> Client = (TTcpSrvClient *)Sender; >>> at_RxPacketIndex = >>>Client->Receive(&at_RxBuffer[at_RxPacketIndex], >>>sizeof(at_RxBuffer) - 1); >>> >> >>>I can then reply with: >>> Client->Send(at_TxBuffer, at_TxPacketIndex); >>> >> >>>Any me_RemoteBgException is handled with: >>> CanClose = TRUE; >>> >> >>>----------------------------------------------------------- >>> >> >>>Is there anything missing from the above events? >>> >> >>>I compile this twice, changing the at_TargetIpAddress to point to the >>>other machine, and swapping the port numbers over. (1001/1002) >>> >> >>>I seem to be very close with what I have, but sometimes messages don't >>>get through. Sometimes I get a Connected event, then no DataSent event, >>>and sometimes nothing at all. Am I missing something, or have I done >>>something wrong? >>> >> >>>And there any major 'gotchas' when working with client/server sockets >>>that I should be aware of that I might have fallen into? >>> >> >>>Any info would be greatfully appreciated. >>> >> >>>Dave >>> >> >> >> >> >> >> >> >> >>>Wilfried Mestdagh wrote: >>> >> >>>>Hello David, >>>> >>>> >>>> >>>>>Is that the correct logic for it all? >>>>> >>>>> >>>>yes it is correct. >>>> >>>> >>>> >>>>>I have tried a number of different ways of doing this, but each way >>>>>doesn't seem to have reliable communications. >>>>> >>>>> >>>>Should be very reliable. If you can provide some more detail of what the >>>>problem is, then we all very glad to help. >>>> >>>>--- >>>>Rgds, Wilfried >>>>http://www.mestdagh.biz >>>> >>>>Tuesday, September 6, 2005, 10:55, David Lewis wrote: >>>> >>>> >>>> >>>>>I've been trying to sort out a stable client/server connection with ICS >>>>>but I'm having a few difficulties, and I'm thinking that maybe my >>>>>understanding of how it works may be a little wrong. >>>>> >>>>> >>>>>Initially, I am building one application which can act as a client & >>>>>server, and put one on the remote machine and for them to talk to each >>>>>other. I set the IP addresses & ports differently on each. >>>>> >>>>> >>>>>What I have: >>>>> >>>>> >>>>>1 x TWSocket for sending messages to a remote server >>>>> >>>>> >>>>>1 x TWSocketServer for receiving messages from a remote system >>>>>The TWSocketServer creates a new TTcpSrvClient for each incoming >>>>>connection. >>>>> >>>>> >>>>>Now, I believe that it works like this: >>>>> >>>>> >>>>>To send a message, I use TWSocket component. >>>>>The remote machine then sees an incoming connection in TWSocketServer >>>>>and creates a TTcpSrvClient for that connection. >>>>>The TTcpSrvClient then receives the message and a reply can be sent back >>>>>through this component. >>>>>This reply is then picked up back in the TWSocket that sent it. >>>>>When the TWSocket closes the connection, the TTcpSrvClient is >>>>>automatically deleted and things go back to square one. >>>>> >>>>> >>>>>Is that the correct logic for it all? >>>>> >>>>> >>>>>I have tried a number of different ways of doing this, but each way >>>>>doesn't seem to have reliable communications. >>>>>I've looked at the examples for these components, and this way is my >>>>>understanding of them, but I want to be sure hence my posting here, >>>>>cause something is still not quite right. >>>>> >>>>> >>>>>Thanks, >>>>> >>>>> >>>>>Dave >>>>> >>>>> >>>> >>>> >> >> -- To unsubscribe or change your settings for TWSocket mailing list please goto http://www.elists.org/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be