2. TCP & UDP TCP(TRANSMISSION CONTROL PROTOCOL) TCP is a connection-based protocol that provides a reliable flow of data between two computers. e.g. : Telephone call. Web browsing. UDP(USER DATAGRAM PROTOCOL) UDP is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. e.g.: ping command.
3. PORT Ports are identified by a 16-bit number. The TCP and UDP protocols use PORTS to map incoming data to a particular process running on a computer.
4. INTERNET ADDRESSING & DNS(DOMAIN NAME SYSTEM) Every computer on a network is known by a unique number which is known as IP Address. Generally we use IPV4 which consists of 4 BYTES. (e.g. 172.17.167.4)etc. Websites have both a friendly address, called a URL, and an IP address. People use URLs to find websites, but computers use IP addresses to find websites. DNS translates URLs into IP addresses (and vice versa).
5. NETWORKING CLASSES(java.net PACKAGE) InetAddress :- This class is used to encapsulate both the numerical IP address and the domain name for that address. To create an InetAddress object we have to use one of the available factory methods:- 1.static InetAddress getLocalHost( ) throws UnknownHostException 2.static InetAddress getByName(String hostName) throws UnknownHostException 3.static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
6. import java.net.*; public class prog1 { public static void main(String args[])throws UnknownHostException { InetAddress in; in=InetAddress.getLocalHost(); System.out.println("local host name="+in); in=InetAddress.getByName("www.google.com"); System.out.println("ip of google::"+in); InetAddress dn[]=InetAddress.getAllByName("www.google.com"); System.out.println("ALL NAMES FOR GOOGLE"); for(int i=0;i<dn.length;i++) System.out.println(dn[i]); } } PROGRAM TO ILLUSTRATE InetAddress
7. URL:- URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. http :// www.google.com:80/index.htm PROTOCOL IDENTIFIER RESOURCE NAME CONSTRUCTOR:- 1. URL(String urlSpecifier) 2.URL(String protocolName, String hostName, int port, String path) 3.URL(String protocolName, String hostName, String path) All of these constructor throws MalformedURLException.
8. PROGRAM TO ILLUSTRATE URL import java.io.*; import java.net.*; public class prog2 { public static void main(String args[])throws MalformedURLException { URL u=new URL("http://www.google.com:80/index.htm"); System.out.println("HOST NAME="+u.getHost()); System.out.println("PORT ="+u.getPort()); System.out.println("PROTOCOL="+u.getProtocol()); System.out.println("PATH="+u.getFile()); } }
9. THE openStream() method It is used for directly reading from a URL. import java.net.*; import java.io.*; public class prog3 { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); In.close(); } }
10. READING FROM A URL THROUGH URLConnection OBJECT We can create URLConnection object using the openConnection() method of URL object. import java.net.*; import java.io.*; public class prog4 { public static void main(String[ ] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }
11.
12.
13. Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
14. LET’S DEAL WITH ServerSocket CLASS accept():- ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications, and then return with a normal Socket that is then used for communication with the client. e.g . ServerSocket s=new ServerSocket(8189); establishes a server that monitors port 8189. The command Socket sp = s.accept(); tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made.
15.
16. PROG2:-COMMUNIACTION BETWEEN MULTIPLE CLIENTS AND A SERVER USING SOCKETS SIMULTANEOUSLY(no need to wait).(ServerSocket) import java.io.*; import java.net.*; import java.util.*; public class prog6 { public static void main(String args[]) throws Exception { int i=1; ServerSocket s=new ServerSocket(8120); System.out.println(" SERVER WAITS FOR THE CLIENTS TO BE CONNECTED..........."); while(true) { Socket sp=s.accept(); System.out.println("client no."+i+"connected"); Runnable r=new netthread(sp,i); Thread t=new Thread(r); t.start(); // it begins the execution of thread beginning at the run() method i++; } } }