Java Networking
TCP Socket programming
Content
1. Introduction
IP address
DNS
Transport protocols
sockets
2. Client/Server Computing
Introduction
Computer networking is used to send and receive messages
among computers on the Internet.
To browse the Web or send an email, your computer must be
connected to the Internet.
The Internet is the global network of millions of computers. Your
computer can connect to the Internet through an Internet Service
Provider (ISP) using a dialup, DSL, or cable modem.
IP address
When a computer needs to communicate with another
computer, it needs to know the other computer’s address.
An Internet Protocol (IP) address uniquely identifies the
computer on the Internet. An IP address consists of four
dotted decimal numbers between 0 and 255, such as
[Link]
A program can use the host name localhost or the IP
address [Link] to refer to the machine on which a client
is running.
DNS
Since it is not easy to remember so many numbers, they are
often mapped to meaningful names called domain names,
such as [Link], [Link] , etc
Special servers called Domain Name Servers (DNS) on the
Internet translate host names into IP addresses
When a computer contacts
[Link], it first asks the
DNS to translate this domain
name into a numeric IP address
and then sends the request using
the IP address.
Transport protocols
The Internet Protocol is a low-level protocol for
delivering data from one computer to another across
the Internet in packets.
Two higher-level protocols used in conjunction with
the IP are the Transmission Control Protocol (TCP) and
the User Datagram Protocol (UDP).
Our
focu
s TCP can detect lost transmissions and resubmit them,
transmissions are lossless, in order and reliable.
UDP, in contrast, cannot guarantee lossless
transmission.
Sockets
A network socket is a software structure within
a network node that serves as an endpoint for
sending and receiving data across the network
(on server and client)
It is identified by an IP (to identify the host) and a
port number (to identify the process)
Port numbers range from 0 to 65536 (2 16 ) but
port numbers 0 to 1024 are reserved for
privileged services.
E.g. web port 80
email port 25
Client/Server communication
Network programming usually involves one server
and one or more clients.
The client sends requests to the server, and the
server responds.
The client begins by attempting to establish a
connection to the server.
The server can accept or deny the connection.
Once a connection is established, the client and the
server communicate through sockets.
The server must be running when a client attempts
to connect to the server.
The server waits for a connection request from the
client.
Client/Server Computing
Java API provides the classes for creating sockets to
facilitate program communications over the Internet.
Java provides the ServerSocket class for creating a server
socket and the Socket class for creating a client socket.
Programs can read from or write to sockets as easily as they
can read from or write to files.
Client/Server Computing
Create a server socket:
ServerSocket serverSocket = new ServerSocket(port);
The server can now listen for connections:
Socket socket = [Link]();
This means the server waits until a client connect to the server
socket.
Then, client issues the following statement to request a connection
to a server using its name or IP:
Socket socket = new Socket("[Link]", 8000);
Socket socket = new Socket("[Link]", 8000);
When you create a socket with a host name, the JVM asks the DNS to
translate the host name into the IP address.
Data Transmission through Sockets
After the server accepts the connection,
communication between the server and the
client is conducted in the same way as for I/O
streams.
To get an input stream and an output stream,
use the getInputStream() and
getOutputStream() methods on a socket
object.
InputStream input =
[Link]();
OutputStream output =
[Link]();
The server can use [Link]() to
receive a double value from the client and
[Link](d) to send the double
value d to the client.
A Client/Server Example
This example presents a client program and a
server program.
The client sends data to a server.
The server receives the data, uses it to
produce a result, and then sends the result
back to the client.
In this example, the data sent from the client
comprise the radius of a circle, and the result
produced by the server is the area of the
circle.
Server program
Client program
A Client/Server Example
Client sends the radius through a
DataOutputStream on the output stream
socket.
Server receives the radius through the
DataInputStream on the input stream socket
The server computes the area and sends it to
the client through a DataOutputStream on the
output stream socket, and the client receives
the area through a DataInputStream on the
input stream socket
A Client/Server Example
With GUI - Server
With GU-Client