Archived

This topic is now archived and is closed to further replies.

ANTnet

[UDP] Send to an IP

Recommended Posts

Hi, My server is to manage many clients at the same time. But I don''t manage to send to a particular IP with sendto This is the code for the server SOCKET s; SOCKADDR_IN localaddr, useraddr, tempaddr; int addrsize; s = socket(AF_INET, SOCK_DGRAM, 0); addrsize = sizeof(SOCKADDR_IN); localaddr.sin_family = AF_INET; localaddr.sin_addr.s_addr = INADDR_ANY; localaddr.sin_port = htons(5000); bind(s, (LPSOCKADDR)&localaddr, sizeof(localaddr)); tempaddr.sin_family = AF_INET; tempaddr.sin_port = htons(5000); r = recvfrom(s, buf, sizeof(buf), 0, (LPSOCKADDR)&useraddr, &addrsize); ... //This sendto is working, useraddr is filled by recvfrom sendto(s, "test", 4, 0, (LPSOCKADDR)&useraddr, addrsize); //This one is not working :-( I don''t understand... tempaddr.sin_addr.s_addr = inet_addr("192.168.0.246"); sendto(s, "test", 4, 0, (LPSOCKADDR)&tempaddr, addrsize); The client can send to this server with no problems: s = socket(AF_INET, SOCK_DGRAM, 0); localaddr.sin_family = AF_INET; localaddr.sin_addr.s_addr = inet_addr("192.168.0.1"); localaddr.sin_port = htons(5000); bind(s, (LPSOCKADDR)&localaddr, sizeof(localaddr)); size = sizeof(localaddr); sendto(s, buf, strlen(buf), 0, (LPSOCKADDR)&localaddr, size); How can I send to IP x.x.x.x, shall I have to create a socket for each IP I want to send something to ? Thanks ANT

Share this post


Link to post
Share on other sites
inet_addr() converts the Internet host address from the standard numbers-and-dots notation into binary data and stores it in the structure of type in_addr (not u_uint32_t):
tempaddr.sin_addr.s_addr = inet_addr("192.168.0.246"); // <--- this should work

Post errors you get.

"I'll be Bach!" (c) Johann Sebastian Schwarzenegger



[edited by - x33 on May 1, 2003 6:14:33 AM]

Share this post


Link to post
Share on other sites
this works fine for me


m_sendaddress.sin_family = AF_INET;
m_sendaddress.sin_port = htons(m_iPort);
m_sendaddress.sin_addr.s_addr = inet_addr(sz);
memset(&(m_sendaddress.sin_zero), ''\0'', 8); // zero the rest of the struct

iNumbytes=sendto(m_Socket, buf, strlen(buf), 0,(struct sockaddr *)&m_sendaddress, sizeof(struct sockaddr));

Share this post


Link to post
Share on other sites
Doesn''t work, the ip specified doesn''t receive anything :-(

x33 > I don''t get any error, it''s just that I send to an ip and the client on this ip receives anything

Share this post


Link to post
Share on other sites
try adding 'addrsize = sizeof (tempaddr);' before this:
tempaddr.sin_addr.s_addr = inet_addr("192.168.0.246");
sendto(s, "test", 4, 0, (LPSOCKADDR)&tempaddr, addrsize);

As for the network configuration i meant firewalling, routing, etc.

"I'll be Bach!" (c) Johann Sebastian Schwarzenegger

[edited by - x33 on May 1, 2003 7:35:13 PM]

Share this post


Link to post
Share on other sites
for UDP, you don''t have to create a seperate socket for every peer u want to connect to.

when u call sendto, pass sizeof(struct sockaddr_in) or sizeof(SOCKADDR_IN) as the 4th parameter. it doesnt need a pointer and that way u know that it always gets the correct amount. if u dont use a constant value, then u have to make sure the variable doesnt get changed between calls to sendto.

i assume u are checking the return value from the 2nd sendto? there must be a process on the host 192.168.0.246 which is bound to port 5000. also, is tempaddr modified between when it first gets set and when u make the 2nd sendto call? i know these are elementary questions, but sometimes i waste entire evenings on forehead slappers.

Share this post


Link to post
Share on other sites