Results 1 to 5 of 5
  1. #1
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default New to multi-threading and server coding, not sure if correct.

    Hey i'm back with more questions woohoo. In about 2 weeks my project(my choice) is due so I've been learning how servers work kind of on the go. In the end I hope to have a server running on my machine, and other machines on the network can run the client to connect. Thinking maybe a little grid layout where little figures shoot fireballs at each other or something, I dunno haha. But that's for later, at the moment I have a multi-threaded server that echos back strings(toUpperCase()) to multi-threaded clients. It seems to be working but it was also hastily put together. Could you please take a look at my novice work and point out things to look out for, suggestions, or anything really? Thanks.

    Server:
    Java Code:
    public class Server{
    	private ServerSocket serverSocket;
    	private Socket socket;
    
    	//Creates a new Server
    	public Server(){
    		// Attempt to create server socket
    		try{
    			serverSocket = new ServerSocket(55555);
    		}
    		catch(IOException e){
    			System.out.println("Could not open server socket.");
    			return;
    		}
    		System.out.println("Socket " + serverSocket + " created.");
    		connect();
    	}
    	
    	private class Outport extends Thread{
    		private Socket socket;
    		private BufferedReader in;
    		private PrintWriter out;
    		
    		public Outport(Socket newSocket){
    			socket = newSocket;
    			start();
    		}
    		public void run(){
    			try{
    				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				out = new PrintWriter(socket.getOutputStream(), true);
    				while(true){
    					String str = in.readLine();
    					if(str.equals("close") || str.equals("Close")){
    						in.close();
    						out.close();
    						socket.close();
    					}
    					out.println(str.toUpperCase());
    				}					
    			}
    			catch(IOException e){
    				System.out.println("Client " + socket + " has disconnected.");
    			}
    		}
    	}
    	
    	//Starts the client accepting process.
    	public void connect(){
    		System.out.println("Server has been started.");
    
    		while(true){
    			try{
    				socket = serverSocket.accept();
    				if(socket != null)
    					System.out.println("Client " + socket + " has connected.");
    					new Outport(socket);
    			}catch(IOException e){
    			}			
    		}
    	}
    	
    	public static void main(String[] args){
    		Server server = new Server();
    	}
    }
    Client:
    Java Code:
    public class Client{
    	private Socket socket;
    	private Scanner scan;
    	private BufferedReader in;
    	private PrintWriter out;
    	
    	public Client() throws IOException{
    		scan = new Scanner(System.in);
    		connect();
    	}
    	
    	//Handles all incoming data from this user.
    	private class Inport extends Thread{
    		public void run(){
    			try{
    				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				out = new PrintWriter(socket.getOutputStream(), true);
    				while(true){
    					System.out.println("Please enter line: ");
    					String ask = scan.nextLine();
    					
    					out.println(ask);
    					if(ask.equals("close") || ask.equals("Close"))
    						close();
    					System.out.println(in.readLine());
    				}
    			}
    			catch(IOException e){
    				System.out.println("Could not get stream from " + toString());
    				return;
    			}
    		}
    	}
    	
    	public void connect() throws UnknownHostException, IOException{
    		socket = new Socket("10.61.19.226", 55555);
    		System.out.println(socket + " has connected to server.");
    		new Inport().start();
    	}
    
    
    	public void close(){
    		try{
    			socket.close();
    			in.close();
    			out.close();
    		}
    		catch(IOException e){
    			System.out.println("Could not close " + socket + ".");
    		}
    	}
    	
    	public static void main(String[] args) throws IOException{
    		Client client = new Client();
    	}
    }
    Last edited by Bestsanchez; 05-22-2012 at 06:34 AM. Reason: Updated code

  2. #2
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to multi-threading and server coding, not sure if correct.

    Ok...

    - generally anything that can hold the main thread of a program up should be put into it's own thread. In this case, putting the accept() call into it's own thread might be a good idea in case you later want the server to do anything else other than just accept.

    - the Outport thread is simply looking for a message, sending it again then quitting. It should wait for messages from client or server then send them to the other. I'm guessing you'll want to do this more than once so you'll need to loop it as well.

    - You'll want to add a loop on the client end as well rather than connecting, sending a message, disconnecting, connecting, sending a message, disconnecting, etc,etc

  3. #3
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: New to multi-threading and server coding, not sure if correct.

    That.................. was actually really helpful thanks haha. Is the nesting of classes frowned upon at all? And this might be silly, but is it possible for the client socket to search for the local server socket? Right now it works, but I have to specify the IP of the computer the server is running on (in the client code) when I run the client on another machine, and I want it to still work if I happen to start the server on a different one and then run the same client... If that makes sense

  4. #4
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to multi-threading and server coding, not sure if correct.

    The nesting of classes isn't really frowned upon, if a class is only ever needed by one other class nesting is the way to go most times. If you want to be able to put the server on any machine then you can't really search for it without a whole load of unecessary hassle. If it's just local then the IP address 127.0.0.1 will of course always work. Generally the IP of the server would be put into a properties file or given as an option in the client interface.

    I wrote something similar a while back in order to get used to network communication, just a simple chat client

    https://sourceforge.net/projects/roxcom/

  5. #5
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: New to multi-threading and server coding, not sure if correct.

    Well that's actually exactly what I just made haha. It's on the console, but it still works great locally and over the internet. Just need to make a gui for it now!

Similar Threads

  1. Multi-Threading
    By TonyDaniels in forum New To Java
    Replies: 4
    Last Post: 03-09-2012, 07:16 PM
  2. Multi-Threading
    By TonyDaniels in forum Advanced Java
    Replies: 1
    Last Post: 03-09-2012, 06:52 PM
  3. Multi-threading issue
    By StevenTNorris in forum Advanced Java
    Replies: 2
    Last Post: 10-19-2011, 10:08 PM
  4. Need Help! Multi-Threading question!
    By pinkette in forum New To Java
    Replies: 8
    Last Post: 01-13-2011, 08:08 PM
  5. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •