Advanced Java Programming Chapter 5 - Network Programming
Advanced Java Programming Chapter 5 - Network Programming
Programming
1
Similar to the phone call. We need the phone number and receiver.
A reliable transport_
After it sends the data, it waits for an acknowledgment from the receiver to make
sure that the data reached the destination. _______
Connectionless
No link needed to be set up before communication.
The sender will not wait to make sure the recipient received the packet
rather continue sending the next packets.
Overview: TCP (Transmission 4
Control Protocol)
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.
Overview: UDP (User 5
Datagram Protocol)
Whatever the service provided by a server, there must be some established protocol
governing the communication that takes place between server and client.
On the client-side: The client knows the hostname of the machine on which
the server is running and the port number on which the server is listening.
Programming
Thursday, March 13, 2025
Overview: Socket
Overview: Socket 12
1. The client sends requests to the server, and the server responds.
When you write Java programs that communicate over the network, you are
programming at the application layer.
Typically, we don't need to concern with the TCP and UDP layers.
ServerSocket:
2. Each client connection handled with a Socket object. Server blocks until
client connects.
Programming
Thursday, March 13, 2025
Creating TCP Server/Client
Socket
Accepting Connection 23
The accept method returns a new socket (with a new port) for
the new channel. It blocks until connection is made
Whenever accept() returns, a new thread is launched to handle
that interaction
Hence, the server can handle several requests concurrently
Serving Multiple Client 24
Programming
Thursday, March 13, 2025
Example
URL Class 27
Programming
Thursday, March 13, 2025
Parsing URL
Reading from a 29
URLConnection
the URL and the application. It can be used to read and write data to the
After you've successfully created a URL, you can call the URL's
openConnection() method to get a stream from which you can read the
Programming
Thursday, March 13, 2025
Reading Directly from a URL
Advanced Programming---Chapter 5---Network
31
Programming
Thursday, March 13, 2025
DatagramPacket, DatagramSocket, and MulticastSocket
UDP based Communication
UDP Communication 32
network whose arrival, arrival time, and content are not guaranteed.
DatagramPacket.
UDP Communication 33
1. Create an array of bytes large enough to hold the data of the packet to
be sent, and fill the array with the data.
Programming
Thursday, March 13, 2025
UDP Sender
Advanced Programming---Chapter 5---Network
38
Programming
Thursday, March 13, 2025
UDP Receiver
Advanced Programming---Chapter 5---Network
39
Programming
Thursday, March 13, 2025
End of Chapter 5
--- End ---