You are on page 1of 10

Networking with Java

Networking Basics N/W with Java

Pc4

We share resources
Pc 1 and data with each Pc2
other

Pc3

 Networking simply means connecting two or more computing devices together so that we can
share resources and data within those devices.

 In Java, Networking is achieved through socket programming.


Networking Basics - Sockets N/W with Java

 A socket is one endpoint of a two-way communication link between two programs running on
the network.
 A socket is bound to a port number.
 Consider,
We have two machines connected in a network. One of them provides a service/resource while
the other uses that service/resource. The machine providing the service becomes the server and
the machine asking for service becomes the client.

Steps in establishing a connection between server and client.

Step 1 : Client sends a request to the server asking for connection.

Request for connection,


server server port client_portNo port server client

The client knows the hostname and port number of the server machine. It sends a request on this
port. At the same time, client binds one of its local ports to listen to the server.
Networking Basics - Sockets N/W with Java

Step 2 : Server accepts the connection requests:

socket socket

server port port server

Server accepts the incoming request and dedicates on of its sockets, connected to its port, to
the client. Thus we have one socket on the server side and its corresponding socket on the
client side.

Step 3 : Using these sockets, the server and the client will communicate, i.e send data, share
resources etc with each other. Thus they read and write on the SOCKETS dedicated to them.

socket socket
Read/write

server port port server


Networking Basics - Port N/W with Java
 The TCP and UDP protocols use ports to map incoming data to a particular process running
on a computer.

 A computer on a network has a single physical connection to the network. All data for a
given computer arrives through that connection. However, the data may be intended for different
applications running on the computer.

 It is through the use of ports that data intended for a specific application reaches that
application. It is because data is accompanied by addressing information that identifies the
computer and the port for which it is sent.

 In connection-based communication such as TCP, a server application binds a socket to a


specific port number. This has the effect of registering the server with the system to receive
all data destined for that port. A client can then rendezvous with the server at the server's port,
socket
socket

socket
socket

port port port port

Over TCP port # data


Destination Computer or UDP
packet
java.net N/W with Java

 Java (J2SE) provides java.net package which consists of classes and interfaces for
communicating over a network.

 These classes and interfaces support following protocols :


a. TCP (Transmission control protocol)
b. UDP (User Datagram Protocol)

 Networking in Java deals with two things, viz :

a. Socket programming
b. URL processing

 Socket class and ServerSocket class in java.net are needed to establish connection with
and talk to sockets respectively.
Networking Basics - Socket N/W with Java

Q > Write a client-server socket program where the server prints the message coming
from the Client.

Refer MyServer.java and MyClient.java for implementation.

Q > Write a client server socket program where the client and server can send messages to
each other and read those messages.

Refer MyRWClient.java and MyRWServer.java for implementation.

Q > Write a client-server socket program. Client program will accept file name from
user and sends it to server. If file does not exists, server will send “File does
not exists” else server will read file contents, will replace vowels with ‘+’ and
sends it to client

Refer FileReaderClient.java and FileReaderServer.java files for implementation.


Networking Basics - URL N/W with Java

 Stands for Uniform Resource Locator .

 It is a reference (an address) to a resource on the Internet.

 In Java we use the URL class belonging to java.net package.

 A URL takes the form of a string that describes how to find a resource on the Internet.

 URLs have two main components: the protocol needed to access the resource and the location
of the resource.

URLs are "write-once" objects. Once you've created a URL object, you cannot change any of its
attributes

 The easiest way to create a URL object is from a String that represents URL address.

URL myURL = new URL("http://example.com/");


 Each of the four URL constructors throws a MalformedURLException if the arguments to the
constructor refer to a null or unknown protocol. Hence include URL creation within try-catch block.

try {
URL myURL = new URL(...);
} catch (MalformedURLException e) {
// exception handler code here // ...
}
Networking Basics N/W with Java

 Some methods of URL class which can be used to parse a given URL :

getProtocol
Returns the protocol identifier component of the URL.
getAuthority
Returns the authority component of the URL.
getHost
Returns the host name component of the URL.
getPort
Returns the port number component of the URL. The getPort method returns an
integer that is the port number. If the port is not set, getPort returns -1.
getPath
Returns the path component of this URL.
getQuery
Returns the query component of this URL.
getFile
Returns the filename component of the URL. The getFile method returns the same as
getPath, plus the concatenation of the value of getQuery, if any.
getRef
Returns the reference component of the URL.
Thank you.

You might also like