Re: Socket connect vs. bind? What is the difference
- From: "DaTurk" <mmagdits@xxxxxxxxxxx>
- Date: 7 Jun 2006 15:10:57 -0700
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.
.
- Follow-Ups:
- Re: Socket connect vs. bind? What is the difference
- From: Kevin Spencer
- Re: Socket connect vs. bind? What is the difference
- References:
- Socket connect vs. bind? What is the difference
- From: DaTurk
- Re: Socket connect vs. bind? What is the difference
- From: Kevin Spencer
- Re: Socket connect vs. bind? What is the difference
- From: DaTurk
- Re: Socket connect vs. bind? What is the difference
- From: Kevin Spencer
- Re: Socket connect vs. bind? What is the difference
- From: DaTurk
- Re: Socket connect vs. bind? What is the difference
- From: Kevin Spencer
- Socket connect vs. bind? What is the difference
- Prev by Date: Re: Book recommendation
- Next by Date: Re: Boolean operations on Enumerations
- Previous by thread: Re: Socket connect vs. bind? What is the difference
- Next by thread: Re: Socket connect vs. bind? What is the difference
- Index(es):
Relevant Pages
|