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

Reply via email to