无法在套接字列表的循环中获取套接字的DataOutputStream

时间:2016-06-19 17:01:59

标签: java sockets dataoutputstream

我正在尝试使用Java创建客户端/服务器程序,其中客户端将简单的字符串发送到服务器,并且连接到服务器的每个客户端都获取此字符串。 所以我为服务器创建了2个服务器类,为每个客户端创建新线程并监听它们。还有一个客户端套接字列表。我想用它向每个客户端发送一个客户端发送的字符串。我使用foreach循环。但是我只为发送String到服务器的客户端获得了Dataoutputstream:

public void run(){
    try{
        while(true){
            String data = in.readUTF();
            for(Socket soc : GlobalQdaServer.allClients){
                DataOutputStream sOut = new DataOutputStream(soc.getOutputStream());
                sOut.writeUTF(data + ". Total clients number in list: " + GlobalQdaServer.allClients.size());
            }                
        }
    }
    catch(EOFException ex){
        System.out.println(ex.getMessage());
    } 
    catch(IOException ex){
        System.out.println(ex.getMessage());
    } 
}

因此,只有这个客户端收到String(它自己发送)。其他什么也没收。 这些是完整的课程:

    import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.*;
import java.util.ArrayList;
import java.util.List;


public class GlobalQdaServer {
    public static List<Socket> allClients = new ArrayList<Socket>();

    public static void main(String[] args) {
        try{
            int serverPort = 7896;
            ServerSocket listenSocket = new ServerSocket(serverPort);
            while(true){
                Socket clientSocket = listenSocket.accept();
                allClients.add(clientSocket);
                Connection c = new Connection(clientSocket);
            }
        }
        catch(IOException ex){
            System.out.println("Server Socket creating failiure");            
        }
    }

}

class Connection extends Thread{
    DataInputStream in;
    DataOutputStream out;
    Socket clientSocket;
    public Connection(Socket aClientSocket){
        try{
            clientSocket = aClientSocket;
            in = new DataInputStream(clientSocket.getInputStream());
            out = new DataOutputStream(clientSocket.getOutputStream());
            this.start();
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }        
    }

    public void run(){
        try{
            while(true){
                String data = in.readUTF();
                for(Socket soc : GlobalQdaServer.allClients){
                    DataOutputStream sOut = new DataOutputStream(soc.getOutputStream());
                    sOut.writeUTF(data + ". Total clients number in list: " + GlobalQdaServer.allClients.size());
                }                
            }
        }
        catch(EOFException ex){
            System.out.println(ex.getMessage());
        } 
        catch(IOException ex){
            System.out.println(ex.getMessage());
        } 
    }
}


    import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;


public class GlobalQdaClient {

    public static void main(String[] args) throws IOException {
        int serverPort = 7896;
        Socket s = new Socket("localhost", serverPort);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        while(!input.equals("end")){
            input = reader.readLine();
            try{
                DataInputStream in = new DataInputStream(s.getInputStream());
                DataOutputStream out = new DataOutputStream(s.getOutputStream());
                out.writeUTF(input);
                String data = in.readUTF();
                System.out.println("Recieved this: " + data);            
            }
            catch(UnknownHostException ex){
                System.out.println(ex.getMessage());
            }
            catch(EOFException ex){
                System.out.println(ex.getMessage());
            }
            catch(IOException ex){
                System.out.println(ex.getMessage());
            }
        }
    }    
}

请帮助找到问题的过程。我是套接字的新手。谢谢

1 个答案:

答案 0 :(得分:1)

您的计划实际上运作正常。

唯一的问题是你用连续读取来阻止客户端。

因此,您实际上会看到您在阅读后解除阻止后在其他客户端上发送的消息。

尝试以下方法:

  • 运行服务器
  • 运行Client1
  • 运行Client2
  • 从Client1发送消息(请注意,Client2现在因输入而被阻止)
  • 从Client2发送消息

现在,您将看到之前从client2上发送的消息。

但是如果你需要两个客户端一旦收到消息就需要创建另一个线程来连续获取更新。

我已经修改了你的客户端。

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class GlobalQdaClient extends Thread {
        int serverPort = 7896;
        BufferedReader reader ;
        static String input = "";
        Socket s;
        DataInputStream in ;
        DataOutputStream out ;
public GlobalQdaClient() {
        try {
            reader  = new BufferedReader(new InputStreamReader(System.in));
             s = new Socket("localhost", serverPort);
             in = new DataInputStream(s.getInputStream());
             out = new DataOutputStream(s.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(GlobalQdaClient.class.getName()).log(Level.SEVERE, null, ex);
        }
}

public static void main(String[] args) throws IOException {
    GlobalQdaClient client = new GlobalQdaClient();
    client.start();
    while(!client.input.equals("end")){
        client.input = client.reader.readLine();
        try{
            client.out.writeUTF(client.input);
            if(client.in.available()>0){
            String data = client.in.readUTF();
            System.out.println("Recieved this: " + data);            
            }
        }
        catch(UnknownHostException ex){
            System.out.println(ex.getMessage());
        }
        catch(EOFException ex){
            System.out.println(ex.getMessage());
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
    }
}    

@Override
public void run(){

    try {
        while(true){
            String data = in.readUTF();
            System.out.println("Recieved this: " + data);            
            }    
    } catch (IOException ex) {
            Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}