Page 1 of 1

c++ winsock udp sockets problem

Posted: Tue Jul 21, 2009 9:36 pm
by Rumi
hi! I have so many unsolved issues in my project that i don't know what to ask first, but i'll go for udp sockets.

I want an app that sends coords to another app running on another pc on my LAN. I have already done that with tcp sockets, but i was forced to do it with udp sockets, but i can't make it work...

Here is my relevant code:

Data sender:

Code: Select all

SOCKET s_;
int* refCounter_;
std::string error;
sockaddr_in dest;
sockaddr_in local;
int ret;

dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr( ip_dest );
dest.sin_port = htons( port );

// create the socket
s_ = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

while(1)
  ret = sendto( s_, s.c_str(), s.length(), 0, (sockaddr *)&dest, sizeof(dest) ); //s is a std::string containing the data
Data receiver:

Code: Select all

SOCKET s_;
int* refCounter_;
std::string error;
sockaddr_in dest;
sockaddr_in local;
int dest_size;
dest_size = sizeof(dest);

local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons( puerto_in );

// create the socket
s_ = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
// bind to the local address
int ret = bind( s_, (sockaddr *)&local, sizeof(local) );

while(1)
{
//I want to analyze characters so i receive char by char
        char r;
	int result = recvfrom( s_, &r, 1, 0, (sockaddr *)&dest, &dest_size );
//Check data...
}
This simply isn't working... I hace checked that data actually sends (sendto returns a correct number of bytes sent), but recvfrom always returns -1...

What can i do?
I would thank A LOT any help...

Thanks guys.

Re: c++ winsock udp sockets problem

Posted: Mon Jul 27, 2009 9:23 pm
by nbeato
Is WSAGetLastError() returning WSAEMSGSIZE? You are trying to read a buffer with s.length() bytes with only 1 byte in the recvfrom buffer. UDP will drop the remaining bytes and return an error indicating the buffer is too small. If you want to process byte by byte, read the entire message and process your software buffer byte by byte (instead of the driver/hardware buffer).

Re: c++ winsock udp sockets problem

Posted: Mon Jul 27, 2009 9:35 pm
by Rumi
I dumped WSAGetLastError() and it was always -1
:(

Re: c++ winsock udp sockets problem

Posted: Mon Aug 03, 2009 6:16 pm
by Moohasha
Are you calling WSAStartup before doing anything? On Windows, this has to be called to initialize Winsock before you can use the sockets. If you got it working with TCP, I assume you're doing this, but it's safe to cover the obvious.

Also, try calling:

Code: Select all

setsockopt(socket, SOL_SOCKET, SO_RCVBUF, buffer, buffer_size);
to set the receive buffer.

If you continue having problems, it might be better to use boost or something that has a defined UDP socket class instead of trying to write the low level socket code yourself.

Re: c++ winsock udp sockets problem

Posted: Mon Aug 03, 2009 6:29 pm
by Rumi
Thanks to you all guys...
I was trying your solutions, but i gave up... :( Nothing seems to work.

I found a socket library than implements udp sockets, and it works.
At last always the better option seems to be NOT writing low level code... ;)

Thanks again!

Re: c++ winsock udp sockets problem

Posted: Mon Aug 03, 2009 9:28 pm
by nbeato
I still don't know what your problem was, but did you look at the boost::asio library? If you are going to use a lib for simple UDP, I'd just look there. I wish I thought of suggesting that sooner.