Multithreaded Sockets (Multithreaded Server) and Working With URL Class

Multithreaded Socket

 
Concurrent client and server application can be built in java using the concept of multi-threading which describe in my previous article. Concurrent server are those that can process many clients at a time. In practically all the server are multithreaded. These server allowed to give the response simultaneously of each client so Clients need not wait for other clients to finish their interaction with the server. In other words this called the parallel execution. A client request arrives at the server it is accepted a new a thread is created for handling the clients request.
 
multithreading7.JPG 

Multithreaded Server code
  1. import java.net.*;  
  2. import java.io.*;  
  3. public class MyMultithreadedServer extends Thread {  
  4.     Socket client;  
  5.     public MyMultithreadedServer(Socket s) {  
  6.         client = s;  
  7.         start();  
  8.     }  
  9.     public void run() {  
  10.         try {  
  11.             OutputStream out = client.getOutputStream();  
  12.             InputStream in = client.getInputStream();  
  13.             PrintWriter pw = new PrintWriter(out, true);  
  14.             BufferedReader br = new BufferedReader(new InputStreamReader( in ));  
  15.             while (true) {  
  16.                 String str = br.readLine();  
  17.                 System.out.println("Reciving from client : " + str);  
  18.                 pw.println("Message from server : " + str);  
  19.             }  
  20.         } catch (IOException e) {  
  21.             System.out.println(e);  
  22.         }  
  23.     }  
  24.     public static void main(String[] args) {  
  25.         try {  
  26.             ServerSocket ss = new ServerSocket(7);  
  27.             while (true) {  
  28.                 Socket s = ss.accept();  
  29.                 MyMultithreadedServer mmts = new MyMultithreadedServer(s);  
  30.             }  
  31.         } catch (IOException e) {  
  32.             System.out.println(e);  
  33.         }  
  34.     }  
  35. }  
Client code
  1. import java.net.*;  
  2. import java.io.*;  
  3. public class Client {  
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             Socket s = new Socket(InetAddress.getLocalHost(), 7);  
  7.             OutputStream out = s.getOutputStream();  
  8.             InputStream in = s.getInputStream();  
  9.             PrintWriter pw = new PrintWriter(out, true);  
  10.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  11.             BufferedReader br1 = new BufferedReader(new InputStreamReader( in ));  
  12.             while (true) {  
  13.                 String str = br.readLine();  
  14.                 pw.println(str);  
  15.                 System.out.println(br1.readLine());  
  16.             }  
  17.         } catch (IOException e) {  
  18.             System.out.println(e);  
  19.         }  
  20.     }  
  21. }  
OUTPUT
 
This is the server output
 
servercmd.jpg 
 
The first client connect and output
 
cientcmd.jpg 
 
The other new client is connected and output is following
 
client1cmd.jpg 
 

URL class

 
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. It is standard way of locating resources on the Internet, e.g www.google.com has some basic parts these part are list below.

Following fields of the URL
  •  Protocol name : http/ftp/mailto, etc.
  •  Host: www.google.com.
  •  Port: This is an option the attribute specified after the host name, for example www.google.com:80.
  •  File: Name of the file to be accessed for example www.google.com/home.html.
  •  Reference: A URL may have appended to it a "fragment", also known as a "ref" or a "reference". The fragment is indicated by the sharp sign character "#" followed by more characters. www.google.com/home#cs.
  •  Relative: An application can also specify a "relative URL", which contains only enough information to reach the resource relative to another URL. Relative URLs are frequently used within HTML pages.
    For example, if the contents of the URL:http://java.sun.com/index.html  contained within it the relative  URL:FAQ.html
    It would be a shorthand for: http://java.sun.com/FAQ.html
Note: The URL class does not encode and decode any URL components according to the escaping mechanism itself. And it is the responsibility of the caller to encode any fields which need to be escaped previous calling URL. And decode any field returned from URL. because URL has no information about URL escaping .it does not recognize equivalence between the encoded or decoded form of the same URL. For example, the two URLs.

Example

http://foo.com/hello world/
 
and
 
http://foo.com/hello%20world.

both are not considered equal to each other.

This code made by using URL class which provides by java in .net package. And in this program we not use all the methods of URL class but we use some general methods.

Example
  1. import java.net.*;  
  2. import java.io.*;  
  3. class MyURLClass {  
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             URL u = new URL(args[0]);  
  7.             System.out.println("Protocol Name :" + u.getProtocol());  
  8.             System.out.println("Host Name :" + u.getHost());  
  9.             System.out.println("file Name :" + u.getFile());  
  10.             System.out.println("Port No :" + u.getPort());  
  11.             System.out.println("Reference Name :" + u.getRef());  
  12.             URLConnection uc = u.openConnection();  
  13.             InputStream in = uc.getInputStream();  
  14.             System.out.println("stream is created");  
  15.             BufferedReader br = new BufferedReader(new InputStreamReader( in ));  
  16.             String str = null;  
  17.             while ((str = br.readLine()) != null)  
  18.                 System.out.println(str);  
  19.             br.close();  
  20.         } catch (MalformedURLException e) {  
  21.             System.err.println(e);  
  22.         } catch (IOException ie) {  
  23.             System.err.println(ie);  
  24.         }  
  25.     }  
  26. }  
OUTPUT
 
urlcmd.jpg 
 
Resources


Similar Articles