Networking DU
Networking DU
Advanced Java
Unit-1
Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Server Client
Response
socket socket
Request
Waits
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Reference: isinotes/javatut/net
Server
Client 1
Reference: isinotes/javatut/net
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Example
InetAddress ip
=InetAddress.getByName("www.darshan.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.darshan.ac.in/89.238.188.50
boolean equals(Object obj) Compares this object against the specified object.
Method
Method
public InputStream getInputStream() returns the InputStream attached with this
throws IOException socket
Constructor
DatagramPacket(byte[] barr, int length) It creates a datagram packet. This
constructor is used to receive the
packets.
DatagramPacket(byte[] barr, int length, It creates a datagram packet. This
InetAddress address, int port) constructor is used to send the
packets.
Example
InetAddress ip
=InetAddress.getByName("www.darshan.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.darshan.ac.in/89.238.188.50
Example
InetAddress ip=InetAddress.getLocalHost();
System.out.println(“LocalHost:“+ip);
Output:
LocalHost:swati-PC/10.254.3.34
Example
InetAddress ip=InetAddress.getByName("10.254.3.34");
System.out.println(“Hostname:”+ip.getHostName());
Output:
Hostname:swati-PC
Example
InetAddress ip=InetAddress.getByName("www.darshan.ac.in");
System.out.println(“HostAddress:”+ip.getHostAddress());
Output:
HostAddress:89.238.188.50
Output:
Host Name: www.darshan.ac.in
IP Address: 89.238.188.50
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
InetAddress
URL
URLConnection
This class implements Server sockets
ServerSocket
TCP
Socket
This class implements Client sockets.
DatagramPacket
DatagramSocket
MulticastSocket
Example
ServerSocket ss=new ServerSocket(1111);
Example
Socket s;
s=new Socket("localhost",1111);
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
InetAddress
URL
URLConnection
ServerSocket This class represents a
Socket datagram packet.
DatagramPacket
DatagramSocket Represents a socket for
UDP
sending and receiving
datagram packets.
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0,dp.getLength());
System.out.println(str);
ds.close();
}
}
Output
Message sent by Datagram socket
ds.send(dp); System.out.println(str);
ds.close(); ds.close();
}
} }
}
Output
Message sent by Datagram socket
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP client sockets
4. TCP/IP server sockets
5. Datagrams
6. URL
7. URLConnection
Example
URL url=new URL("http://www.darshan.ac.in");
Method
public URLConnection openConnectio This method of URL class returns the
n() throws IOException object of URLConnection class
Example
URLConnection urlcon=url.openConnection();
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP client sockets
4. TCP/IP server sockets
5. Datagrams
6. URL
7. URLConnection
public InputStream getInputStream() Returns an input stream that reads from this
throws IOException open connection.
public OutputStream getOutputStrea Returns an output stream that writes to this
m() throws IOException connection.
10 Explain the following classes with their use. i. URLConnection class [Sum-19]
ii. DatagramSocket (iii) DatagramPacket class [3]