Unit 1
Unit 1
Unit 1
URL
URLConnection
Socket
ServerSocket
DatagramSocket
DatagramPacket
Network Basics
• internet communication is built of several layers:
• When you write JAVA applications that communicate over a network, you
are programming in the Application Layer.
• JAVA allows two types of communication via two main types of Transport
Layer protocols:
TCP vs UDP 8
Example of URL
• import java.io.*; import
java.net.*;
• public class URLDemo{
• public static void main(String[] args){
• try{
• URL url=new
URL("https://retail.axisbank.co.in/wps/portal/
rBanking/axisebanking/AxisRetailLogin") ;
• System.out.println("Protocol: "+url.getProtocol());
• System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
• }catch(Exception e){System.out.println(e);}
• }
• }
30
Output
Socket Programming
• Java Socket programming is used for
communication between the applications running
on different JRE.
• Java Socket programming can be connection-
oriented or connection-less.
• Socket and ServerSocket classes are used for
connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are
used for connection-less socket programming.
Socket Programming
• The client in socket programming must know
two information:
• IP Address of Server, and
• Port number.
JAVA TCP Sockets
• java.net.Socket
– Implements client sockets (also called just “sockets”).
– An endpoint for communication between two machines.
– Constructor and Methods
• Socket(String host, int port): Creates a stream socket and
connects it to the specified port number on the named host.
• InputStream getInputStream() : returns the InputStream
attached with this socket.
• OutputStream getOutputStream() : returns the
OutputStream attached with this socket.
• close() : closes this socket
• InetAddress getInetAddress();
• InetAddress getLocalAddress();
JAVA TCP Sockets
• java.net.ServerSocket
– Implements server sockets.
– Waits for requests to come in over the network.
– Performs some operation based on the request.
– Constructor and Methods
• ServerSocket(int port)
• Socket Accept(): Listens for a connection to be made to this
socket and accepts it. This method blocks until a connection
is made.
• InputStream getInputStream()
• OutputStream getOutputStream()
• InetAddress getInetAddress();
• int getLocalPort();
Stream Based Connection
Socket I/O
• Socket I/O is based on the Java I/O support
– in the package java.io
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
• import java.io.*;
• import java.net.Socket;
• public class client {
• public static void main(String[] args) {
• try{
• Socket s=new Socket("localhost",6666);
• DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
• DataInputStream dis=new
DataInputStream(s.getInputStream());
• System.out.println("Connected to server.");
• dout.writeUTF("Hello Server");
• String str=din.readUTF();
• System.out.print("Server says"+str);
• dout.close();
• s.close();
• }catch(Exception e){System.out.println(e);}
• } }
• import java.io.*;
• import java.net.*;
• public class server {
• public static void main(String[] args){
• try{
• ServerSocket ss=new ServerSocket(6666);
• Socket s=ss.accept(); //establishes connection
• System.out.println("Connection established");
• DataInputStream dis=new
DataInputStream(s.getInputStream());
• DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
• String str=(String)dis.readUTF();
• System.out.println("message from client= "+str);
• dout.writeUTF(“Hello Client);
• ss.close();
• }catch(Exception e){System.out.println(e);}
• }
• }
Socket Programming with UDP
• UDP
– Connectionless and unreliable service.
– There isn’t an initial handshaking phase.
– Doesn’t have a pipe.
– transmitted data may be received out of order, or lost
DatagramSocket
Constructors:
• DatagramSocket()
• DatagramSocket(int port)
• Methods:
• void receive(DatagramPacket p) - It receives the datagram
packet from the socket.
• void send(DatagramPacket p) - It sends the datagram packet
from the socket.
• void close()
Constructors: DatagramPacket 24
class UDPSender {
public static void main (String argv[]) throws Exception {
Scanner sc=new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
InetAddress ip = InetAddress.getByName("127.0.0.1");//IP of server
byte[] sendData = new byte[1024];
System.out.println(“Enter String to send”);
String str = sc.nextLine();
sendData = str.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData,sendData.length,ip,7777);
ds.send(sendPacket);
ds.close();
}
}
Example: UDPReceiver 26
Class UDPReceiver {
public static void main (String argv[]) throws Exception {
DatagramSocket serverSocket = new
DatagramSocket(7777);
byte[] receiveData = new byte[1024];
while(true) {
DatagramPacket receivePacket = new DatagramPacket
(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
String str = new String(receivePacket.getData());
System.out.println(str.trim());
InetAddress ip = receivePacket.getAddress();
System.out.print("IPAddress of Sender"+ip);
}
}
}
Chatclient(TCP)
public class chatclient {
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new
DataInputStream(s.getInputStream());
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
chatclient
str=sc.nextLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
din.close();
s.close();
}
}
Chatserver(TCP)
import java.io.*;
import java.net.*;
public class chatserver {
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new
DataInputStream(s.getInputStream());
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
Chatserver
str=din.readUTF();
System.out.println("client says: "+str);
str2=sc.nextLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
FileClient
class FileClient{
public static void main(String args[])
throws Exception{
Socket s = new Socket("127.0.0.1",3000);
DataOutputStream dos = new
DataOutputStream(s.getOutputStream());
System.out.print("Enter the file name");
Scanner sc = new Scanner(System.in));
String fname=sc.nextLine();
dos.writeUTF(fname);
FileClient
FileReader fr= new FileReader(fname);
BufferedReader br1=new BufferedReader(fr);
String str1;
dos.flush();
while((str1=br1.readLine()) != null)
{
System.out.print(str1);
dos.writeUTF(str1);
}
System.out.println(“File Transferred”);
br1.close();
s.close();
}
}
FileServer
class FileServer {
public static void main(String args[]) throws
Exception
{
ServerSocket ss = new ServerSocket(3000);
Socket s = ss.accept();
System.out.print("connection done");
DataInputStream dis = new DataInputStream(
s.getInputStream());
FileServer
RMI
36
RMI
37
RMI
38
Example: AdditionInterface
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
AdditionClass()
{
}
public int addition(int a, int b)
{
return (a+b);
}
}
Example: Server 40