Introduction To Sockets
Introduction To Sockets
• Socket
• Why Sockets? –
• Used for Interprocess communication.
• • The Client-Server model – Most interprocess
communication uses client-server model
• – Client & Server are two processes that wants
to communicate with each other
• – The Client process connects to the Server
process, to make a request for
information/services own by the Server.
• – Once the connection is established between
Client process and Server process, they can start
sending / receiving information.
• What are Sockets? –
• End-point of interprocess communication.
• – An interface through which processes can
send / receive information
• What exactly creates a Socket?
• – tuple – {Source , Destination }
• What makes a connection?
• i.e. source socket – destination socket pair
uniquely identifies a connection.
• Socket Types –
• STREAM – uses TCP which is reliable, stream
oriented protocol,
• DATAGRAM – uses UDP which is unreliable,
message oriented protocol
• RAW – provides RAW data transfer directly over
IP protocol (no transport layer)
• Sockets can use –
• “unicast” ( for a particular IP address destination) –
• “multicast” ( a set of destinations – 224.x.x.x)
• “broadcast” (direct and limited)
• Transmission Control Protocol
• A connection-based protocol that provides a reliable flow
of data between two computers.
• Provides a point-to-point channel for applications that
require reliable communications.
• – The Hypertext Transfer Protocol (HTTP),
File Transfer Protocol (FTP),
• and Telnet are all examples of applications that require a
reliable communication channel
• Guarantees that data sent from one end of the
connection actually gets to the other end and in the same
order it was sent.
• Otherwise, an error is reported.
User Datagram Protocol
• A protocol that sends independent packets of data,
called datagrams, from one computer to another with
no guarantees about arrival.
• UDP is not connection based like TCP and is not
reliable:
• – Sender does not wait for acknowledgements
• Arrival order is not guaranteed – Arrival is not
guaranteed
• Used when speed is essential, even in cost of
reliability –
• e.g. streaming media, games, Internet telephony, etc
Client-Side Programming
• To connect to another machine we need a socket connection.
• A socket connection means the two machines have information about each other’s
network location (IP Address) and TCP port.
• The java.net.Socket class represents a Socket.
• To open a socket:
• Socket socket = newSocket(“127.0.0.1”, 5000)
•
The TCP/IP Client Socket or Socket Class
int getPort() -- Returns the port number on which the socket is connected
int getLocalPort() -- Returns the local port number on which the socket is
created