[...]
So I've got an IP address of the end device and a port number.
192.168.1.2
5281 Can someone explain to me the difference between a port number and
a
socket, and can anyone point to some example programs.
IMHO, the reference that has been posted is misleading.
For one, a port is not restricted to use within TCP, as the other post
implies. A port along with an IP address is the uniquely identified
"endpoint" for the TCP/IP network protocol, which includes TCP and UDP
(both of which are implemented on top of IP, which is if I recall
correctly where the port/IP address behavior actually comes from). In
addition, TCP/IP isn't the only network protocol that has the concept of a
port. In fact, all the protocols I can think of off the top of my head
use _some_ kind of "computer address with server-entity" semantics, where
there's a computer address along with a port or something equivalent to a
port.
For another, IMHO a "socket" is not a network protocol characteristic.
You can implement something that uses TCP/IP without using sockets.
Hardly anyone does, of course, but you can. The Berkeley sockets API on
which Winsock is based is a widely used standard outside of Windows, and
of course on Windows we use Winsock or things derived from it (.NET Socket
class and its related classes). So it's incorrect to call the socket a
"unique identifier throughout all networks connected together". The
socket isn't a unique identifier at all; it's an operating system object
that allows use of a networking API. It does encapsulate a unique address
(in most cases), but it is not in and of itself a unique address.
So, how would I define "socket" and "port"? First, let's address why
someone might be confused about the difference in the first place: the
words "socket" and "port" are, outside the computer world, used in very
similar ways. A socket is something you can plug something else into, and
a port can also be something you can plug something else into. IMHO, a
socket is more "this part fits perfectly into this other part", while a
"port" is more of a "this part is a general-purpose receiver for this
other part", but the words have been blurred through common usage and
share similar linguistic basis.
But in the context of computer networking, the two are _very_ different
ideas. It's in some ways unfortunate that two so-similarly-used words
were applied to these two distinct ideas, as it can lead to confusion.
The socket is, as I mention above, an operating system object. It is the
object with which an application binds to a specific OS-based i/o source.
In the same way that a Graphics object connects you to the screen, and a
FileStream object connects you to a file, a Socket object connects you to
the network. And just as the Graphics object is not the screen itself,
nor is the FileStream object the actual file, neither is the Socket object
the actual network address you're using. It's just a way for your
application to reference that address and the resources associated with it
that the OS provides to allow your application to use that address.
It is, in some ways, an abstraction of the address. But it is also an
abstraction of a bunch of other things that actually allow the application
to use the address without much difficulty. You could use the address
without the Socket, and in fact you can have Sockets that are not tied to
a specific TCP/IP endpoint address (ie IP address with a port number).
This is only possible because it's an abstraction.
So what's a port? Here, the NASA reference is more accurate.
Essentially, the port is what allows a variety of applications sharing a
single network adapter to still be treated uniquely. It is part of the
endpoint address, along with the IP address(es) for the host. It's sort
of like a household where there's one phone number, but multiple
residents. You call the phone number, and then ask for the specific
person you want to talk to. Though, in that scenario only one person can
be on the phone at a time. So I guess it's more like a business phone
with a "private branch exchange", where there's a single phone number
people can call, but an operator that can redirect the call internally
allows for that one phone number to be reused among many people, even
simultaneously.
The port is what allows a specific IP datagram (on which all of TCP/IP is
based) to then, upon having arrived at the correct computer with a
specific network adapter IP address, be routed to the appropriate process
running on that computer, with a specific port number.
So that's the difference. As a final note, you may run into the idea of
the "port" in other contexts. In particular proxies and routers that
treat different port numbers differently. While there's no inherit
behavior in the port number other than I describe above, in fact it's
useful for common protocols to always use the same port number. Things
like FTP (file transfer), HTTP (web server), POP and SMTP (email), NNTP
(news servers), etc. all have port numbers that are assigned to them for
common use. There's no absolute requirement that those protocols be
handled on those ports, but using the same port number all the time for
those protocols makes using those protocols much simpler, as the client
only needs to know the destination IP address, and can assume the port
number for a given protocol. So, servers of those protocols almost always
use the standard port for the given protocol.
A client, on the other hand, does not need a specific port number. The
client, being the endpoint that contacts the server initially, can be
assigned an arbitrary port number. The server, upon receiving the initial
contact from the client, will know at that point what the client's IP
address and port number is, and so the port number (and the IP address,
for that matter) can be anything. Only the server, which needs to be
findable in order to make first contact with, has to have a consistent,
well-known endpoint address (IP address with port number).
So, with various ports being assigned to specific applications by
convention, that allows proxy servers and NAT routers and other kinds of
networking objects to treat particular port numbers in special ways.
Usually this involves either forwarding all traffic on a given port to a
given computer, or blocking all traffic on a given port. But there are
other things that can be done with this knowledge of how the port number
is assigned.
Sorry for the essay, especially as I suppose it's a bit out of context
here in the C# newsgroup. But I guess if we're going to answer any sort
of Socket class questions here, the above is as on-topic as any other
Socket class question.
Hope it helps.
Pete