tags:

views:

205

answers:

1

Hi,

I have to come back once again to sockets in Symbian. Code to set up a connection to a remote server looks as follows:

TInetAddr serverAddr;   
TUint iPort=111;

TRequestStatus iStatus;
TSockXfrLength len;

TInt res = iSocketSrv.Connect();

res = iSocket.Open(iSocketSrv,KAfInet,KSockStream, KProtocolInetTcp);

res = iSocket.SetOpt(KSoTcpSendWinSize, KSolInetTcp, 0x10000);

serverAddr.SetPort(iPort);
serverAddr.SetAddress(INET_ADDR(11,11,179,154));

iSocket.Connect(serverAddr,iStatus);
User::WaitForRequest(iStatus);

Over the iSocket i receive packets of variable size. On very few occurences it happens that such a packet is corrupted. What I would like to do then is to clear all the data that is currently in the iSocket buffer and ready to be read. I have not seen any method of RSocket that allows me to clear the content of the buffer. Does anyone know how to do that? If possible, I would like to avoid using RecvOneOrMore() or similar recv function clear the buffer

Thanks

+1  A: 

You should not do what you are asking. Receiving corrupted data is an indication that the application at the other end is faulty somehow, so this is an application-layer issue, not a transport-layer issue. You should, in your application protocol that you have built on top of TCP, include an explicit error response message that you can use in this situation to inform the other end of this situation (if you are attempting some form of reliability). Since you apparently have specific message boundaries in your application protocol, it should not be necessary to close the connection, just discard the corrupted message by reading bytes until the next message boundary, and continue processing normally from then on.

In addition, clearing the receive buffer would be the wrong thing to do even if you could do it. TCP is a byte-stream protocol, so it has no conception of message boundaries. At any specific time, the receive buffer is going to contain a number of bytes that are not guaranteed to correspond in any way to application message boundaries, so clearing it might get rid of many correct messages and possibly leave only a partial message for the next read. (This is, by the way, a common mistake made by beginning network programmers, since if your application communicates only rarely, it might appear as if TCP preserved message boundaries. But it really doesn't, and assuming it does will bite you eventually.)

jk