networking - Confused between ports and sockets -
ok when tried research on ip addresses, ports, , sockets, got out of it:
ip addresses used map different devices on network.
port numbers used specific application on hosts.
sockets combination of two..
what don't understand if ports connect specific application, should have 1 port number per application right? example port 80 used http, if application using port it's listening http requests right? happens if more 1 person tries access it? sockets , ports have me confused lot..
a socket abstraction used in software make easier programmers send , receive data through networks. interface, use in application-level code, access underlying network protocol implementations provided os , language runtime.
the tcp protocol, ip protocol, , other popular network protocols not, in of themselves, have concept of "sockets". "sockets" concept implementers of tcp/ip came with.
so concept of "socket"? basically, object can write data to, , read data from. "opening" socket means creating 1 of objects in program's memory. can "close" socket, means freeing system resources object uses behind scenes.
some kinds of sockets can "bound" local , remote addresses, can think of setting data fields, or properties, on socket object. value of fields affect happens when read or write socket.
in unix, there various kinds of sockets. if "open" tcp socket, "bind" local , remote addresses (and ports), , write data it, libraries/os package data tcp segment , send out through whichever network interface matches local address "bound" socket to. if "open" ip socket, , write data it, data packaged ip packet (without added tcp headers) , sent out. if open "raw", link-level socket, , write it, data sent out payload of link-level frame, minus ip and tcp headers. there "unix domain sockets". if open 1 of , write it, data passed directly through system memory process on same machine.
so although used in non-oo languages c, sockets perfect example of oo languages call "polymorphism". if ever have trouble explaining "polymorphism" someone, teach them network sockets.
"ports" different concept. idea of "ports" built in tcp , other transport protocols.
others may give more high-falutin', , perhaps more technically accurate, definitions of "port". here 1 totally down earth:
a "port" number appears in tcp headers on tcp segment. (or udp headers on udp segment.)
just number. nothing more, nothing less.
if using "socket"-based interface network programming, significance of number each of tcp or udp sockets has "local port" property, , "remote port" property. said before, setting properties called "binding".
if socket's "local port" property "bound" 80, tcp segments send out have "80" in "sender port" header. then, when others respond messages, put "80" in "destination port" headers.
more that, if socket "bound" local port 80, when data arrives elsewhere, addressed port 80, os pass your application process , not other. then, when try read socket, data returned.
obviously, os needs know port each of sockets bound to. when "binding", system calls must made. if program not running sufficient privileges, os may refuse let bind port. then, depending on language using, networking library throw exception, or return error code.
sometimes os may refuse let bind port, not because don't have right privileges, because another process has bound it. however, , of other answers wrong, if flags set when opening socket, os may allow more one socket bound same local address , port.
you still don't know "listening" , "connected" sockets are. once understand above, small jump.
the above explains difference between today call "socket" , call "port". may still not clear is: why need make distinction?
you have got me thinking here (thank you)! call software abstraction called "socket" "port" instead, instead of calling socket_recv
call port_recv
?
if interested in tcp , udp, maybe work. remember, "socket" abstraction not tcp , udp. other network protocols, inter-process communication on same machine.
then again, tcp socket not map port. "connected" tcp socket maps local ip address, local port, remote address, and remote port. also has other associated data, including various flags, send , receive buffers, sequence numbers incoming/outgoing data streams, , various other variables used congestion control (rate limiting), etc. data not belong local port.
there can thousands of tcp connections going simultaneously through same "port". each of connections has own associated data, , software object encapsulates per-connection data "tcp socket".
even if use tcp/udp, , if have single process using given local port @ 1 time, , if have single connection going through each local port @ 1 time, think "socket" abstraction still makes sense. if called sockets "ports", there more meanings conflated in 1 word. reusing same word many meanings hinders communication.
"ports" transport-protocol level identifiers application process. "sockets" objects used in software send/receive messages addressed from/to identifiers.
differentiating between "my address" , "the thing sends letters addressed coming me" useful distinction make. "my address" label. label not active, things sending data. logical give "the thing used send data" own name, different name denotes "the sender address data labelled with".
Comments
Post a Comment