I'm writing a sample using TCP socket.
To communicate between server and client, i use blocking functions (e.g : RSokcet::Read(), RSocket::Write() ) to transmit data. It's OK.
But when i want to use non-blocking function, i have a problem !
I used RSocket::GetOpt() to determine when data are available for reading but it didn't work correctly.
Here's my code :
// ............................................................
// some code above for listening and and accepting a socket is OK
TPckgBuf<TInt> pckLen(0); TInt len = 0;
do
{
// determine when data are available for reading
acceptor.GetOpt(KSOReadBytesPending, KSOLSocket, pckLen);
len = pckLen();
User::After(5);
}while(len==0);
// ............................................................
// somce code below for reading data
P/S : i use prnesocket library so it restricts some function such as RSocket::Ioctl()
hello ..
this is the server side code...
without accept any request...
its running...
at least it should be wait at accept system call...
plz tell me...
what is the error in the following code...
what i have to do for that...
thanx
RSocketServ session;
RSocket listen,socket;
TRequestStatus istatus;
TInt k=session.Connect();
if(k==KErrNone)
{
User::InfoPrint(_L("Connect Success"😉);
}
//User::WaitForRequest(istatus);
if(listen.Open(session,KAfInet,KSockStream,KProtocolInetTcp)!=KErrNone)
Exit();
User::InfoPrint(_L("listen open"😉);
if(socket.Open(session)!=KErrNone)
Exit();
User::InfoPrint(_L("socket open"😉);
TInetAddr k1(KInetAddrAny, KTestPort);
TInt err=listen.Bind(k1);
if(err==KErrNone)
User::InfoPrint(_L("Bind success"😉);
TInt listenStat = listen.Listen(5);
User::InfoPrint(_L("after listen"😉);
User::LeaveIfError(listenStat);
listen.Accept(socket,istatus);
if(istatus!=KErrNone)
User::InfoPrint(_L("accept Success"😉);
User::WaitForRequest(istatus);
TBuf8<256>data;
socket.Read(data,istatus);
User::WaitForRequest(istatus);
socket.Write(data,istatus);
TFileName recv_data;
recv_data.Copy(data);
User::InfoPrint(recv_data);
socket.Close();
listen.Close();
session.Close();
The problem is that before you can start listening the port, you should get IP address for your phone (emulator in this case). So you can open socket to the known IP address and close it right after opening (this activates a connection between phone and server) or you can use browser application instead of "hacking" the code: make connection to the Web, close the browser and execute your program.