Java套接字处理多个客户端

时间:2015-12-11 19:02:33

标签: java sockets

我有一个服务器和一个客户端客户端已成功连接到服务器,即使我启动另一个客户端它成功连接,这是我的问题客户端可以发送数据到服务器(它基本上只是一个字符串)服务器得到两个客户端的数据,但客户端只返回自己的数据,我希望两个客户端从服务器获取相同的数据。

服务器:

public class Server extends Application {

@Override
public void start(Stage primaryStage) {

}

static ServerSocket serverSocket;
static Socket socket;
static DataOutputStream out;
static DataInputStream in;
static Users[] user = new Users[10];



public static void main(String[] args) throws IOException {
    System.out.println("Starting server...");
    serverSocket = new ServerSocket(7777);
    System.out.println("Server started");
    while(true){
    socket = serverSocket.accept();
    for(int i = 0;i < 10; i++){
    System.out.println("Connection from:" + socket.getInetAddress());
    out = new DataOutputStream(socket.getOutputStream());
    in = new DataInputStream(socket.getInputStream());

    if(user[i] == null){
        user[i] = new Users(out,in,user);
    Thread thread = new Thread(user[i]);
    thread.start();
    break;
            }  
}
}
}

private static class Users implements Runnable{

DataOutputStream out;
DataInputStream in;
Users[] user = new Users[10];
String name;

public Users(DataOutputStream out,DataInputStream in,Users[] user){
    this.out = out;
    this.in = in;
    this.user = user;

}



@Override
public void run() {
    while(true){
        try {
            String recievingData = in.readUTF();
            System.out.println(recievingData);
            out.writeUTF(recievingData);

        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}}}

客户(两者都相同):

public class ServerClient {

static Socket socket;
static DataInputStream in;
static DataOutputStream out;

public ServerClient() throws IOException{
    System.out.println("Connecting");
    socket = new Socket("localhost",7777);
    System.out.println("Connecting succesful");
    in = new DataInputStream(socket.getInputStream());
    out = new DataOutputStream(socket.getOutputStream());
    System.out.println("Recieving");

    Input input = new Input(in);
    Thread thread = new Thread(input);
    thread.start(); 



}

public static void UploadPos(){
    try {
        out.writeUTF("TEST");

    } catch (IOException ex) {
        Logger.getLogger(ServerClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}}


class Input implements Runnable{

DataOutputStream out;
DataInputStream in;

public Input(DataInputStream in){
    this.in = in;
}

@Override
public void run() {
   while(true){
       try {

           String data = in.readUTF();
           System.out.println(data);

       } catch (IOException ex) {
           Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, ex);
       }

   }

}}

1 个答案:

答案 0 :(得分:0)

创建输出流列表。

List<DataOutputStream> clientOuts = new ArrayList<>();

out = new DataOutputStream(socket.getOutputStream());

添加

clientOuts.add(out);

然后替换

out.writeUTF(recievingData);

for(DataOutputStream cout :clientOutputs) {
     cout.writeUTF(recievingData);
}