Professional Documents
Culture Documents
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.
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.
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
socket socket
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
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.
socket
socket
Java (J2SE) provides java.net package which consists of classes and interfaces for
communicating over a network.
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.
Q > Write a client server socket program where the client and server can send messages to
each other and read those messages.
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
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.
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.