Help! UDP Recv Not Working...

Login to reply to this topic.
Mon, 2005-06-13 17:18
Joined: 2005-02-14
Forum posts: 13
I have a simple UDP sending and receiving issue.  THis should work!

I open a UDP socket like this:

   err=sock->Open(socksvr, KAfInet, KSockDatagram, KProtocolInetUdp);
   User::LeaveIfError(err);
   sock->Connect(address, stat);

The socket opens fine and I use it to send a datagram.  I know the datagram goes
out and is in the correct format. 

Then I use the following code to wait for a response:

   iRDBuffer = HBufC8::NewL(10);
   iRData = new TPtr8(iRDBuffer->Des());
   sock->Recv(*iRData, 0, stat);
   User::WaitForRequest(stat);

And the code does not move on -- it hangs waiting for the return packet. I have
also used "sock->RecvFrom(*iRData, address, 0, stat);" with the same results.

I know the packet comes back; I can track it.

I am using UIQ 2.1 with the WinSock method of connecting the emulator to the
Internet.  However, this same behavior is seen when using GPRS on a UIQ phone.

Does anyone have any hints for me.  This should work.  It's so easy.

Thanks.

-F

Mon, 2005-06-13 18:26
Joined: 2005-05-09
Forum posts: 33
Re: Help! UDP Recv Not Working...

I think you should use recvfrom instead recv  as you are using UDP. i have written program in windows for UDP socket communication

i can send the code if u want( the code wrtten using winsock API)
Mon, 2005-06-13 18:40
Joined: 2005-02-14
Forum posts: 13
Re: Help! UDP Recv Not Working...
I will gladly accept any code that works and will post any solution I find! However, let me ask:
is this implemented on Symbian OS or in Windows (since you mention the winsock API)?
I need working clues for Symbian OS (UIQ 2.1).

Many thanks.  Send code to "frethop@hotmail.com".  I'll check it out closely!

- F
Mon, 2005-06-13 19:23
Joined: 2005-05-09
Forum posts: 33
Re: Help! UDP Recv Not Working...
 it works only for windows not for symbian:

simple echo program:

// Client Program

#include <WinSock.h>
#include <stdio.h>
#include <iostream.h>

#define SERVER_PORT 4000

void Initialize() {
   WORD wVersionRequested;
   WSADATA wsaData;
   int err;

   wVersionRequested = MAKEWORD( 1, 1 );

   err = WSAStartup( wVersionRequested, &wsaData );
   if ( err != 0 ) {
      /* Tell the user that we couldn't find a useable */
      /* winsock.dll.                                  */
      printf("\n couldn't find a useable winsock.dll");
      exit(1);
   }

   /* Confirm that the Windows Sockets DLL supports 1.1.*/
   /* Note that if the DLL supports versions greater    */
   /* than 1.1 in addition to 1.1, it will still return */
   /* 1.1 in wVersion since that is the version we      */
   /* requested.                                        */

   if ( LOBYTE( wsaData.wVersion ) != 1 ||
          HIBYTE( wsaData.wVersion ) != 1 ) {
      /* Tell the user that we couldn't find a useable */
      /* winsock.dll.                                  */
      WSACleanup( );
      printf("\n couldn't find a useable winsock.dll");
      exit(1);
   }
}

int main()
{
   SOCKET            sockfd;
   SOCKADDR_IN         servaddr,cliaddr;
   int sin_size = sizeof(struct sockaddr_in);
    char recvline[100];
   char sendStr[100];
   int n;

   Initialize();

   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr = inet_addr("10.6.21.13");
   servaddr.sin_port = htons(SERVER_PORT);
      

      sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        cout<<"UDP socket created with ID ="<<sockfd<<endl;
      
      while(1) // always true
      {
            cout<<endl<<"Enter string or write exit to quit"<<endl;
            cin>>sendStr;
      
            if(strcmp("exit",sendStr)==0)
               exit(0);
            sendto(sockfd, sendStr, strlen(sendStr), 0, (LPSOCKADDR) &servaddr, sin_size);
            n = recvfrom(sockfd, recvline, 100, 0, (LPSOCKADDR) &cliaddr,  &sin_size);
            recvline[n] = '\0';   /* null terminate */   
            cout<<"Received from Server    = "<<recvline<<endl;
            
      }

   return 0;
}

===================================

//Server Program:


#include <WinSock.h>
#include <stdio.h>
#include <iostream.h>
#define SERVER_PORT 4000


void Initialize() {
   WORD wVersionRequested;
   WSADATA wsaData;
   int err;

   wVersionRequested = MAKEWORD( 1, 1 );

   err = WSAStartup( wVersionRequested, &wsaData );
   if ( err != 0 ) {
      /* Tell the user that we couldn't find a useable */
      /* winsock.dll.                                  */
      printf("\n couldn't find a useable winsock.dll");
      exit(1);
   }

   /* Confirm that the Windows Sockets DLL supports 1.1.*/
   /* Note that if the DLL supports versions greater    */
   /* than 1.1 in addition to 1.1, it will still return */
   /* 1.1 in wVersion since that is the version we      */
   /* requested.                                        */

   if ( LOBYTE( wsaData.wVersion ) != 1 ||
          HIBYTE( wsaData.wVersion ) != 1 ) {
      /* Tell the user that we couldn't find a useable */
      /* winsock.dll.                                  */
      WSACleanup( );
      printf("\n couldn't find a useable winsock.dll");
      exit(1);
   }
}


int   main()
{
   SOCKET            sockfd;
   int               n;
   SOCKADDR_IN         servaddr, cliaddr;
    char            mesg[100];
    int sin_size = sizeof(struct sockaddr_in);

   Initialize();

   sockfd = socket(AF_INET, SOCK_DGRAM, 0);

   cout<< "Socket Created with ID" <<sockfd<<endl;

   servaddr.sin_family      = AF_INET;
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servaddr.sin_port        = htons(SERVER_PORT);
   memset(&(servaddr.sin_zero), 0, 8 );

   if(bind(sockfd, (LPSOCKADDR) &servaddr, sizeof(servaddr))==0)
      cout<<"Bind operation successfully done"<<endl;
   else
      cout<<"Bind operation failed"<<endl;

   while(1)
   {
      
      n = recvfrom(sockfd, mesg, 100, 0,(LPSOCKADDR) &cliaddr,  &sin_size);
      mesg[n] = '\0';   /* null terminate */   
      cout<<"Message Received           :"<<mesg<<endl<<endl<<endl;
      sendto(sockfd, mesg, strlen(mesg), 0, (LPSOCKADDR) &cliaddr,  sin_size);
      printf("Message Sent back to client : %s\n",mesg);
   }

   return 0;
}

Mon, 2005-06-13 19:34
Joined: 2005-05-09
Forum posts: 33
Re: Help! UDP Recv Not Working...

same kind of query posted in the follwing link check it out

http://forum.newlc.com/index.php/topic,5960.msg17361.html#msg17361
Mon, 2005-06-13 20:26
Joined: 2005-02-14
Forum posts: 13
Re: Help! UDP Recv Not Working...
Thanks...But there is no solution given to the posting you are referring to.  I need the solution! Smiley

Was that one solved? Chaosy?

Thanks.

- F
Wed, 2006-08-02 12:46
Joined: 2005-01-13
Forum posts: 17
Re: Help! UDP Recv Not Working...
Try with

   iSocket.RecvFrom(iBuffer,iRecAddr,0, iStatus);

this worked for me.


Regards
Birinder

Wed, 2006-08-02 19:55
Joined: 2005-02-18
Forum posts: 100
Re: Help! UDP Recv Not Working...
Quote
I am using UIQ 2.1 with the WinSock method of connecting the emulator to the
Internet.

When you use winsock, and sockets don't work, the problem has nothing to do with Symbian OS (it is bypassed). The problem is ALWAYS either in winsock or some error in the application.

Quote
However, this same behavior is seen when using GPRS on a UIQ phone.

You may have different problem there. The first thing there is to get the connection up and working: does your server receive the packet and reply to it in GPRS case?

Quote
And the code does not move on -- it hangs waiting for the return packet.

Then that that means: on emulator winsock does not get the packet, or on the phone symbian stack does not get the packet. Or the return packet is somehow faulty:

Quote
I know the packet comes back; I can track it.

Just to be sure, you have then verified that the ports and addresses of your return packet exactly match the addresses in you outgoing packet (src/dst reversed)?
  • Login to reply to this topic.