Re: Socket connect vs. bind? What is the difference

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I told you, the client and server do not share the same IPEndPoint (IP
Address and Port).

Also, you should remember that I told you that UDP is a connectionless
protocol, and there is no need to call Connect.

If you look at the 2 tutorials I referenced, you'll notice that Connect is
never called in either one.

I would suggest simplifying your code and making sure that the simplest form
of it is solid before adding all the complexity. The more variables there
are in a problem, the more points of failure there are, and the harder it is
to isolate the problem. You might benefit from reading the following article
on Wikipedia about Unit Testing:

http://en.wikipedia.org/wiki/Unit_testing

The basic principle (like so many basic principles) is simple: Test each
piece before you try to combine them. This will save you from getting grey
hair at an early age, and speed up your development process as well!

While it is not necessary to follow the patterns and procedures mentioned in
the article and related links, it is very important to understand the KISS
principle of Unit Testing. I, for example, over many years, have developed
my own methodology for Unit Testing (long before it became part of the "Pop
Programming Culture" buzz-word dictionary), and use it whenever I do
development. As my Uncle Chutney sez, "Big things are made up of lots of
Little things." Make little things. Test them and make sure they work. Then
put a couple of little things together. Test that and make sure it works.
Add more little things to it. Eventually, you will have eaten the elephant,
one byte at a time (and in far less time, without getting indigestion!).

Perhaps a good start might be to take the code from the MSDN article
verbatim. Run it. Make sure it works. Tweak it to something closer to what
you're working on. Test that. Tweak some more. Test that. Before you know
it, you'll be standing on the top of Mt. Everest, wishing you had documented
the code along the way. ;-)

Another alternative (easier but less instructive) would be to use the
System.Net.Sockets.UdpClient class. This is a little higher-level class
which handles most of the low-level Socket work for you. See:

http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mmagdits@xxxxxxxxxxx> wrote in message
news:1149718257.059705.95420@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I understand the majority of what your saying. Right now I have two
classes inheriting from a base class which has the connect method for
the socket. The two child classes represent a sender, and a receiver.
Now, say I want to test this sender receiver locally, I kinda have it
set up to default to values for testing puposes. And I don't want to
use 127.0.0.1. Both send and receive use the same connect method.
When I say connect, it's really bind and connect depending on a flag.
Here, maybe looking at the code might help. Here's the connect method.

private bool Connect()
{
bool connectSuccessful = false;
IPAddress localIp = null, remoteIp = null;
try
{

//Making sure the remoteIP is there, otherwise using a default.
if(this._multiCastIP == null || this._multiCastIP.Length <= 0)
{
remoteIp = IPAddress.Parse(MCAST_ADDRESS_A);
}
else
{
remoteIp = IPAddress.Parse(this._multiCastIP);
}

//Making sure the localIP is there, otherwise using a default.
if(this._localMachineIP == null || this._localMachineIP.Length <= 0)
{
localIp = GetLocalIP();
}
else
{
localIp = IPAddress.Parse(this._localMachineIP);
}

//Making sure the port is there, otherwise using a default.
if(this._port == null || this._port.Length <= 0)
{
this._port = MCAST_PORT_A;
}

//Just making sure the Time TO Live is greater then 0
if(this._multiCastTtl <= 0)
{
this._multiCastTtl = DEFAULT_TTL;
}

_socket = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram,ProtocolType.Udp );
IPEndPoint ipep = new IPEndPoint(localIp, int.Parse(this._port));
if(this._isSend)
{
_socket.Connect(ipep);
}
else
{
_socket.Bind(ipep);
}
_socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);


MulticastOption mcastOption = new MulticastOption( remoteIp, localIp
);
_socket.SetSocketOption( SocketOptionLevel.IP,
SocketOptionName.AddMembership, mcastOption );
_socket.SetSocketOption( SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, this._multiCastTtl );
connectSuccessful = true;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

return connectSuccessful;
}

sorry about the bas formatting, and here are the default values I use.
plus, I just grab the local IP address of the machine I'm on.

public static String MCAST_ADDRESS_A = "227.1.2.3";
public static String MCAST_ADDRESS_B = "227.5.7.11";

public static String MCAST_PORT_A = "6203";
public static String MCAST_PORT_B = "6204";

Now, if I try to run both on the same box, with both the send and
receive binding to the same IP/PORT I get an error, but if I connect
the sender, I don't. I really appreciate the help.



.



Relevant Pages

  • Re: Socket connect vs. bind? What is the difference
    ... //Making sure the remoteIP is there, ... //Making sure the localIP is there, ... _socket = new Socket(AddressFamily.InterNetwork, ...
    (microsoft.public.dotnet.languages.csharp)
  • GRE issues
    ... Trying to set up GRE here for routing a /29 to the house. ... LocalIP and RemoteIP are internet IP addresses/blocks per ... ifconfig gre1 tunnel ...
    (freebsd-questions)