Help! UDP Recv Not Working...
| Mon, 2005-06-13 17:18 | |
|
I have a simple UDP sending and receiving issue. THis should work!
I open a UDP socket like this:
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:
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 |
|






Forum posts: 33
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)
Forum posts: 13
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
Forum posts: 33
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;
}
Forum posts: 33
same kind of query posted in the follwing link check it out
http://forum.newlc.com/index.php/topic,5960.msg17361.html#msg17361
Forum posts: 13
Was that one solved? Chaosy?
Thanks.
- F
Forum posts: 17
iSocket.RecvFrom(iBuffer,iRecAddr,0, iStatus);
this worked for me.
Regards
Birinder
Forum posts: 100
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.
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?
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:
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)?