Escolar Documentos
Profissional Documentos
Cultura Documentos
• URL class
• InetAddress class
Networking:
Advantage:
• sharing resources
Networking Terminology:
IP Address
HostName
Protocol
Port Number
MAC Address
Connection-oriented and connectionless protocol
Socket
IP Address:
IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 .
the default Ip address of a computer is 127.0.0.1 and it's mapped host name is
localhost.
Host name is the alias name of the computers. as human being we cannot remember
comupters with numbers. so each computer Ip address is mapped with a string called
hostname.
Note:-
Example:-
192.168.2.19 NareshIT19
192.168.2.20 NareshIT20
to know IP address--ipconfig
Hostname------hostname
Protocol:
protocol is a set of instutcions to send request and receive response via network.
Types of protocols:-
1.TCP/IP : Tcp/IP is a connection oriented and secured protocol, for every request
we will get response.
1. Http
2. Https
3. Ftp
4. Smtp
2. UDP--UserDatagram protocol:
UDP is connection less protocol, and it is nonsecured protocol. using this protocol we
cannot guarantee data transfer.
Ex:-
Socket Programming:
A socket is one end-point of a two-way communication link between two
programs running on the network. Socket classes are used to represent the
connection between a client program and a server program. The java.net
package provides two classes--Socket and ServerSocket--that implement the
client side of the connection and the server side of the connection, respectively.
Socket class:
A socket is simply an endpoint for communications between the machines. The Socket
class can be used to create a socket.
ServerSocket class:
The ServerSocket class can be used to create a server socket. This object is used to
establish communication with the clients.
Q) What is server?
ans: a server is a networking program that can receive a request from client and it can send response to
the client.
Q) What is client?
ans: a client is also a networking program that can send a request to the server and it can receive a
response from the server.
Q) How to develop client and server programs?
To develop client and server program we use ServerScoket and Socket classes of java.net package and
the following steps:
import java.net.Socket;
import java.net.ServerSocket;
try{
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){
System.out.println(e);
import java.io.DataOutputStream;
import java.net.Socket;
try{
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){
System.out.println(e);
To execute above Socket programs open two command prompts and execute each
program at each command prompt as displayed in the below figure. First execute
server app and then after client app.
Note :
* to send the multiple requests and get the Multiple responses from server put the
logic inside an infinite loop (i.e. while (true)).
See the next example to multiple requests and get multiple responses from server.
import java.io.*;
import java.net.*;
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
while(true){
String data1=br.readLine();
dos.writeUTF(data1);
String data2=dis.readUTF();
if(data1.equalsIgnoreCase("bye"))
break;
import java.io.*;
import java.net.*;
System.out.println("Server Started..");
Socket s=ss.accept();
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
while(true){
String data1=dis.readUTF();
String data2=br.readLine();
dos.writeUTF(data2);
if(data2.equalsIgnoreCase("Ok Bye"))
break;
Output:
URL class:
The URL class represents a URL. URL is an acronym for Uniform Resource Locator. It
points to a resource on the World Wide Web. For example:
http://www.nareshit.com/index.jsp
• File Name or directory name: In this case, index.jsp is the file name.
import java.net.URL;
try{
System.out.print("Protocol: "+url.getProtocol());
catch(Exception e){
System.out.println(e);
}
Output :
InetAddress class:
datagrams sends and receives completely independent packets of information and they
also don?t need a dedicated point-to-point channel.
Datagrams are simply a bundles of information data passed between machines. Java
implements datagrams on top of the UDP protocol by using the following classes which
are in java.net package as well as define as under :
DatagramSocket
DatagramPacket
DatagramSocket class:
DatagramPacket class:
The DatagramPacket is message that can be sent or received. If you send multiple
packet, it may arrive in any order. Moreover, packet delivery is not guaranteed.
import java.net.*;
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
import java.net.*;
ds.receive(dp);
System.out.println(str);
ds.close();
Summary :
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 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.
server is a networking program that can receive a request from client and it can
send response to the client.
client is also a networking program that can send a request to the server and it
can receive a response from the server.
To develop client and server program we use ServerScoket and Socket classes of
java.net package
A URL (Uniform Resource Locator) is the address of a resource on the Internet.
In java network programming we can use URLs to connect and retrieve
information over the Internet.
DatagramSocket
DatagramPacket