簡體   English   中英

Java簡單服務器客戶端

[英]Java simple server client

我正在用Java開發一個簡單的線程服務器客戶端消息傳遞應用程序。 我的問題是,當客戶端打開時,服務器會收到消息,但僅將消息回顯給發送消息的客戶端,而不回顯其他客戶端。 我如何將其發送到連接到服務器的所有客戶端。

服務器代碼

import java.io.*;
import java.net.*;
public class ThreadedSever
{
  private static final int port=5000;
  //Threaded Server
  public ThreadedSever(){
     System.out.println("A multi-threaded server has started...");
     ServerSocket serverSocket = null; 
     try {
         serverSocket = new ServerSocket(port);
     }catch(Exception e){
         System.out.println("Could not create a server socket: " + e);
     }
     //Chat with the client until breaks connection or says bye
     try{
         while(true){
             System.out.println("Waiting for client communication");
             Socket currentSocket = serverSocket.accept();
             //Create a new thread to deal with this connection
             new ServerThread(currentSocket);
         }

     }catch(Exception e){
         System.out.println("Fatal Server Error!");
     }
  }
   //Inner class to handle individual commincation
   private class ServerThread extends Thread{
   private Socket sock;
   private DataInputStream in = null;
   private DataOutputStream out = null;

   public ServerThread(Socket sock){
       try{
           this.sock = sock;
           in = new DataInputStream(this.sock.getInputStream());
           out = new DataOutputStream(this.sock.getOutputStream());
           System.out.print("New client connection established");
           out.writeUTF("Type bye to exit, otherwise, prepare to be echoed");
           start();
       }catch(Exception e){
           System.out.println("Oops");
       }
   }
   public void run(){
     try{
        String what = new String("");
        while(!what.toLowerCase().equals("bye")){
            what = in.readUTF();
            System.out.println("Client told me: " + what);
            out.writeUTF(what);
        }
     }catch(Exception e){
         System.out.println("Connection to current client has been broken");
     }
     finally{
       try{
           sock.close();
       }catch(Exception e){
           System.out.println("Error socket could not be closed");
       }
     }
   }
  }
  public static void main(){
    new ThreadedSever();
  }
}

客戶代碼

import java.io.*;
import java.net.*;
public class simpleClient
{
    private static final int port= 5000;
    private static String server = "localhost";
    private static Socket socket = null;
    private static DataInputStream input = null;
    private static DataOutputStream output = null;
    private static InputStreamReader inReader = null;
    private static BufferedReader stdin = null;

    public static void main(){
        try{
            socket = new Socket(server, port);
        }catch(UnknownHostException e){
            System.err.println("Unknow IP address for server");
            System.exit(-1);
        }
        catch(IOException e){
            System.err.println("No server found at specified port");
            System.exit(-1);
        }
        catch(Exception e){
            System.err.println("Something happened!");
            System.exit(-1);
        }
        try{
            input = new DataInputStream(socket.getInputStream());
            output = new DataOutputStream(socket.getOutputStream());
            inReader = new InputStreamReader(System.in);
            stdin = new BufferedReader(inReader);
            String what = new String("");
            String response;
            while(!what.toLowerCase().equals("bye")){
                // Expect something from the server and output it when it arrives
                response = input.readUTF();
                System.out.println("Server said  \"" + response + "\"");
                //Read a line from the user and send it to the server
                what = stdin.readLine();
                output.writeUTF(what);
            }
        }
        catch(IOException e){
            System.err.println("Broken connection with server");
            System.exit(-1);
        }

    }
}

您可以定義一個方法echoMessage每個ServerThread 每次實例化ServerThread時,都ServerThread實例添加到ThreadedSeverlist中。 而且, ServerThread ServerThread接收到數據,還應使ServerThread成為觀察者 ,然后該觀察者方法應在ServerThread列表中的每個元素上調用echoMessage。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM