You are on page 1of 7

Networking and java Library Network: A collection of interconnected computers is called a network.

To have the information exchange between the connected computers, we have 2 end to end protocols specifying the type of the connection. They are 1) Connection oriented protocol(TCP- Transmission Control protocol) 2) Connection less protocol(UDP- User Datagram Protocol)

Protocol: A protocol is a set of rules and conventions used in order to have information
exchange over the network. TCP- Transmission Control protocol: 1) It is the reliable connection oriented protocol that allows a byte stream from one machine to be delivered without error on any other machines over the network. 2) TCP does the flow control i.e. it checks that a fast sender is not going to overwhelm a slow receiver with more information. 3) It sends the fragments of information to the destination, where all the fragment are collected over there. UDP- User Datagram Protocol: 1) It is an unreliable connectionless protocol for application that does not need sequencing (sequential informational flow). 2) The data/information to be delivered at the destination is placed in packets 3) All the packets at the destination are collected and then the information is placed in the sequential order. 4) Every packet should have the destination address with them. 5) These independent packets are called datagrams. 6) But using UDP we can have multiple clients. Socket: In order to connect to a target system we need a new communication end point called socket. A socket is a plug-in application. A numbered socket is called a port.

Socket : In order to connect to a target system we need a new communication end point
called socket. A socket is a plug-in application. Each socket has a socket number. A numbered socket is called a port. The port numbers below 256 are called well-know ports or reserved ports. For example Application name port number FTP 21 TELNET 23 HTTP 80

SMTP POP

25 110

There are 3 kinds of sockets: TCP - Connection oriented(Uses streams) UDP - Connection less (Uses datagrams) Multicast - Connection less (Uses datagrams) TCP & UDP - One to one connection Multicast – One to many connection Many to one connection Many to many connection. In order to connect to the remote system/host we need 1) Address of that host 2) Port number of the host. Port number: The number given to each and every device, all connected to the system, in order to find out the device interacting with it. Java provides the networking capabilities through java.net package Some classes list in the java.net package – Socket ServerSocket DatagramPacket DatagramSocket MultiCastSocket URL URLConnection InetAddress Any system connected in the network can be identified by IP Adress or by DNS(Domain Name Service).

Ip Address : It is 4 bytes or 32 bit address given to the system in internet for
identification. Ex: 192.200.10.01 - An internet address is a number, where each computer on the network can be uniqely identified. DNS (Domain Name Service) : 1) Instead of addressing a system with a number, we can also specify the computer hierarchy with DNS. 2)IP Address describes a network hierarchy from left to right and the domain name specifies the hierarchy from right to left. The name of an internet address called its domain name – describes a machine location.

InetAddress: This class has no public constructors in order to create an object of
InetAddress class we use its factory method.

Factory Method: A static method which returns the instance of a class is called factory
method. Example Factory methods: InetAddress getByName(String host) Ex: InetAddress ia = new InetAddress(“node4”); InetAddress[] getAllByName(String host) – Returns the IP address of all hosts having same DNS names as an array of InetAddress objects. InetAddress getLocalHost()Returns the current system address or the local system address as IpAddress/ DNS combination. String getHostName()Returns the DNS of the system bound in an InetAddress object. Ex: InetAddress ia =InetAddress.getLocalHost(); String hostName = InetAddress.getHostName(); String getHostAddress()Returns the IPAddress of the system bound in an InetAddress object. Ex: InetAddress ia =InetAddress.getByName(“node1”); String hostAddress = InetAddress.getHostAddress(); Socket class : Constructor Socket(String target, int portNo) target – Ip address or DNS Ex: Socket s = new Socket(“localhost”, 1234); Exception raised –This socket constructor will throw an checked exception- UnknownHostException, which is to be handled. Methods InputStream getInputStream() – Returns the fullfledged object of the input stream. OutputStream getOutputStream()Returns the fullfledged object of the output stream. ServerSocket Class : Constructor – ServerSocket(int portNum) Ex: ServerSocket ss = new ServerSocket(1234); The server is listening always to its server socket at the port number say 1234.

When client connects to this socket on the same port number 1234, server should accept the client connection. Method used to accept the client connectionSocket accept() – Accepts the client connection and returns a full fledged object of Socket. Socket s = ss.accept(); TCP/IP SocketEx: Program import java.io.*; import java.net.*; class Client{ public static void main(String args[]){ Socket s= new Socket(“node1”, 1234); DataInputStream dis = new DataInputStream(System.in); DataOutputStream dout = new DataOutputStream(s.getOutputStream()); String line = dis.readLine(); dout.writeBytes(line + “\n”); dout.close(); dis.close(); s.close(); } } class Server{ public static void main(String args[]){ ServerSocket ss = new ServerSocket(1234); Socket s = ss.accept(); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readLine()); dis.close(); s.close(); ss.close(); } } Using UDP Socket: 1. UDP socket is connectionless. 2. Need to prepare Datagram packet 3. Send this packet using datagram socket. Sender –

DatagramPacket classConstructorDatagramPacket(byte b[], int size, InetAddress target, int portNo) DatagramSocket classConstructorDatagramSocket() Methodpublic void send(DatagramPacket dp) Receiver – DatagramPacket classConstructorDatagramPacket(byte b[], int size) DatagramSocket classConstructorDatagramSocket( int portNo) Methodpublic void receive(DatagramPacket dp) Ex: program using datagramPacket import java.net.*; import java.io.*; class Sender{ public static void main(String[] args){ String msg = “Hello Receiver”; byte b[] = msg.getBytes(); InetAddress target = InetAddress.getLocalHost(); DatagramPacket dp = new DatagramPacket( b, b.length, target, 1234); DatagramSocket ds= new DatagramSocket(); ds.send(dp); } } class Receiver{ public static void main(String[] args){ byte b[] = new byte[100]; DatagramPacket dp = new DatagramPacket( b, b.length); DatagramSocket ds= new DatagramSocket(1234); ds.receive(dp); String s = new String(b); System.out.println(s); } }

MultiCastSockets: The multicast addres range – 224.0.0.0 . . 239.255.255.255 The multicast address is a logical address not physical address. Ex Program: import java.net.*; import java.io.*; class MultiCastSender{ public static void main(String[] args){ InetAddress target = InetAddress.getByName(“244.10.10.10”); String s = “Hi receiver”; byte b[] = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length, target, 1234); MulticastSocket m = new MultiCastSocket(); m.send(dp); } } class MultiCastReceiver{ public static void main(String[] args){ InetAddress target = InetAddress.getByName(“244.10.10.10”); byte b[] = new byte[30]; DatagramPacket dp = new DatagramPacket(b, b.length); MulticastSocket m = new MultiCastSocket(1234); m.joinGroup(target); m.receive(dp); String msg = new String(b); System.out.println(msg); } } Note: There is no necessity for the sender to join the group. URL (Unified Resource Locator)- Used to contact any existing server in the network. This works better for hhtp web server and gives the information about the default files , content type, content size etc. URL classURL urlObject = new URL(“www.google.com”);

Methods – public int getPort() public String getProtocol() public String getFileName() public String getContentType() public int getContentSize() public URLConnection openConnection() URLConnection class – Methods – public InputStream getInputStream() public OutputStream getOutputStream() Ex: Program to contact yahoo site, retrieve data and write in the local hard drive. Class Download{ public static void main(String args[]){ URL url = new URL(“www.yahoo.com”); URLConnection con = u.openConnection(); RandomAccessFile rf = new RandomAccessFile(con.getFileName(), “rw”); BufferedInputStream bin = new BufferedInputStream( con.getInputStream()); DataInputStream dis = new DataInputStream(bin); String line = dis.readLine(); while(line != null){ rf.writeBytes(line + “\n”); line = dis.readLine(); } rf.close(); bin.close(); dis.close(); } }