Thread: Server and client help

Results 1 to 4 of 4
  1. #1 Server and client help 
    Registered Member
    Join Date
    Feb 2008
    Posts
    151
    Thanks given
    7
    Thanks received
    4
    Rep Power
    33
    I've been learning network IO recently, and made a server and client pair. The server can accept multiple clients, but when I write to the server from a client it prints it back to only the client that sent it.

    Code:
    import java.io.IOException;
    import java.net.ServerSocket;
    
    public class Server {
    	
    	public static void main(String[] args) throws IOException {
    		ServerSocket s = null;
    		boolean listening = true;
    		try {
    			s = new ServerSocket(5555);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    		while(listening)
    			new ServerThread(s.accept()).start();
    		
    	}
    
    }
    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class ServerThread extends Thread {
    
    	private Socket sock = null;
    
    	public ServerThread(Socket socket) {
    		super("Server Thread");
    		this.sock = socket;
    	}
    
    	public void run() {
    		
    		PrintWriter out = null;
    		BufferedReader in = null;
    
    		try {
    			out = new PrintWriter(sock.getOutputStream(), true);
    			in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    			
    			String input;
    			
    			while((input = in.readLine()) != null) {
    				System.out.println(input);
    				out.println(input);
    			}
    			
    			in.close();
    			out.close();
    			sock.close();
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    }
    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client {
    
    	public static void main(String[] args) throws IOException {
    		Socket sock = null;
    		PrintWriter out = null;
    		BufferedReader in = null;
    
    		try {
    			sock = new Socket("127.0.0.1", 5555);
    			out = new PrintWriter(sock.getOutputStream(), true);
    			in = new BufferedReader(
    					new InputStreamReader(sock.getInputStream()));
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		BufferedReader stdIn = new BufferedReader(new InputStreamReader(
    				System.in));
    		String userInput;
    		
    		
    		while ((userInput = stdIn.readLine()) != null) {	
    			out.println(userInput);
    			System.out.println(in.readLine());
    		}
    
    		out.close();
    		in.close();
    		sock.close();
    	}
    
    }
    Quote Originally Posted by HardStyle View Post
    Na all the nub owners are like Z0mG!!! 200 playerz!!! my server r da beast!
    Funny
    Reply With Quote  
     

  2. #2  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,246
    Thanks given
    3,283
    Thanks received
    2,874
    Discord
    View profile
    Rep Power
    5000
    Ok, op. Now that you've posted your whole application and told us what it does, could you please tell us what the damned problem is?
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Feb 2008
    Posts
    151
    Thanks given
    7
    Thanks received
    4
    Rep Power
    33
    Quote Originally Posted by Mister Maggot View Post
    Ok, op. Now that you've posted your whole application and told us what it does, could you please tell us what the damned problem is?
    I'm sorry, I thought you would have been able to tell that I wanted it to send the message to all the clients instead of just the one that typed. I apologize.
    Quote Originally Posted by HardStyle View Post
    Na all the nub owners are like Z0mG!!! 200 playerz!!! my server r da beast!
    Funny
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Feb 2011
    Posts
    117
    Thanks given
    0
    Thanks received
    76
    Rep Power
    158
    iterating through an array of clients would be the easiest.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2010, 11:40 PM
  2. Replies: 3
    Last Post: 02-14-2010, 07:15 PM
  3. Replies: 38
    Last Post: 10-23-2007, 12:58 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
  •