c++ winsock udp sockets problem
Posted: Tue Jul 21, 2009 9:36 pm
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:
Data receiver:
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.
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
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...
}
What can i do?
I would thank A LOT any help...
Thanks guys.