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 the other word 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

import java.net.*;
import java.io.*;
public class MyMultithreadedServer extends Thread
{
Socket
client;
public MyMultithreadedServer (Socket s)
{
client = s;
start();
}
public void run()
{
try
{
OutputStream out =
client.getOutputStream();
InputStream   in =client.getInputStream();
PrintWriter pw = new PrintWriter(out,true);
BufferedReader br=
new BufferedReader(new InputStreamReader(in));
while(true)
{
String str=br.readLine();
System.
out.println("Reciving from client : "+str);
pw.println(
"Message from server : "+str);
}
}
catch(IOException e)
{
System.
out.println(e);
}
}
public static void main(String[] args)
{
try
{
ServerSocket ss=
new ServerSocket(7);
while(true)
{
Socket s=ss.accept();
MyMultithreadedServer mmts=
new  MyMultithreadedServer(s);
}
}
catch(IOException e)
{
 System.
out.println(e);
}
}
}

Client code

import
java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args)
{
try
{
Socket s=
new Socket(InetAddress.getLocalHost(),7);
OutputStream out=s.getOutputStream();
InputStream in=s.getInputStream();
PrintWriter pw =
new PrintWriter(out,true);
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1=
new BufferedReader(new InputStreamReader(in));
while(true)
{
String str=br.readLine();
pw.println(str);
System.
out.println(br1.readLine());
}
}

catch
(IOException e)
{
System.
out.println(e);
}
}
}

OUTPUT

This is the server output

servercmd.jpg

The first client connect and output


cientcmd.jpg

The other new client is connect 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 atrribute specified after the host name, for example www.google.com:80.
  •  File : Name of the file to be accesed 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 decoded any URL components according to the escaping mechanism itself. And it is the responsibility of 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 recognise 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 consider equal to each other.
This code made by using URL class which provide 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

import java.net.*;
import java.io.*;
class MyURLClass
{
public static void main(String[] args)
{
try
{
URL u =
new URL(args[0]);
System.
out.println("Protocol Name :"+u.getProtocol());
System.
out.println("Host Name :"+u.getHost());
System.
out.println("file Name :"+u.getFile());
System.
out.println("Port No :"+u.getPort());
System.
out.println("Reference Name :"+u.getRef());
URLConnection uc=u.openConnection();
InputStream in=uc.getInputStream();
System.
out.println("stream is created");
BufferedReader br=
new BufferedReader(new InputStreamReader(in));
String str=
null;
while((str=br.readLine())!=null)
System.
out.println(str);
br.close();
}
catch(MalformedURLException e)
{
System.
err.println(e);
}
catch(IOException ie)
{
System.
err.println(ie);
}
}
}

OUTPUT

urlcmd.jpg

Resources

Web ProxyServer in C# and VB

Sample of MulthiThreaded Application using C#.Net: Part III

Thread Synchronization using VS.NET 2005

Volatile keyword in C# Threading

Up Next
    Ebook Download
    View all
    Learn
    View all