UDP Connections

Started by
7 comments, last by GeniX 23 years, 9 months ago
Hey all, Im trying to listen on a bound socket of type SOCK_DGRAM... everything I call from the socket right thru to the listen() returns no errors. So one would assume it is indeed listening... But then my client is trying to connect a socket of type SOCK_DGRAM to the server. Now all the calls from the socket() thru the DNS lookup and all - they succeed. Even the connection succeeds (apparently) and it sends the data. Strange thing is that my server doesnt get a connection returned from its listen() call. In fact if I *dont* run the server, the client is somehow still able to connect to the server, and send a UDP packet. ??? Im using Visual C++ 6.0 under Windows 2000. I did remember to call the WSAStartup() and WSACleanup() stuff (or the other networking goodies would return errors). The client and server are, for testing purposes, being run on the same machine. The client is being told to connect to address "localhost" on port "1234" which is the same port as the server is listening on. I am now stuck for how and why and would appreciate any comments. regards, GeniX
regards,GeniXwww.cryo-genix.net
Advertisement
As an extra I forgot to mention --

The socket returned to both server and client applications for use is 100.

Im sure the client cant use 100 if the server already has it bound, and is listening on it?



regards,

GeniX
regards,GeniXwww.cryo-genix.net
With SOCK_DGRAM, you don''t connect(). UDP is a ''connectionless'' protocol. Just send...

Here''s code that will send a packet to a specified IP address & port number:

    int IP[4];if(sscanf(m_DestinationIP, "%d.%d.%d.%d", &IP[0],&IP[1],&IP[2],&IP[3]) != 4){	AfxMessageBox("Invalid Destination IP Address", MB_ICONSTOP);	return;}int Dest_Port = atoi(m_DestinationPort);ASSERT((Dest_Port >= 1) && (Dest_Port) <= 65535));CAsyncSocket SendSock;SendSock.Create(0, SOCK_DGRAM);SendSock.SendTo(m_TransmitText, m_TransmitText.GetLength(), Dest_Port, m_DestinationIP);    


// CHRIS
// CHRIS [win32mfc]
Forgot to mention, you don''t need to bind() or listen() on the server side, and you don''t need to connect() on the client side (like I said in the previous message.)

On the server side, all you need to do is Create().
On the client side, Create() and then SendTo().

Two UDP sockets can be creat()ed for the same port on the same machine; sounds crazy but I''ve done it. Remember, though, on the client side you don''t need to specify a port number -- one will be assigned automatically. Hence the "Create(0, SOCK_DGRAM)" on the client.

Hope that helps..

// CHRIS
// CHRIS [win32mfc]
Yes I always thought that UDP was connectionless, but my reference was reading:

http://www.ecst.csuchico.edu/~beej/guide/net/

I read that one can perform a connection with UDP the same as TCP, only for non-reliable traffic.

As for your example - thanks Ill have to do some reading on it as I have not used CAsyncSocket before.

Ive been using the (I believe its POSIX) SOCKET with Send and Recv.




regards,

GeniX
regards,GeniXwww.cryo-genix.net
A connected UDP socket will give you only packets from the connected host ip, port#. Also, it enables you to receive asynchronous errors.
Yeah, CAsycSocket is really a thin wrapper around the SOCKET handle. Same with the CSocket class. In fact, if you're not using MFC in your application(s), it's probably not very hard to 'rip' out those classes from the MFC library and just include them into your own program.

If you want a decent example, I wrote a UDP test program. It's a dialog app that lets you bind to whatever port you want and then send UDP messages (to your self or another copy of the program running on a different machine.) You can send 'batches' of messages too- just enter the number to send and the number of milliseconds to wait between each send. I wrote it to learn how to do UDP and also to test the 'reliability factor'. When I used it on my LAN at work, I didn't lose any packets, ever. Over the internet to my home machine on a cable modem, I lost only about 1 or 2 percent.

It's an MFC project, BTW. Not sure if you're into (or like) MFC.

// CHRIS


Edited by - win32mfc on July 12, 2000 10:55:06 AM
// CHRIS [win32mfc]
Thanks ppl!

I seem to have figured it out anyways :-)

You cant do an actual connection, but you can connect() a udp socket and then use the send and recv functions.

Yeah and 1 or 2% packet loss aint much anyways



regards,

GeniX
regards,GeniXwww.cryo-genix.net
quote:Original post by GeniX

Thanks ppl!

I seem to have figured it out anyways :-)

You cant do an actual connection, but you can connect() a udp socket and then use the send and recv functions.

Yeah and 1 or 2% packet loss aint much anyways



regards,

GeniX


Where are you getting this 1 or 2% packet loss? On a LAN?
This ratio may increase as you move from a LAN to the internet or WAN.
I would do some testing to determine if this is the case.

Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous

This topic is closed to new replies.

Advertisement