Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Advanced Java Programming Chapter 5 - Network Programming

Chapter 5 of the Advanced Programming document focuses on network programming in Java, detailing the concepts of TCP and UDP protocols for communication between devices. It explains the client-server architecture, the use of sockets for data transmission, and how Java's java.net package facilitates network communication. The chapter also covers the creation of server and client sockets, handling multiple clients, and working with URLs and IP addressing in Java.

Uploaded by

btbonsa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Advanced Java Programming Chapter 5 - Network Programming

Chapter 5 of the Advanced Programming document focuses on network programming in Java, detailing the concepts of TCP and UDP protocols for communication between devices. It explains the client-server architecture, the use of sockets for data transmission, and how Java's java.net package facilitates network communication. The chapter also covers the creation of server and client sockets, handling multiple clients, and working with URLs and IP addressing in Java.

Uploaded by

btbonsa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Advanced Programming---Chapter 5---Network

Programming
1

Thursday, March 13, 2025


JAVA.NET PACKAGE
Chapter 5: Network
Programming
Overview: Networking 2
 Networking is the concept of connecting multiple remote

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
or local devices together.
 Computers running on the Internet communicate to each
other using
1. Transmission Control Protocol (TCP)
2. User Datagram Protocol (UDP)
 Most Internet applications use TCP as their transport
mechanism
Overview: Types of 3
 Communication
Connection-oriented

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Setup the link before communication.

 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.

 Similar to sending a letter. We need the address and receiver.

 It is unreliable, but much faster.

 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)

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 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.
Overview: UDP (User 5
Datagram Protocol)

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 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 at the cost of reliability


 e.g. streaming media, games, Internet telephony, etc.
Overview: Protocol 6

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Protocol: set of rules and guidelines which provides the instructions
to send request and receive response over the network.
 E.g., HTTP, FTP, TCP, UDP, SMTP, …

 Whatever the service provided by a server, there must be some established protocol
governing the communication that takes place between server and client.

 Internet Protocol(IP) Address: is an identification number that


uniquely identifies the computer on the Internet.
 An IP address consists of four dotted decimal numbers between 0 and 255 e.g.,
192.168.2.01

 Each computer on the Internet has a unique IP address


Overview: Port 7

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 A computer has a single physical connection to the network.

 All data destined for a particular computer arrives through that


connection.
 However, the data may be intended for different applications running on the
computer.

 So how does the computer know to which application to forward the


data?
 Through the use of ports.
Overview: Port 8

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Port Number: way to identify a specific process to which an Internet
or other network message is to be forwarded when it arrives at a
server.

 The port number is unique for different applications.

 It is a 32-bit positive integer number having between


ranges 0 to 65535.

 The port numbers ranging from 0 - 1023 are restricted; they


are reserved for use by well-known services such as HTTP and FTP
and other system services. These ports are called well-known ports.

 Data transmitted over the Internet is accompanied by


addressing information that identifies the computer and the
Overview: Client-Server 9
architecture

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Network-based systems consist of a server , client , and a
media for communication. This communication is instructed
through network programs and technique is called
network programming
 Server: a computer listens for requests from client
components.
 When a request is received, the server processes the
request, and then sends a response back to the client.
 Communication takes place through Socket
Overview: Socket 10

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 An interface between application and network.

 Socket: a listener through which computer can receive requests and


responses.

 A server runs on a specific computer and has a socket that is


bound to a specific port number.
 The server just waits, listening to the socket for a client to make a connection
request.

 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.

 A program can read from a socket or write to a socket as simply as reading


Advanced Programming---Chapter 5---Network
11

Programming
Thursday, March 13, 2025
Overview: Socket
Overview: Socket 12

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Network socket connections are made to look like I/O
streams.
 We simply read and write data using the usual stream
methods (all socket communication is in 8-bit bytes), and it
automatically appears at the other end.
 Unlike a stream, a socket supports two-way communication.

 There is a method to get the input stream of a socket, and


another method to get the output stream.
 This allows the client and server to talk back and forth
13

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
Network Programming in Java
Network programming: writing programs that execute across
multiple computers, in which the devices are all connected to each
other using a network.
Networking in Java 14

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Network programming usually involves a server and one or more
clients.

1. The client sends requests to the server, and the server responds.

2. The client begins by attempting to establish a connection to the server.

3. The server can accept or deny the connection.

4. Once a connection is established, the client and the server communicate


through sockets.

5. The server must be running when a client attempts to connect to the


server.

6. The server waits for a connection request from a client.

7. The statements needed to create sockets on a server and a client


Networking in Java 15

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 The Java API provides the classes for creating sockets to facilitate program
communications over the Internet.

 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.

 We use the classes in the java.net package.


 Classes under the package provide system-independent network communication.

 Primary communications is through sockets.

 (Web type access also available through URL class)

 “Default” socket is stream (TCP).


Networking in Java 16

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Through the classes in java.net, Java programs can use TCP or
UDP to communicate over the Internet.
 Stream-based communications: use TCP for data
transmission
Socket, ServerSocket, URL, URLConnection classes are for use
with TCP to communicate over the network.

 Packet-based communications : use UDP to communicate


over the network.
The DatagramPacket, DatagramSocket, and MulticastSocket
classes are for use with UDP.
17

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
TCP based client server
communication
Socket, ServerSocket, URL, URLConnection
TCP Communication 18

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Client and Server computers establish their connection by means of
a special object called a Socket.
 A socket can be used to connect Java’s I/O system to other program that
may reside either on the local machine or on any other machine on the
Internet.
 There are two kinds of TCP sockets in Java. One is for server and
other is for clients.
 The ServerSocket class is designed to be a “listener”, which helps
servers establish socket connections with clients.
 The Socket class is designed to connect to server sockets and initiate
protocol exchanges.
TCP Communication 19

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 The Java API provides TCP streams by means of two
classes:
 ServerSocket - This class implements server sockets. A server
socket waits for requests to come in over the network.

 Socket - This class implements client sockets.

 ServerSocket:

 accept - Listens for a connection to be made to this socket and


accepts it. The result of executing accept is an instance of Socket.
Creating TCP Server Socket 20

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
Five steps to create a simple stream server in Java:

1. ServerSocket object. Registers an available port and a maximum number


of clients.

2. Each client connection handled with a Socket object. Server blocks until
client connects.

3. Sending and receiving data

• OutputStream to send and InputStream to receive data.

• Methods getInputStream and getOutputStream on Socket object.

4. Process phase. Server and client communicate via streams.

5. Close streams and connections.


Creating TCP Client Socket 21

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
Four steps to create a simple stream client in Java:

1. Create a Socket object for the client.

2. Obtains Socket’s InputStream and/or OutputStream.

3. Process information communicated.

4. Close streams and Socket.


Advanced Programming---Chapter 5---Network
22

Programming
Thursday, March 13, 2025
Creating TCP Server/Client
Socket
Accepting Connection 23

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Usually, the accept() method is executed within an infinite loop
 i.e., while(true){...}

 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

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Multiple clients are quite often connected to a single server
at the same time.
 Typically, a server runs continuously on a server computer,
and clients from all over the Internet can connect to it.
 You can use threads to handle the server’s multiple clients
simultaneously.
 Simply create a thread for each connection.
 Here is how the server handles the establishment of a
connection:
IP addressing Class 25

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 For IP addressing, three classes are provided :
 Inetaddress – for both IPv4 and IPv6

 Inet4address – for IPv4

 Inet6address – for Ipv6

 Inetaddress class doesn’t have a constructor.


 The normal way to create it is to call one of its static methods,
such as getByName, isReachable, getAllByName which
throws exception
 Any program using this class must import either
java.net.InetAddress or java.net.*
Advanced Programming---Chapter 5---Network
26

Programming
Thursday, March 13, 2025
Example
URL Class 27

 In Java, you handle URLs with the URL class.

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 URL uniquely identifies or addresses information on the Internet.
 Java’s URL class has several constructors (each can throw a
MalformedURLException) and methods.
 The most commonly used constructors are:
 URL(String spec)
 URL(String protocol, String host, int port, String file)
 URL(String protocol, String host, String file)
 Purpose:
 Get an input stream from a URL so you can read data from a server
 Get content from the server as a Java object
 The following methods of URL can be used for parsing URLs:
getProtocol(), getHost(), getPort(), getPath(), getFile(), getQuery(),
getRef()
Advanced Programming---Chapter 5---Network
28

Programming
Thursday, March 13, 2025
Parsing URL
Reading from a 29

URLConnection

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 The Java URLConnection class represents a communication link between

the URL and the application. It can be used to read and write data to the

specified resource referred by the URL.

 The openConnection() method opens a socket to the specified URL and

returns a URLConnection object.

 A URLConnection represents an open connection to a network resource.

 If the call fails, openConnection() throws an IOException.

 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

contents of the URL.


Advanced Programming---Chapter 5---Network
30

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

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 A datagram is an independent, self-contained message sent over the

network whose arrival, arrival time, and content are not guaranteed.

 Java implements Datagram on top of the UDP protocol by using two

classes, namely the DatagramPacket and DatagramSocket

 The DatagramPacket object is the data container

 The DatagramSocket is the mechanism used to send or receive the

DatagramPacket.
UDP Communication 33

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
 Datagram packets are used to implement a connectionless
packet delivery service supported by the UDP protocol.
 Each message is transferred from source machine to
destination based on information contained within that packet.
 That means, each packet needs to have destination address and each packet
might be routed differently, and might arrive in any order.

 Packet delivery is not guaranteed packet


UDP Communication 34

 The java.net.DatagramPacket: used to send datagram

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
packets
 The java.net.DatagramSocket: used to receive datagram
packets
 Sender does not wait for acknowledgements

 Arrival order is not guaranteed, Arrival is not


guaranteed.
 The receiving device also never know if the datagram

received is damaged or not.


 So why use UDP if it unreliable?
Send Datagram Packet 35

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
STEPS TO SEND DATAGRAM PACKET

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.

2. Create a new DatagramPacket object that contains the array of bytes,


as well as the server name and port number of the recipient.

3. A DatagramSocket is instantiated, and it is specified which port (and


specific localhost address, if necessary)

4. The send() method of the DatagramSocket class is invoked, passing in


the DatagramPacket object.
Receive Datagram Packet 36

Thursday, March 13, 2025


Programming
Advanced Programming---Chapter 5---Network
STEPS TO RECEIVE DATAGRAM PACKET
1. Create an array of bytes large enough to hold the data of the incoming
packet.

2. A DatagramPacket object is instantiated using the array of bytes and its


length.

3. A DatagramSocket is instantiated, and it is specified which port (and


specific localhost address, if necessary) on the localhost the socket will
bind to.

4. The receive() method of the DatagramSocket class is invoked, passing in


the DatagramPacket object. This causes the thread to block until a
datagram packet is received or a time out occurs.
Advanced Programming---Chapter 5---Network
37

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 ---

You might also like