UDP sockets

This is a discussion on UDP sockets within the Symbian forums in Other Technologies category; I am trying to have two objects communicating with each other by means of s= ockets and the UDP protocol.=20 Both objects are instances of the same class, one marked as "client", the o= ther as "server".=20 The "client" makes a call to SendTo(..) and the "server" to RecvFrom(...).= =20 The asynchronous call to SendTo seems to function (the CActive::RunL method= is called, after a while), but the asynchronous RecvFrom call never gets f= ulfilled.=20 I have checked that the two IP addresses are correct. Is there any other wa= y to check what is going on during socket communication ...

Go Back   Application Development Forum > Other Technologies > Symbian

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-10-2008, 12:01 PM
mach
Guest
 
Default UDP sockets

I am trying to have two objects communicating with each other by means of s=
ockets and the UDP protocol.=20
Both objects are instances of the same class, one marked as "client", the o=
ther as "server".=20

The "client" makes a call to SendTo(..) and the "server" to RecvFrom(...).=
=20
The asynchronous call to SendTo seems to function (the CActive::RunL method=
is called, after a while), but the asynchronous RecvFrom call never gets f=
ulfilled.=20
I have checked that the two IP addresses are correct. Is there any other wa=
y to check what is going on during socket communication (I am using one mob=
ile phone, and one emulator)?

To be (over)complete, I include the code below:=20

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

const TUint KSocketBufferSize =3D 256;

void
CUdpServer::ConstructL()
{
iWriteBuf.CreateL(KSocketBufferSize);
iReadBuf.CreateL(KSocketBufferSize);
}

void
CUdpServer::RequestFunctionL(bool startSending)
{
TInt err =3D iSocketServ.Connect();
if (err !=3D KErrNone) ...

TInt iapID =3D 0;
GetIAPUsed(findString, iapID);

// Open a connection =20
User::LeaveIfError(iConnection.Open(iSocketServ));
TCommDbConnPref prefs;
prefs.SetIapId(iapID);
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPr ompt);

err =3D iConnection.Start(prefs);
if (err !=3D KErrNone) ... =20

RHostResolver hostResolver;
hostResolver.Open(iSocketServ, KAfInet, KProtocolInetUdp, iConnection);

TBuf<255> hostName;
hostResolver.GetHostName(hostName);

TNameEntry nameEntry;
err =3D hostResolver.GetByName(hostName, nameEntry);

TNameRecord nameRec =3D nameEntry();
aInetAddr =3D (TInetAddr&)(nameRec.iAddr);

err =3D iSocket.Open(iSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp,=
iConnection);

// Bind the listening socket to the required port.
if (isClient)
inetAddr.SetPort(KUdpClientPort);
else
inetAddr.SetPort(KUdpServerPort);

err =3D iSocket.Bind(inetAddr);

if (isClient) {
iSocketState =3D ESending;

inetAddr.SetAddress(iRemoteAddress);
inetAddr.SetPort(KUdpServerPort);
iSocket.SendTo(iWriteBuf, inetAddr, 0, iStatus); =20
} else {
iSocketState =3D EReceiving;

inetAddr.SetAddress(iRemoteAddress);
inetAddr.SetPort(KUdpClientPort);
iSocket.RecvFrom(iReadBuf, inetAddr, 0, iStatus);
}

SetActive();
}

void
CUdpServer::RunL()
{
if (iStatus !=3D KErrNone) {
....
}

switch(iSocketState) {
case EReceiving:
{
....
}
case ESending:
{
....
}
default:
ASSERT(EFalse);
}
}

Edited by: mach on 10-Jun-2008 17:00
Reply With Quote
  #2  
Old 06-10-2008, 01:22 PM
mach
Guest
 
Default Re: UDP sockets

Ok, I can see part of my mistake. If I read a buffer of a certain size with=
RecvFrom, then the entire buffer has to be filled before RecvFrom is done.=
In other words, I have to fill the WriteBuf with the same amount of data a=
s the size of the Readbuf buffer.=20
However, if I do that, I get another error:=20
Main Panic ESock_client 14
, in other words: "Descriptor parameter error".....

Reply With Quote
  #3  
Old 06-10-2008, 05:52 PM
maxpayne
Guest
 
Default Re: UDP sockets

Hi [~mach],

A couple of issues with the code you've posted:

1. Don't use Bind, instead use SetLocaPort, all you need to do is specify t=
he port to bind to
2. All in/out parameters passed to an asynchronous method must remain throu=
ghout the lifetime of the call. You are correctly using member descriptors =
for SendTo RecvFrom calls, but if you notice carefully, you are not for th=
e TSockAddr param for both the calls. I suspect that's where the Esock_Clie=
nt 14 panic is coming from. Its raised by Esock server on offending clients=
for passing in invalid descriptor params. In your case, since TSockAddr is=
passed as a stack variable, it gets ou of scope as soon as you exist Reque=
stFunctionL()
3. If you are using UDP sockets, which you are, calling RecvFrom would retu=
rn with the first queued datagram packet. If you don't provide a buffer tha=
t's not bigh enough for the datagram received, you'll only get the partial =
data and the rest will be lost.
4. You don't need to create a temporary or do casting. The following could =
be written=20
bq. bq. from \\ TNameRecord nameRec =3D nameEntry(); \\ aInetAddr =3D (TIne=
tAddr&)(nameRec.iAddr); \\ To \\ const TInetAddr& addr =3D nameEntry().iAdd=
r; \\ Or (if you want a copy) \\ TInetAddr addr =3D nameEntry().iAddr;

Hope that helps.
Reply With Quote
  #4  
Old 06-11-2008, 06:26 AM
mach
Guest
 
Default Re: UDP sockets

Hello maxpayne,=20

Many thanks, having the IP address as a member solved the problem.=20
Your other suggestions proved to be very useful as well.=20
Again, many thanks,=20

Machiel

Edited by: mach on 11-Jun-2008 11:26
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:58 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.