[Go to site: main page, start]

0% found this document useful (0 votes)
34 views8 pages

Java Networking Fundamentals

1) The document discusses computer networks and client-server architecture. It describes how a computer network allows sharing of resources like printers between connected computers. In client-server architecture, a central server machine shares its resources with multiple client machines. 2) It then explains concepts in networking like sockets, ports, IP addresses etc. Socket classes in Java are used to establish connections between client and server programs. Port numbers identify applications and allow data transfer between nodes. 3) The document provides examples of Java socket programming to demonstrate communication between client and server applications running on different systems. The ServerSocket class is used to create server sockets and accept client connections, while the Socket class represents the client side.

Uploaded by

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

Java Networking Fundamentals

1) The document discusses computer networks and client-server architecture. It describes how a computer network allows sharing of resources like printers between connected computers. In client-server architecture, a central server machine shares its resources with multiple client machines. 2) It then explains concepts in networking like sockets, ports, IP addresses etc. Socket classes in Java are used to establish connections between client and server programs. Port numbers identify applications and allow data transfer between nodes. 3) The document provides examples of Java socket programming to demonstrate communication between client and server applications running on different systems. The ServerSocket class is used to create server sockets and accept client connections, while the Socket class represents the client side.

Uploaded by

srgcpawan01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit – 3

Java Networking

Computer Network
Let us consider that you started a commercial activity where you were using a
computer along with a printer, modem and a CD-ROM. Very soon you need to
expand your business and you felt the requirement to add more people as well more
computers. Suppose you buy 5 computers instead of going for a decision to buy 5
more printers, you should opt for an environment having the capabilities to share
the available printer by the new 5 computers, this will save the money and resources
to buy and manage the 5 new printers. Such an environment providing the sharing
and exchanging facilities among the computer machines is called as a computer
network.

Now you just need to maintain a network in which all the computers are
interconnected and share the same printer and all such devices i.e. we just link the
computer devices together with hardware and software supporting data
communication across the network.

Client-Server Architecture
Client-server architecture can be considered as a network environment that
exchanges information between a server machine and a client machine where server
has some resources that can be shared by different clients.

In a Client/server architecture individual computers (known as clients) are connected


to a central computer which is known as ?server?.
Let?s take an example of a file server to understand the core process of a
client/server network, the file server acts as a storage space on the network for the
files, spreadsheets, databases, etc. Instead of storing these records on every
individual computer, the file server allows the clients to store their files on one
central computer and make them sharable. The client-server architecture is beneficial
in reducing the multiple iterations of a single file and allowing the organization to
have one centralized point for every computer to access the same file.

The interaction between a lamp and an electrical socket can be considered as a


interaction between client and server is just like. In the example the electrical socket
is just like a server and the lamp works like a client.

Socket and ports


In common language we can say that the socket is one of the most primal
technologies of computer networking. Sockets are just like an end-point of two-way
communication, which allow applications to communicate using network hardware
and operating systems. However in case of java never get confused with a socket.
Socket classes are used to establish a connection between client program and a
server program. In java there is a [Link] package, which provides two types of
classes- first is ordinary socket, which implement the client side connection and
second is server socket, which implement the server side connection.

The main purpose of the server socket is to listen an incoming connection request
and ordinary socket is used to ask to server for the connection. Once a connection
between client and server established, both the connected socket can communicate
with each other.

In other hand we can consider the work of port in connection-based communication


is like a medium through which, an application establish a connection with another
application by binding a socket by a port number. Addressing the information and
the port no., accompanied the data transfer over the network. The Ports are used by
TCP and UDP to deliver the data to the right application, are identified by a 16-bit
number.

If we consider the client-server architecture, a server application binds a socket to a


specific port number in connection-based communication. It registered the server
with the system where all the data destined for that port.

Now we are aware of the importance of the port number. In the same order there
are some ports which are predefine and called reserved ports. Some of them are
given below :-

Reserved port numbers.


Service Port no.
echo 7
daytime 13
ftp 21
telnet 23
smtp 25
finger 79
http 80
pop3 110

If we consider the range of the port numbers, there are 0 to 65,535 ports available.
The port numbers ranging from 0 - 1023 are reserved ports or we can say that are
restricted ports. All the 0 to 1023 ports are reserved for use by well-known services
such as FTP, telnet and http and other system services. These ports are called well-
known ports.

Java Networking
Java Networking is a concept of connecting two or more computing devices
together so that we can share resources.

Java socket programming provides facility to share data between different


computing devices.

Advantage of Java Networking


1. Sharing resources
2. Sharing information
3. Centralize software management

The [Link] package supports two protocols,

1. TCP: Transmission Control Protocol provides reliable communication between


the sender and receiver. TCP is used along with the Internet Protocol referred
as TCP/IP.
2. UDP: User Datagram Protocol provides a connection-less protocol service by
allowing packet of data to be transferred along two or more nodes

Java Networking Terminology


The widely used Java networking terminologies are given below:

1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket

1) IP Address
IP address is a unique number assigned to a node of a network e.g. [Link] . It is
composed of octets that range from 0 to 255.

It is a logical address that can be changed.

2) Protocol
A protocol is a set of rules basically that is followed for communication. For example:

o TCP
o FTP
o Telnet
o SMTP
o POP etc.

3) Port Number
The port number is used to uniquely identify different applications. It acts as a
communication endpoint between applications.

The port number is associated with the IP address for communication between two
applications.

4) MAC Address
MAC (Media Access Control) address is a unique identifier of NIC (Network Interface
Controller). A network node can have multiple NIC but each with unique MAC
address.
For example, an ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.

5) Connection-oriented and connection-less protocol


In connection-oriented protocol, acknowledgement is sent by the receiver. So it is
reliable but slow. The example of connection-oriented protocol is TCP.

6) Socket
A socket is an endpoint between two way communications.

Java Socket Programming


Java Socket programming is used for communication between the applications
running on different JRE.

Java Socket programming can be connection-oriented or connection-less.

Socket and ServerSocket classes are used for connection-oriented socket


programming and DatagramSocket and DatagramPacket classes are used for
connection-less socket programming.

The client in socket programming must know two information:

1. IP Address of Server, and


2. Port number.

Socket class
A socket is simply an endpoint for communications between the machines. The
Socket class can be used to create a socket.

Important methods
Method Description

1) public InputStream getInputStream() returns the InputStream attached with this


socket.

2) public OutputStream returns the OutputStream attached with this


getOutputStream() socket.

3) public synchronized void close() closes this socket

ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to
establish communication with the clients.

Important methods
Method Description

1) public Socket accept() returns the socket and establish a connection between
server and client.

2) public synchronized void closes the server socket.


close()

File: [Link]

import [Link].*;
import [Link].*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=[Link]();//establishes connection
DataInputStream dis=new DataInputStream([Link]());
String str=(String)[Link]();
[Link]("message= "+str);
[Link]();
}catch(Exception e){[Link](e);}
}
}

File: [Link]

import [Link].*;
import [Link].*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream([Link]());
[Link]("Hello Server");
[Link]();
[Link]();
[Link]();
}catch(Exception e){[Link](e);}
}
}

Common questions

Powered by AI

IP addresses and MAC addresses are both critical in network communication but serve different functions. An IP address is a logical address used to identify a device on a network, essential for routing packets across the internet. It can change as it is assigned by a network or DHCP. In contrast, a MAC address is a permanent physical address embedded in the NIC (Network Interface Controller) that uniquely identifies a device on a local network, ensuring the correct destination of frames within the local network .

Java's socket programming facilitates communication by enabling applications on different devices to exchange data over a network via sockets, which serve as endpoints of two-way communication. The Java.net package provides classes like Socket and ServerSocket for connection-oriented communication, and DatagramSocket for connection-less communication. These allow applications to send and receive data streams through established ports, leveraging IP addresses and port numbers for precise targeting of the communication endpoints .

In Java networking, the port number acts as an endpoint identifier for communication and is crucial in distinguishing multiple running applications using the same IP address. Each application binds to a specific port to send or receive data packets. Changing the port number can disrupt data flow if not properly updated across all relevant parts of the communication setup, potentially leading to failure in establishing connections or misrouting data to unintended applications .

TCP (Transmission Control Protocol) is connection-oriented, ensuring reliable communication through handshake protocols and requiring acknowledgment for data sent. This results in more reliable but slower data transmission, suitable for applications requiring data accuracy, like web pages and emails. In contrast, UDP (User Datagram Protocol) is connection-less, does not require acknowledgments, and is faster, making it ideal for applications where speed is crucial and data loss is tolerable, such as live streaming or gaming .

Java's networking capabilities enhance software management by enabling centralized resource management and facilitating data sharing and communication across devices. Using Java's networking features, such as socket programming and built-in protocol support (TCP/UDP), organizations can manage software updates and data exchanges from a central point. This reduces the overhead of individual device management and ensures consistency and reliability in software deployment and communication .

Java's support for both connection-oriented (TCP) and connection-less (UDP) protocols enhances its networking functionality by providing flexible options based on the application's needs. Connection-oriented support allows for reliable data transfer with error-checking and acknowledgment, vital for applications like web services and email. Connection-less support facilitates faster, less reliable communication suitable for real-time applications like video streaming or gaming, where speed is prioritized over perfect accuracy .

A client-server architecture in a computer network is significant because it centralizes resource management, reducing the need for multiple installations of software or storage across individual machines. For instance, in a file server setup, clients store files on a central server, which allows the entire network to access these resources without redundant copies on multiple machines. This results in efficient resource utilization and easier management .

Reserved port numbers play a critical role in network communication by standardizing and identifying services that certain applications use, which ensures that the correct application processes the data received on those ports. These ports, ranging from 0 to 1023, are crucial because they prevent conflicts by reserving specific numbers for common services such as HTTP (port 80) and FTP (port 21). This standardization is crucial for the proper operation and interoperability of internet services across different systems and networks .

Using well-known ports in client-server communications has security implications as these ports are predictable targets for attackers, since they are commonly used by well-known services like HTTP (port 80) and FTP (port 21). This makes network services vulnerable to attacks such as port scanning and DOS attacks. Securing these communications requires implementing measures like firewalls, port redirection, and encryption to protect data integrity and ensure that unauthorized individuals cannot exploit service vulnerabilities .

The Client/Server architecture is akin to an electrical socket where the server acts as the socket providing resources, while the client acts as the device (like a lamp) drawing on these resources. This analogy helps in understanding network operations by illustrating how clients depend on servers for accessing services and resources similar to a lamp requiring electricity from a socket to function, emphasizing the central role of the server .

You might also like