Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Multithreaded client/server

  1. #1
    Member
    Join Date
    May 2013
    Posts
    161
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Multithreaded client/server

    I have been trying to create a multithreaded client server though not working.
    import java.net.*;
    import java.io.*;
     
    import org.omg.CORBA.PUBLIC_MEMBER;
     
      public class Server implements Runnable{
    	 public static void main(String[] args)throws IOException{
    		 (new Thread(new Server())).start();
    	 }
    	 public void run(){
    		 try{
    		 ServerSocket serverSocket = new ServerSocket(8765);
    		 Socket clientAccept = serverSocket.accept();
    		 while(true){
    		 BufferedReader input = new BufferedReader(new InputStreamReader(clientAccept.getInputStream()));
    		 String s = input.readLine();
    		 System.out.println(s);
     
    		 BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    		 String p = out.readLine();
    		 PrintStream print = new PrintStream(clientAccept.getOutputStream());
    		 print.println(p);
    		 }
    		 }catch(IOException e){
    			 e.printStackTrace();
    		 }
     
     }
      }
    import java.io.*;
    import java.net.*;
     
    public class Client1 implements Runnable{
     
     
    	public static void main(String[] args)throws IOException{
    	(new Thread(new Client1())).start();
    	}
     
     
     
    		@Override
    			public void run(){
    				try{
    				while(true){
    			    Socket socket = new Socket("127.0.0.1",8765);
    				//Attempted output
    				BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    				String s = out.readLine();
    				PrintStream print = new PrintStream(socket.getOutputStream());
    				print.println(s);
    				//Attempted Input
    				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				String p = in.readLine();
    				System.out.println(p);
    				}
    				}catch(IOException e){
    					e.printStackTrace();
    				}
    				}
     
    		}
    import java.io.*;
    import java.net.*;
     
    public class Client implements Runnable{
    	public static void main(String[] args)throws IOException{
    	(new Thread(new Client())).start();
    	}
     
     
     
    		@Override
    			public void run(){
    				try{
    				while(true){
    			    Socket socket = new Socket("127.0.0.1",8765);
    				//Attempted output
    				BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    				String s = out.readLine();
    				PrintStream print = new PrintStream(socket.getOutputStream());
    				print.println(s);
    				//Attempted Input
    				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				String p = in.readLine();
    				System.out.println(p);
    				}
    				}catch(IOException e){
    					e.printStackTrace();
    				}
    				}
     
    		}


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded client/server

    How is it not working? Is there an error?

  3. #3
    Member
    Join Date
    May 2013
    Posts
    161
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Multithreaded client/server

    Thank you for replying, the server does not output to the right client, sometimes the client does not output to the server and sometimes server returns null. I think the data is getting mixed up, I tried to use the synchronized on the run methods though still get mixed wrong results.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Multithreaded client/server

    I am not sure how your server is multi-threaded - it creates a single thread to handle all IO from the clients, who then both continually try to connect in an infinite while loop, and once one does connect sends the single thread into an infinite loop. You should consider Thread pool on the Server, each thread of which handles a client
    See
    Thread Pools (The Java™ Tutorials > Essential Classes > Concurrency)

    You should also properly close all connections and Streams

  5. #5
    Member
    Join Date
    May 2013
    Posts
    161
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Multithreaded client/server

    I've done the program and it works great though not sure if it would be considered multithreaded which was my aim, what do you guys think?
    import java.io.*;
    import java.net.*;
    public class Server{
    	public static void main(String[] args)throws IOException{
    		ServerSocket serverSocket = new ServerSocket(8976);
    		while(true){
    			final Socket clientAccept = serverSocket.accept();
    			Runnable Task = new Runnable(){
     
    				@Override
    				public void run() {
    					try {
    						input(clientAccept);
    						output(clientAccept);
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
     
    				}
     
     
    		};
    		new Thread(Task).start();
    		}
     
    	}
    	public static synchronized void input(Socket connection)throws IOException{
    		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    		String read = reader.readLine();
    		System.out.println(read);
    	}
    	public static synchronized void output(Socket connection)throws IOException{
    		BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    		String output = out.readLine();
    		PrintStream print = new PrintStream(connection.getOutputStream());
    		print.println(output);
    	}
    }
    import java.net.*;
    import java.io.*;
    public class Client{
    	public static void main(String[] args)throws IOException{
    		Socket socket = new Socket("127.0.0.1",8976);
    		while(true){
    		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    		String read = reader.readLine();
    		PrintStream print = new PrintStream(socket.getOutputStream());
    		print.println(read);
     
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		String out = in.readLine();
    		System.out.println(out);
    		}
     
    	}
    }
    java concurrency in practice has been a great help.
    Thanks for the advice so far guys.

  6. #6
    Member
    Join Date
    May 2013
    Posts
    161
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Multithreaded client/server

    I'm trying to do the same task with thread pools and it works for around two cycles then the server stops inputting and outputting, I'm not sure what the problems is, please could someone advise me.
    import java.net.*;
    import java.io.*;
    public class Client{
    	public static void main(String[] args)throws IOException{
    		Socket socket = new Socket("127.0.0.1",8976);
    		while(true){
    		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    		String read = reader.readLine();
    		PrintStream print = new PrintStream(socket.getOutputStream());
    		print.println(read);
     
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		String out = in.readLine();
    		System.out.println(out);
    		}
     
    	}
    }
    import java.io.*;
    import java.net.*;
    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    public class Server implements Executor{
    	private static  final Executor exec = Executors.newFixedThreadPool(2);
    	public static void main(String[] args)throws IOException{
    		ServerSocket serverSocket = new ServerSocket(8976);
    	   while(true){
    		   final Socket clientAccept = serverSocket.accept();
    		   Runnable Task = new Runnable(){
     
    			@Override
    			public void run(){
    				try{
    					input(clientAccept);
    				    output(clientAccept);
    				}catch(IOException e){
    					e.printStackTrace();
    				}
    			}
    		   };
    		   exec.execute(Task);
    	   }
     
    	}
     
     
    	public static synchronized void input(Socket connection)throws IOException{
    		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    		String read = reader.readLine();
    		System.out.println(read);
    	}
    	public static synchronized void output(Socket connection)throws IOException{
    		BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    		String output = out.readLine();
    		PrintStream print = new PrintStream(connection.getOutputStream());
    		print.println(output);
    	}
    	@Override
    	public void execute(Runnable command) {
    		new Thread(command).start();
     
    	}
     
    }

Similar Threads

  1. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 06:15 AM
  2. Multithreaded Proxy Server java
    By hivesh in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 2nd, 2013, 11:19 AM
  3. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  4. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 12:54 AM
  5. Replies: 0
    Last Post: January 22nd, 2011, 10:52 AM