473,703 Members | 3,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket connect vs. bind? What is the difference

I am makeing a Multicast server client setup and was wondering what the
difference is between Socket.Connect, and Socket.Bind.

It may be a stupid question, but I was just curious. Because I tried a
test, and I can't bind two sockets to the same port/ip but I can
connect with one socket, and bind with another to the same ip/port.

Thanks

Jun 6 '06 #1
14 25262
Bind associates a Socket with a local IPEndPoint. Connect connects a Socket
to a remote IPEndPoint.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** **************@ h76g2000cwa.goo glegroups.com.. .
I am makeing a Multicast server client setup and was wondering what the
difference is between Socket.Connect, and Socket.Bind.

It may be a stupid question, but I was just curious. Because I tried a
test, and I can't bind two sockets to the same port/ip but I can
connect with one socket, and bind with another to the same ip/port.

Thanks

Jun 6 '06 #2
Thanks for the reponse, but what exactly do you mean by associate, and
connect. I guess what I'm asking is what is the difference, and when
would you want to use one over the other, what situations.

At this point I'm using sockets to create a multicast socket server
setup, and in examples I saw, the socket that sends typically connects,
and the socket that listens binds.

Jun 7 '06 #3
That's correct, because the listener is the server. You can have multiple
clients connect to the server socket. It's a little more complicated than
that, but basically, you want to bind your server to the server socket and
set it to Listen. Then your clients will Connect to the server socket to
interact with the server application. The server calls Accept for each
client that it wants to accept when they attempt to connect. Each client
will have its own IPEndPoint, which is made up of an IP address and a Port.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Thanks for the reponse, but what exactly do you mean by associate, and
connect. I guess what I'm asking is what is the difference, and when
would you want to use one over the other, what situations.

At this point I'm using sockets to create a multicast socket server
setup, and in examples I saw, the socket that sends typically connects,
and the socket that listens binds.

Jun 7 '06 #4
Yes, but what if your just subscribing a socket to a multi cast group.
So that the one socket will send to the group, and many sockets will be
able to read from the group. Would you need to connect as well? And
does it matter that it's multicast over UDP?

And not to sound daft, but I still don't really understand the
difference between associating with and endpoint rather then connecting
to an endpoint. Thanks again for the reply.

Kevin Spencer wrote:
That's correct, because the listener is the server. You can have multiple
clients connect to the server socket. It's a little more complicated than
that, but basically, you want to bind your server to the server socket and
set it to Listen. Then your clients will Connect to the server socket to
interact with the server application. The server calls Accept for each
client that it wants to accept when they attempt to connect. Each client
will have its own IPEndPoint, which is made up of an IP address and a Port.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Thanks for the reponse, but what exactly do you mean by associate, and
connect. I guess what I'm asking is what is the difference, and when
would you want to use one over the other, what situations.

At this point I'm using sockets to create a multicast socket server
setup, and in examples I saw, the socket that sends typically connects,
and the socket that listens binds.


Jun 7 '06 #5
Actually, if you're doing UDP Multicast, you don't need to connect. UDP is a
connectionless protocol.

Let me explain a little bit. Network IP communications involve 2 processes
on 2 different IPEndPoints. Each IPEndPoint represents an IP address and a
Port number. They can be on the same or different machines, as long as each
is unique. TCP is a connection-oriented protocol, which requires the client
to call Connect to establish a TCP connection to the remote IPEndPoint. UDP
is connectionless, so it isn't even necessary to establish a connection to
the remote IPEndPoint.

The Bind method is used to associate (or "bind") a Socket to a *local*
IPEndPoint. In the case of the "server," there is a single process bound to
an IPEndPoint. In the case of the "clients" there is generally one "client"
per client IPEndPoint. Each Socket sends to a remote IPEndPoint and receives
from its own local IPEndPoint.

The Sockets class is fairly flexible, and can be used in any of several
ways. In some cases, there is no "right way," but may be several "right
ways" to communicate, particularly in the case of UDP. The local IPEndPoint
can be set in any of several ways. For example, a client can Bind to a local
IPEndPoint, which means that the client picks the Port it is bound to
(associated with). It can be set with an overload of the Socket constructor.
It can be set by calling "Connect," but again, you don't need to call
"Connect" to interact with a multicast server. UDP allows each member to
just begin sending and receiving. It is up to the processes to know what to
expect from the remote process, when to send, what to send, when to receive,
and what to receive. Dropped packets must be handled by the process, as the
protocol doesn't handle them. It is basically a very primitive, but
fast/powerful network protocol. It requires the processes to be more
responsible for themselves, but the payoff is in the communication speed.
Data is sent and received in DataGrams.

Here are a couple of references concerning UDP and multicast that may help:

http://msdn.microsoft.com/msdnmag/issues/06/02/UDP/
http://zeus.osix.net/modules/article/?id=409

And the RFC for UDP:
http://www.faqs.org/rfcs/rfc768.html

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** *************@f 6g2000cwb.googl egroups.com...
Yes, but what if your just subscribing a socket to a multi cast group.
So that the one socket will send to the group, and many sockets will be
able to read from the group. Would you need to connect as well? And
does it matter that it's multicast over UDP?

And not to sound daft, but I still don't really understand the
difference between associating with and endpoint rather then connecting
to an endpoint. Thanks again for the reply.

Kevin Spencer wrote:
That's correct, because the listener is the server. You can have multiple
clients connect to the server socket. It's a little more complicated than
that, but basically, you want to bind your server to the server socket
and
set it to Listen. Then your clients will Connect to the server socket to
interact with the server application. The server calls Accept for each
client that it wants to accept when they attempt to connect. Each client
will have its own IPEndPoint, which is made up of an IP address and a
Port.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
> Thanks for the reponse, but what exactly do you mean by associate, and
> connect. I guess what I'm asking is what is the difference, and when
> would you want to use one over the other, what situations.
>
> At this point I'm using sockets to create a multicast socket server
> setup, and in examples I saw, the socket that sends typically connects,
> and the socket that listens binds.
>

Jun 7 '06 #6
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 connectSuccessf ul = false;
IPAddress localIp = null, remoteIp = null;
try
{

//Making sure the remoteIP is there, otherwise using a default.
if(this._multiC astIP == null || this._multiCast IP.Length <= 0)
{
remoteIp = IPAddress.Parse (MCAST_ADDRESS_ A);
}
else
{
remoteIp = IPAddress.Parse (this._multiCas tIP);
}

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

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

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

_socket = new Socket( AddressFamily.I nterNetwork,
SocketType.Dgra m,ProtocolType. Udp );
IPEndPoint ipep = new IPEndPoint(loca lIp, int.Parse(this. _port));
if(this._isSend )
{
_socket.Connect (ipep);
}
else
{
_socket.Bind(ip ep);
}
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
MulticastOption mcastOption = new MulticastOption ( remoteIp, localIp
);
_socket.SetSock etOption( SocketOptionLev el.IP,
SocketOptionNam e.AddMembership , mcastOption );
_socket.SetSock etOption( SocketOptionLev el.IP,
SocketOptionNam e.MulticastTime ToLive, this._multiCast Ttl );
connectSuccessf ul = true;
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}

return connectSuccessf ul;
}

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.

Jun 7 '06 #7
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.Sock ets.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/lib...udpclient.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"DaTurk" <mm******@hotma il.com> wrote in message
news:11******** *************@y 43g2000cwc.goog legroups.com...
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 connectSuccessf ul = false;
IPAddress localIp = null, remoteIp = null;
try
{

//Making sure the remoteIP is there, otherwise using a default.
if(this._multiC astIP == null || this._multiCast IP.Length <= 0)
{
remoteIp = IPAddress.Parse (MCAST_ADDRESS_ A);
}
else
{
remoteIp = IPAddress.Parse (this._multiCas tIP);
}

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

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

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

_socket = new Socket( AddressFamily.I nterNetwork,
SocketType.Dgra m,ProtocolType. Udp );
IPEndPoint ipep = new IPEndPoint(loca lIp, int.Parse(this. _port));
if(this._isSend )
{
_socket.Connect (ipep);
}
else
{
_socket.Bind(ip ep);
}
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
MulticastOption mcastOption = new MulticastOption ( remoteIp, localIp
);
_socket.SetSock etOption( SocketOptionLev el.IP,
SocketOptionNam e.AddMembership , mcastOption );
_socket.SetSock etOption( SocketOptionLev el.IP,
SocketOptionNam e.MulticastTime ToLive, this._multiCast Ttl );
connectSuccessf ul = true;
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}

return connectSuccessf ul;
}

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.

Jun 8 '06 #8
Thank you very much for the information, I really appreciate the
assistance.

Jun 8 '06 #9
One more question, I read through the examples you sent me to, and I
found them very helpful, but was a little curious about a couple
things.

In all the examples, when doing a MultiCast send the socket never binds
to the endpoint it creates, it just sends to it. Is it not good form
to bind while sending?

I was under the impression you needed to create a localendpoint to the
local address of the machine, in case there were multiple NIC's in the
machine, And bind to that, then create a MultiCastOption with the
multicast address, and the local address.

like this
MulticastOption mcastOption = new MulticastOption ( remoteIp, localIp );
_socket.SetSock etOption( SocketOptionLev el.IP,
SocketOptionNam e.AddMembership , mcastOption );

Jun 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5266
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
0
1419
by: yaron | last post by:
Hi, I want to use non-blocking socket operations (and not async socket operations) When i use this code : socket.Blocking = false; socket.Connect(remote); i get this error :
9
1629
by: yaron | last post by:
Hi, 1. It seems to me that there is a bug when using non-blocking socket connect operation, because the LocalEndPoint, RemoteEndPoint of the Socket are nulls on the client side although that the client and the server are already connected. 2. If it is a bug, is there any hotfix for this ? Thanks.
2
2158
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
1
1718
by: HABJAN ®iga | last post by:
Hello, how can i bind a socket for listen on some port, and all addresses : In the sample i bind myself to localhost(127.0.0.1) on port 5500. It works fine when connecting to localhost, but if i connect from another computer, or i connect by computer name, the accept callback never happens. Private sckL As Socket
3
3417
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
0
1204
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public IPScanner() { } private void connect(object data)
9
18552
by: bukzor | last post by:
I'm having an issue where my program hangs while doing socket.connect() for a couple minutes, then times out the connection and crashes. I'm connecting to an apache2 process on the same machine, for testing. When looking at netstat, the socket is in the SYN_SENT state, like this: $netstat -a -tcp tcp 0 0 *:www *:* LISTEN 7635/apache2 tcp 0 1 bukzor:38234 adsl-75-61-84-249.d:www
0
11356
by: jinsue | last post by:
Hi all, I am having trouble getting a client/server relationship to connect; receiving the above error. Python 2.6.5, Win7 On one end we have the socket being bound and that is all working with correct (IP) and as far as I know, correct socket settings. def listen_for_peer_setup(server_connection): listening_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listening_socket.setsockopt(socket.SOL_SOCKET,...
0
8674
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6595
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.