|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIf your network Symbian program just blocks forever mysteriously, or you just want a simple piece of code to demonstrate how to use socket in Symbian. This story is for you. The expeditionVery often, we want to read a given number of bytes from a network when, for example, the message body length is known after reading the header. Symbian RSocketServ socketServ;
RSocket socket;
RSocket listener;
User::LeaveIfError(socketServ.Connect());
CleanupClosePushL(socketServ);
User::LeaveIfError(listener.Open(socketServ,
KAfInet,KSockStream, KProtocolInetTcp));
User::LeaveIfError(listener.SetLocalPort(80));
User::LeaveIfError(listener.Listen(1));
TRequestStatus status;
TBuf8<256> buffer;
socket.Open(socketServ);
listener.Accept(socket, status);
User::WaitForRequest(status);
......
TBuf8<256>data;
socket.Read(data,status);
User::WaitForRequest(status);
......
The This is not the end of the story because while we are coding, we usually have no idea if it must be 256. The number of bytes to be read can be known only at run time. But the size of the stack based descriptor TPtr8 gDataPtr(NULL,0); UInt32 msglen; //... assign value to msglen ... HBufC8* buffer = HBufC8::NewL(msglen); gDataPtr.Set(buffer->Des()); socket.Read(gDataPtr,status); User::WaitForRequest(status); It works as expected. But is it correct? I thought so before it took me a whole day to find out why sometimes the program just blocks forever. For example, when the value of Why the MaxLength is 140 and not 137? Because “the maximum length of the heap cell in which the I can’t imagine that the Symbian descriptor system doesn’t provide any solution for such a simple problem to the TUint8 *buf=new TUint8[msglen];
gDataPtr.Set(buf,0,msglen);
blank.Read(gDataPtr,status);
User::WaitForRequest(status);
Of course, now I have to take care of releasing the array space myself. I'll appreciate if any Symbian expert can give us an official solution. My expedition has to stop here before I can’t help deleting my Symbian SDK and install double copies of Java and .NET.
|
||||||||||||||||||||||||||||||||||||||||||||