0% found this document useful (0 votes)
11 views

Ch 4

Uploaded by

Tade Mf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ch 4

Uploaded by

Tade Mf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter four

Networking in Java
 Sockets,ports, URIs
 Connecting to a server

 Implementing Servers in Java


 Networking in Java refers to the process of connecting different computers
or devices across a network to exchange data
 Networking in Java is an essential concept that allows different programs,
running on different machines or the same machine, to communicate with
each other over a network. Java provides a comprehensive API for
building networked applications via the java.net package, which includes
classes for both client-side and server-side communication.
 Java provides a comprehensive set of classes and interfaces in the java.net
package to support networking capabilities, allowing developers to create
client-server applications, transfer data over the internet, and handle
communication between computers.
 Sockets are used to enable communication between two networked
devices, and they combine an IP address and port number to uniquely
identify a service on a device.
 Ports are numerical identifiers that distinguish different services or
processes running on a device. They help direct data to the appropriate
application.
 URIs provide a way to uniquely identify resources, commonly used in
web protocols (URLs). A URI can be a URL (with a location and protocol)
or a URN (identifying a resource by name).
 A socket is one endpoint of a two-way communication link between two
programs running on the network. It is an abstraction provided by the
operating system to manage network communication. A socket is a
combination of an IP address and a port number that uniquely identifies a
specific application or service running on a device.
 Type of socket

1. Stream Sockets (TCP): Provide reliable, connection-oriented communication


(using the Transmission Control Protocol, TCP). Data is delivered in order
and without errors.

2. Datagram Sockets (UDP): Provide connectionless communication (using


User Datagram Protocol, UDP), w/h is faster but does not guarantee data
order or delivery.
 TCP connection is connection oriented. Whereas UDP
connection is connection less (UDP does not establish a
connection before sending data).
 In TCP connection there is a handshaking b/n sender and
receiver, Whereas there is not handshaking(the receiver can
not send acknowledgement implies There is no feedback to
the sender about whether the packet was successfully received
or not.) so TCP is reliable.
 TCP is reliable communication(Uses acknowledgments and
retransmissions to ensure that data is received correctly.) but
UDP is not.
 A port is a 16-bit number that identifies a specific process or service on a
device. It is used in conjunction with an IP address to form a unique
identifier for a network service. Ports are used to direct data to the
appropriate application running on the server or device.
 Port number
• Well-known Ports (0-1023): Reserved for specific services (e.g., HTTP on
port 80, HTTPS on port 443, FTP on port 21).
• Registered Ports (1024-49151): Used by user or third-party applications
(e.g., PostgreSQL uses port 5432)
• Dynamic/Private Ports (49152-65535): Assigned dynamically by the
operating system for ephemeral (short-lived) connections
 Common Ports:
• HTTP – 80

• HTTPS – 443

• FTP – 21

• SMTP – 25

• DNS – 53
 Example in Java:

 When creating a socket, you specify the port number along with the IP

address (or hostname).


Socket socket = new Socket("localhost", 8080); // Connects to port 8080
 A URI is a string that provides a way to uniquely identify a resource, typically on
the web. It can either be a Uniform Resource Locator (URL) or Uniform
Resource Name (URN). The most commonly used form of URI is the URL,
which specifies both the resource's location and the protocol used to access it.
 URL: A URL is a specific type of URI that not only identifies a resource but also
describes its location on the internet. It typically includes the protocol (HTTP,
FTP, etc.), the hostname or IP address, the port number (optional), and the path
to the resource.
 Structure of a URL:

protocol://hostname:port/path?query#fragment
 Protocol: Specifies the communication protocol ((e.g., http, ftp)
 hostname: The domain name or IP address of the server (e.g.,
www.example.com)
 port: (Optional) The port number on the server (default ports are used if
omitted, like port 80 for HTTP).
 path: The specific location of the resource on the server (e.g., /page.html)
 query: (Optional) A query string, usually used in HTTP for passing
parameters (e.g., ? Id=123)
 fragment: (Optional) A reference to a specific section within the resource
(e.g., #section1)
 Example

https://www.example.com:443/path/to/resource?query=value#section
 Java example(using java.net.URL)

URI uri=new URI("https://www.example.com:443/path/to/resource?


query=value#section");
 System.out.println(uri.getHost()); // Output: www.example.com
 System.out.println(uri.getPort()); // Output: 443
 Connecting to a server involves establishing a communication link
between a client and a server over a network. In Java, this is commonly
achieved using sockets, which enable both client and server to send and
receive data.
 The two primary communication protocols used for transmitting data over
the internet or within networks are TCP and UDP.
 Both are part of the Transport Layer (Layer 4) in the OSI model and
provide mechanisms for sending data between computers, but they differ
significantly in how they handle that data.
 The server listens for incoming connections on a specific port, using a
serversocker. Once a client connects, the server can read and write data
using the client’s socket.
 A socket can refer to a client socket (on the client machine) or a server
socket (on the server machine).
 A server socket is a special type of socket that is used by a server to listen
for incoming client connections.
 The server socket is responsible for waiting for clients to connect to it,
accepting those connections, and then passing the communication to a
regular socket for further data exchange.
 To establish simple server follow the step
1. Open the server socket
 Server block until client connects.

ServerSocket server = new ServerSocket(port);


2. Wait for the client request
Socket client = server.accept();
3. Create I/O streams for communication to client
DataInputStream is = new DataInputStream(client.getInputStream());
DataOutputStream os = new DataOutputStream(client.getOutputStream());
4. Perform communication with client receive from client
String line = is.readUTF(); //read from keyboard
Send to client: os.writeBytes(“Hello \n”);
5. Close the socket
client.close();
 for main function
try{
String message = "";
ServerSocket server = new ServerSocket(1002); //server strat at 1002 port number
Socket conn = server.accept(); //now server will accepts the connection
// create data input/output that read and write from connection socket,
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(conn.getInputStream());
while(!message.equals("exit")){
message = in.readUTF();
msg_area.setText(msg_area.getText() + "\n Client says: "+ message); //displays the message
}
}
catch(Exception e){
}
For send button
try{
String mesg = "";
mesg = msg_text.getText();
out.writeUTF(mesg);
}
catch(Exception e){

}
 The client creates a socket to connect to the server at a specific IP address
and port. The step that used to create simple client using TCP stream are
1. Create socket object – obtain socket input and output stream
Socket client = new Socket(server, port_id)
2. Create I/O stream for communication between server
is = new DataInputStream(client.getInputStream());
os = new DataOutputStream(client.getOutputStream());
3. Perform I/O stream for communication between server
 Receive data from server

String line = is.readUTF()


 Send data from server

os.writeBytes(“Hello \n”);
4. Close the socket
client.close();
 for main function
try{
String meg = "";
Socket soc = new Socket("localhost", 1002);
DataInputStream input = new DataInputStream(soc.getInputStream());
DataOutputStream output = new DataOutputStream(soc.getOutputStream());
while(!meg.equals("exit")){
meg = input.readUTF();
text_area.setText(text_input.getText() + "\n Server says: " + meg);

}
}
catch (Exception e){
}
For send button
try{
String msg = "";
msg = text_input.getText();
output.writeUTF(msg);

}
catch(Exception e){

}
 Establishing a simple server using UPD

You might also like