Hi!
I am compeltely new to netwrork programming, so this ,ight be a really stupid question...
I an trying to make an application that will both send and receive UDP datagrams. I thought I could use the same socket. I create socket, bind it to my port address, and wait for messages (recvfrom). This part works fine. Then after I process a message, I want tu use sendto using the same port and this does not work. I try to send to a multicast address. Sendto() does not return an error, but I cannot capture any packets send using Ethereal.
Is it OK to use the same socket for both send and receive? If yes, I can supply with the code I wrote.
Thanks!
ahoodin
December 14th, 2007, 10:14 AM
Yes, you can call sendto() and recvfrom() is by using the same socket. That is a correct practice.
You best post your code.
Luke1410
December 14th, 2007, 10:20 AM
Is it OK to use the same socket for both send and receive? If yes, I can supply with the code I wrote.
Short answer: yes. You can use a single socket to send and receive data, if the port is the same (on the server and on the clients).
How about starting with sending data to a simple client (rather than to a multicast group), to troubleshoot your problem)?
ahoodin
December 14th, 2007, 10:29 AM
Well perhaps we can fix her code. I am assuming that she perhaps has different client ip address vars referenced, one in her sendto() and another recvfrom(). :D
Elise5
December 14th, 2007, 11:24 AM
Can you please be more accurate on the last comment?
ahoodin
December 14th, 2007, 11:54 AM
If yes, I can supply with the code I wrote.
Honestly I just want to help you with your actual problem, not a theoretical problem. So I can not be more accurate until you post the code you speak of...
Also dont forget to use the code tags.
Luke1410
December 14th, 2007, 12:05 PM
If I got ahoodin right, he referred to the possibility that you specified incorrect ips when sending/receiving data (i.e. the first parameter given to recvfrom() or the last two parameters to sendto() are incorrect).
Though this might be the case, as far as I'm concerned this shouldn't be the only reason for Ethereal not displaying any data (since with UDP sending data with an incorrect ip, the data should still be sent over the net) - unless of course you set up Ethereal on the receiving host rather than the sending one.
henky@nok.co.id
December 14th, 2007, 09:18 PM
Can you please be more accurate on the last comment?
One of important thing when fixing the problem is knowing the root cause of
problem. Without attaching the actual code, we are here just assuming what
you have done and attempting to give a hint.
This will be different if your question is about concept or theory. We can
answer without assumption but based-on theory, concept, specification, etc.
Elise5
December 15th, 2007, 05:36 PM
Of course that It is dissicult to come with advice without seeing my code. So, here it comes.
The receive task looks like this:
DWORD WINAPI DlcpUdpReceiveMsgThr( void* param )
{
struct sockaddr_in udpClientAddr;
int udpClientLen;
unsigned char udpBuffer[UDP_BUF_SIZE];
int rcvdMsgSize;
// Set the size of the in-out parameter
udpClientLen = sizeof(struct sockaddr_in);
for (;;) { // Run forever
// Block until receive message from a client
if ((rcvdMsgSize = recvfrom(gServerSock,(char*)udpBuffer,UDP_BUF_SIZE,0,(struct sockaddr *)&udpClientAddr,&udpClientLen)) < 0) {
printf("recvfromFail = %d\n",WSAGetLastError());
DieWithError("rcvfrom failed\n",gServerSock);
return 1;
}
//Check the massage content, if mgmt, pass it to mgmt task;
DlcpCheckComonHdrAndPassMsg(udpBuffer);
Sleep(100);
} // for (;;)
// Not Reached
}
The send function, used by another task:
int DLCPUdpSendMsg( unsigned int destIpAddr, unsigned char * udpBuf, int bufLen){
I have given up sending multicast. I managed to see unicast some packets leave the maschine (looks like it depends on the destination address). The packets seen in Ethereal are erroneous, the IP checksum is 0. The rest of the content seems to be OK. I guess there si something very basically wrong with the recvAddr I use ?! So, it might be very obvious to you, but I am very new to all this stuff :-)
Luke1410
December 16th, 2007, 10:23 AM
If I may point you again to my response in your other thread: http://www.codeguru.com/forum/showthread.php?t=441411
The information given there should be enough to realize the things you left out.
Elise5
December 16th, 2007, 02:30 PM
Thanks Luke, that is a pretty informative article, and I see I have left out some essentinal things. I am going to rewrite the code, and let you know if I managed to do it right this time.
Elise5
December 17th, 2007, 01:32 AM
Hi again! Well, I hav read your article Dennis, and I guess I need a new pair of glasses or a bigger brain (or both). I still do not see what I am missing for my unicast packets. I see that the set up is uncomplete for to send multicast, but I do not undestand why unicast is not working?
Luke1410
December 17th, 2007, 02:39 AM
Well, your code looks ok as far as I see... But it's been already a couple of months since I last worked with the Winsock API, so I very well might miss something.
However if I were you I'd concentrate on the other parts of the code (how these functions are called, the setup of the sockets, etc.). Since Ethereal returned an incorrect IP with your packet: Did you make sure that the ip address given to the functions is already in Network Byte Order?
For further information concerning unicast, I can suggest you this article which covers the basics about networking:
http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html
Elise5
December 17th, 2007, 07:14 AM
Hello again! partly good news: unicast now works properly. Well, I was not aware of the fact that Ethereal on the sender side sometimes presents "wrong data" (IP cehcksum 0). I started Etherel at the receiver, and there was nothing wrong with the IP packet. The reason I got packets in return was the fact that I have no matching receiver application (I was juts sending to a RecvUdp.exe which do not understand the dat I sent in my UDP packet).
So, unicast is OK, but multicast still not. I don't need to joing the multicast group I am sending to? Is it OK to disable the looping backet packets (IP_MULTICAST_LOOP)?
Is it ok that the options are set after socket bind()?
As earlier, no packets are actually being sent, and sendto reports no error. Nothing wrong when setting the socket options above.
Could it be somthing with my interface card?
MikeAThon
December 17th, 2007, 11:12 AM
Here is an article that describes multicasting: "Advanced WinSock Multiplayer Game Programming: Multicasting" at http://www.gamedev.net/reference/articles/article1587.asp
Maybe that will help explain things a bit.
Mike
Elise5
December 18th, 2007, 07:09 AM
Hi again! Yes, I have already seen the article, but thanks.
I have come a bit further in deciding what is not working. I see that the multicast options I am trying to send are actually not being set athough I get no error when setting them. Right after I bind the socket and set the multicast options, I try to send a multicast, and I check the options with getsockopt. None of the options is actually being set. Without options being set, no wonder that I see no mcast packets leave the machine. The question is why are they not being set?
So, here is the code (attached...), and if anyone sees anything missing, or wrong order of setting things let me know. I am suspecting old lib version or something like that.
Luke1410
December 18th, 2007, 12:37 PM
don't need to joing the multicast group I am sending to?
No, you don't.
Is it OK to disable the looping backet packets (IP_MULTICAST_LOOP)?
Google is your friend: http://tldp.org/HOWTO/Multicast-HOWTO-6.html
Is it ok that the options are set after socket bind()?
That depends on the option.
Could it be somthing with my interface card?
It could be everything which is part of your network topology. OS, NIC, Router, ... http://www.cisco.com/en/US/tech/tk828/tech_brief09186a00800e9952.html is an interesting article when you need to know more details about the low-level stuff behind multicasting.
Elise5
December 20th, 2007, 05:44 AM
I cannot find the information on which options need to be set before bind, which after. Is there any specific order to follow?
ahoodin
December 20th, 2007, 08:22 AM
This topic has gone far too long without yielding any result. Here this link has source code shows all the ins and outs. Also buy that book if you want a short concise reference.