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. 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);}} }
OUTPUTThis is the server outputThe first client connect and outputThe other new client is connect and output is followingURL 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
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);}} }
OUTPUTResources
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: