Unit 1
Unit 1
Unit-1
Java Networking
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
Example
InetAddress ip
=InetAddress.getByName("www.gtu.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.gtu.ac.in/89.238.188.50
Example
InetAddress ip
=InetAddress.getByName("www.gtu.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.gtu.ac.in/89.238.188.50
Example
InetAddress ip=InetAddress.getLocalHost();
System.out.println(“LocalHost:“+ip);
Output:
LocalHost:My-PC/10.254.3.34
Example
InetAddress ip=InetAddress.getByName("10.254.3.34");
System.out.println(“Hostname:”+ip.getHostName());
Output:
Hostname:My-PC
Example
InetAddress ip=InetAddress.getByName("www.gtu.ac.in");
System.out.println(“HostAddress:”+ip.getHostAddress());
Output:
HostAddress:89.238.188.50
Output:
Host Name: www.gtu.ac.in
IP Address: 89.238.188.50
boolean equals(Object obj) Compares this object against the specified object.
InetAddress
URL
URLConnection This class implements Server sockets
ServerSocket
TCP
Socket This class implements Client sockets.
DatagramPacket
DatagramSocket
MulticastSocket
Constructor
ServerSocket(int port) Creates a server socket, bound to the specified
port.
Method
public Socket accept() returns the socket and establish a connection
between server and client.
Example
ServerSocket ss=new ServerSocket(1111);
socket
Method
public InputStream getInputStream() returns the InputStream attached with this
socket
public OutputStream getOutputStream() returns the OutputStream attached with this
socket.
Example
Socket s;
s=new Socket("localhost",1111);
Output
message= Hello Server
InetAddress
URL
URLConnection
ServerSocket This class represents a
datagram packet.
Socket
DatagramPacket
Represents a socket for sending
UDP DatagramSocket and receiving datagram
packets.
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.
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
Example
URL url=new URL("http://www.gtu.ac.in");
Method
public URLConnection openConnection() This method of URL class returns the
throws IOException object of URLConnection class
Example
URLConnection urlcon=url.openConnection();
public InputStream getInputStream() Returns an input stream that reads from this
throws IOException open connection.
public OutputStream getOutputStream() Returns an output stream that writes to this
throws IOException connection.
9 Write a client-server program using TCP sockets to echo the message send by [Sum-19]
the client.[7]
10 Explain the following classes with their use. i. URLConnection class [Sum-19]
ii. DatagramSocket (iii) DatagramPacket class [3]