首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java简单服务器客户端

Java简单服务器客户端
EN

Stack Overflow用户
提问于 2017-11-06 04:12:35
回答 1查看 126关注 0票数 0

我正在用java制作一个简单的线程服务器客户端消息传递应用程序。我的问题是,当客户机打开时,服务器获取消息,但将消息回显给发送消息的客户端,而不是其他客户端。如何将其发送到连接到服务器的所有客户端。

服务器代码

代码语言:javascript
复制
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();
  }
}

客户端代码

代码语言:javascript
复制
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);
        }

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-06 04:26:44

您可以在每个echoMessage中定义一个方法ServerThread。每次实例化ServerThread时,都将该实例添加到ThreadedSever中的list中。如果ServerThread接收到数据,那么观察者方法应该对ServerThread列表中的每个元素调用echoMessage。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47129626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档