1. TCP Sockets
A socket is a connection between two hosts. It can
perform seven basic operations:
• Connect to a remote machine
• Send data
• Receive data
• Close a connection
• Bind to a port
• Listen for incoming data
• Accept connections from remote machines on the
bound port
2. Client Sockets
• The program creates a new socket with a Socket( )
constructor.
• The socket attempts to connect to the remote host.
• Once the connection is established, the local and remote
hosts get input and output streams from the socket and use
those streams to send data to each other.
• This connection is full-duplex; both hosts can send and
receive data simultaneously.
• What the data means depends on the protocol; different
commands are sent to an FTP server than to an HTTP server.
There will normally be some agreed-upon hand-shaking
followed by the transmission of data from one to the other.
• When the transmission of data is complete, one or both sides
close the connection. Some protocols, such as HTTP 1.0,
require the connection to be closed after each request is
serviced. Others, such as FTP, allow multiple requests to be
processed in a single connection.
7. Continued..
public Socket(String host, int port) throws
UnknownHostException, IOException
• public Socket(String host, int port, InetAddress
interface, int localPort) throws IOException
• public Socket(InetAddress host, int port,
InetAddress interface, int localPort) throws
IOException
if 0 is passed for the local port number, java
randomly selects port number between 1024 and
65,535.
8. Getting Information About a
Socket
• public InetAddress getInetAddress( )
• public int getPort( )
• public int getLocalPort( )
• public InetAddress getLocalAddress( )
public String toString( ) (Object Method)
9. Sockets for Servers
• Java provides a ServerSocket class to allow
programmers to write servers.
• A ServerSocket runs on the server and listens
for incoming TCP connections.
• Each ServerSocket listens on a particular port
on the server machine.
• When a client Socket on a remote host attempts
to connect to that port, the server wakes up,
negotiates the connection between the client
and the server, and opens a regular Socket
between the two hosts.
10. • The operating system stores incoming
connection requests addressed to a
particular port in a first-in, first-out queue.
• The default length of the queue is normally
50, though this can vary from operating
system to operating system.
11. The basic life cycle of a server
1. A new ServerSocket is created on a particular port using a
ServerSocket( ) constructor.
2. The ServerSocket listens for incoming connection attempts on
that port using its accept( ) method.
3. accept( ) blocks until a client attempts to make a connection, at
which point accept( ) returns a Socket object connecting the client
and the server.
4. Socket's getInputStream( )method, getOutputStream( ) method,
are called to get input and output streams that communicate with
the client.
5. The server and the client interact according to an agreed-up on
protocol until it is time to close the connection.
6. The server, the client, or both close the connection.
7. The server returns to step 2 and waits for the next connection
12. Constructors
• public ServerSocket(int port) throws IOException,
BindException
• public ServerSocket(int port, int queueLength)throws
IOException, BindException
• public ServerSocket(int port, int
queueLength,InetAddress bindAddress) throws
IOException
• An IOException when creating a ServerSocket almost
always means one of two things.
– Either another server socket is already using the requested port,
– or you're trying to connect to a port from 1 to 1023 on Unix
without root (superuser) privileges.