Java服务器在需要时监听套接字

时间:2014-04-29 16:35:23

标签: java sockets client listen

public class Main {

    public static void main(String[] args) {
        Server server = new Server(9008);
    }
}

public class Server {

    private ServerSocket server;
    private Socket client;

    public Server(int port) {
        try {
            // Create out server with our desired port
            server = new ServerSocket(port);
            // Server started, let the user know
            System.out.println("Server started at port " + port + "...");
        } catch (IOException e) {
            // Unable to start server, print error
            System.out.println("Unable to start server on port " + port + "...");
        } 
        // Start our main server method
        runServer();
    }

    public void runServer() {
        while (true) {
            try {
                // Wait for new clients and accept them
                client = server.accept();
                // Let the user know - print
                System.out.println("New user connected - " + client.getLocalAddress().getHostAddress());
                // Start thread for our client
                Thread clientThread = new Thread(new ClientConnection(client));
                clientThread.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

所以在这一点上一切都很好,现在在我的客户端内线程问题开始了

    public class ClientConnection implements Runnable {

    private Socket socket;

    public ClientConnection(Socket client) {
        // Set client socket
        this.socket = client;
    }

    public void run() {
        try {
            // Read from our client input
            BufferedReader readClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = readClient.readLine()) != null) {
                System.out.println("Client says - " + readClient.readLine());
            }
        } catch(IOException e) {

        }
    }
}

有没有更好的方法来解决这个问题?

我的实际客户

public class Main {

    public static void main(String args[]) {

        try {
            Socket socket = new Socket("localhost", 9008);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            writer.write("Hello\n");
            writer.flush();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我会显示“客户端说 - null”

1 个答案:

答案 0 :(得分:1)

更新:在InputStream / Reader中读取的方式有点像

while ((myString = readClient.readLine()) != null) {
   System.out.println(myString);
}

这样,当连接关闭时,循环将退出。

此外,将try/catch移到循环外部,或进行一些错误控制。如果你得到一个例外,你不想再尝试再循环。

UPDATE2:如果我的评论不够清楚,请更新您的更新代码

        String line;
        while ((line = readClient.readLine()) != null) {
            System.out.println("Client says - " + line);
        }

每次迭代只需一次读取while,因此如果linenull,则循环可以退出(这意味着连接已关闭)。