Chapter 6 Network Programming
Chapter 6 Network Programming
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 1
rights reserved.
Objectives
• To explain terms: stream-based communications, and packet-based
communications
• To create servers using server sockets and clients using client sockets
• To implement Java networking programs using stream sockets
• To develop an example of a client/server application.
• To obtain Internet addresses using the InetAddress class.
• To develop servers for multiple clients.
• To send and receive objects on a network.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 2
rights reserved.
Client/Server Communications
The server must be running when a client starts. After the server accepts the
The server waits for a connection request from a connection, communication
client. To establish a server, you need to create a between server and client is
server socket and attach it to a port, which is conducted the same as for
where the server listens for connections. I/O streams.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 3
rights reserved.
Client/Server Network Communication via Sockets
1. The Java server program listens and waits for a connection on port 6789. Different programs
may be listening on other ports.
Server Computer
port 0
port 1
Java …
server
port 6789
program
…
2. The Java client program connects to the server on port 6789. It uses a local port that is assigned
automatically, in this case, port 8312.
3. The Java server program can now communicate over a socket bound locally to port 6789 and remotely
to the client’s address at port 8312, while the client communicates over a socket bound locally to port
8312 and remotely to the server’s address at port 6789.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
Communication via Sockets
• Server program
• Listen for a connection on a specified port; when one is made:
• Create a Scanner with an InputStreamReader based on the socket that the server
will listen on; use this for input from a client
• Create a PrintWriter with the socket to send data to the client
• Client program
• Initiate a connection to the server on a specified port
• Create a Scanner to read from the socket
• Create a PrintWriter to send to the socket
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
Data Transmission through Sockets
Server Client
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 6
rights reserved.
A Client/Server Example
• Problem: Write a client to send data to a server. The
server receives the data, uses it to produce a result,
and then sends the result back to the client. The client
displays the result on the console.
• In this example, the data sent from the client is the
radius of a circle, and the result produced by
the server is the area of the circle.
compute area
radius
Server Client
area
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 7
rights reserved.
A Client/Server Example, cont.
Network Network
(A) (B)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 8
rights reserved.
A Client/Server Example, cont.
compute area
radius
Server Client
area
Next, you can display the client's host name and IP address, as
follows:
while (true) {
Socket socket = serverSocket.accept();
Thread thread = new ThreadClass(socket);
thread.start();
}
The server socket can have many connections. Each iteration of the while loop
creates a new connection. Whenever a connection is established, a new thread is
created to handle communication between the server and the new client; and this
allows multiple connections to run at the same time.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 11
rights reserved.
Example: Serving Multiple Clients
Server
A serve socket
Start Server
Client 1 ... Client n
Start Client
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 12
rights reserved.
Example: Passing Objects in Network Programs
Write a program that Server Client
collects student student object student object
information from a
client and send them to in.readObject() out.writeObject(student)
a server. Passing
student information in in: ObjectInputStream out: ObjectOutputStream
an object.
socket.getInputStream socket.getOutputStream
socket socket
Student Class
Network
Student Sever
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 13
rights reserved.
Stream Socket vs. Datagram Socket
Stream • A dedicated point-to-point channel between a client and
socket server.
• Use TCP (Transmission Control Protocol) for data
transmission.
• Lossless and reliable.
• Sent and received in the same order.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 14
rights reserved.
DatagramPacket
The DatagramPacket class represents a datagram packet. Datagram
packets are used to implement a connectionless packet delivery
service. Each message is routed from one machine to another based
solely on information contained within the packet.
java.net.DatagramPacket
length: int A JavaBeans property to specify the length of buffer.
address: InetAddress A JavaBeans property to specify the address of the machine where the
package is sent or received.
port: int A JavaBeans property to specify the port of the machine where the
package is sent or received.
+DatagramPacket(buf: byte[], Constructs a datagram packet in a byte array buf of the specified length
length: int, host: InetAddress, port: with the host and the port for which the packet is sent. This constructor
int) is often used to construct a packet for delivery from a client.
+DatagramPacket(buf: byte[], Constructs a datagram packet in a byte array buf of the specified length.
length: int)
+getData(): byte[] Returns the data from the package.
+setData(buf: byte[]): void Sets the data in the package.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 15
rights reserved.
DatagramSocket
DatagramSocket The DatagramSocket class represents a socket for sending and
receiving datagram packets. A datagram socket is the sending or
receiving point for a packet delivery service. Each packet sent or
received on a datagram socket is individually addressed and routed.
Multiple packets sent from one machine to another may be routed
differently, and may arrive in any order.
Create a client
To create a client DatagramSocket, use the constructor
DatagramSocket DatagramSocket(), which binds the socket with any available port
on the local host machine.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 16
rights reserved.
Sending and Receiving a DatagramSocket
Sending To send data, you need to create a packet, fill in the
contents, specify the Internet address and port number for
the receiver, and invoke the send(packet) method on a
DatagramSocket.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 17
rights reserved.
Datagram Programming
Datagram programming is different from stream socket programming
in the sense that there is no concept of a ServerSocket for datagrams.
Both client and server use DatagramSocket to send and receive
packets.
Designate DatagramServer DatagramClient
one a server DatagramSocket socket; DatagramSocket socket;
socket = new DatagramSocket(8000); socket = new DatagramSocket();
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All 18
rights reserved.