创建一个对象,将该对象传递给另一个对象的构造函数,在对象上调用 wait(),然后在 Java 中调用 notification()

发布于 2024-12-10 00:17:20 字数 9226 浏览 0 评论 0原文

我正在尝试处理服务器同一端口上的多个连接。我通过实例化一个对象并将其传递到另一个实现 Runnable 的类的构造函数来实现此目的。然后,我在 Runnable 类中设置一个套接字,并在客户端连接到端口后对传递的对象调用 notification()。然后,这应该允许服务器重新启动其循环,在收到通知后创建 Runnable 类的另一个实例。但是,目前直到客户端关闭后才会到达 wait()。以下是我拥有的 3 个相关类:

服务器类:

   package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;

public class Server {

    public static void main(String[] args){
        HashMap<String, PortDummy> portDummies = new HashMap<String, PortDummy>();
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8000);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        for(;;){
            Object block = new Object();
            PortDummy dummy = new PortDummy(serverSocket, block, portDummies);
            System.out.println("Running dummy port...");
            dummy.start();
            try {
                synchronized(block){
                    System.out.println("Waiting...");
                    block.wait();
                    System.out.println("Block notified.");
                }
            } catch (InterruptedException e) {
                System.out.println("Can't be interrupted!");
                e.printStackTrace();
            }
        }
    }
}

PortDummy(可运行)类:

   package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class PortDummy extends Thread {

    private Object block;
    private HashMap<String, PortDummy> portDummies;
    private String clientName = null;
    ServerSocket serverSocket;
    BufferedReader socketIn;
    PrintWriter socketOut;

    public PortDummy(ServerSocket serverSocket, Object block, HashMap<String, PortDummy> portDummies){
        this.block = block;
        this.portDummies = portDummies;
        this.serverSocket = serverSocket;
    }

    @Override
    public void run() {
        try {
            System.out.println("Starting dummy port...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Connection made.");
            synchronized(block){
                System.out.print("Notifying...");
                block.notify();
                System.out.println("...done.");
            }

            socketIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            socketOut = new PrintWriter(clientSocket.getOutputStream(), true);

            String inContent;

            boolean loggedIn = false;
            while((inContent = socketIn.readLine()) != null){   
                socketOut.println("Server Echo: " + inContent);
                if(inContent.startsWith("/exit")){
                    if(loggedIn){
                        portDummies.remove(clientName);
                        System.out.println(clientName + " signed out. Removed from portDummies.");
                    }
                    else{
                        System.out.println("Closing...");
                    }
                }
                else if(inContent.startsWith("/register")){
                    System.out.println("/register accepted");
                    if(!loggedIn){
                        if(registerUser(inContent)){
                            System.out.println("Successfully registered.");
                            socketOut.println(clientName + " successfully registered.");
                            loggedIn = true;
                        }else{
                            socketOut.print("That user already exists.");
                        }
                    }
                    else{
                        socketOut.print("Already logged in.");
                    }
                }
                else if(inContent.startsWith("/tell")){
                    if(!loggedIn){
                        socketOut.println("You need to log in.");
                    }
                    else{
                        String[] parts = inContent.split("\\w");
                        String[] withoutCommand = new String[parts.length-1];
                        for(int i = 1; i<parts.length-1; i++){
                            withoutCommand[i] = parts[i];
                        }
                        String[] messageParts = new String[withoutCommand.length-1];
                        String message = "";
                        for(int j = 1; j<withoutCommand.length-1; j++){
                            message += withoutCommand[j] + " ";
                        }

                        String recipient = withoutCommand[0];
                        sendMessage(recipient, message);
                    }
                }
                else if(inContent.startsWith("/help")){
                    socketOut.print("/help ~~~~~~~ List all commands. \n " +
                            "/register <username> ~~~~~~~ Register a new user with 'username'. \n " +
                            "/tell <username> <message> ~~~~~~~ Send 'username' text 'message'. \n " +
                            "/exit ~~~~~~~ Log out.");
                }
            }

            System.out.println("Shutting down client connections...");
            socketOut.close();
            socketIn.close();
            clientSocket.close();
            serverSocket.close();

        } catch (IOException e) {
            System.out.println("IOException!");
            e.printStackTrace();
        }       
    }

    private boolean registerUser(String text){
        System.out.println("Registering user...");
        String user = text.substring(10);
        if((user != null) && !(portDummies.containsKey(user))){
            portDummies.put(user, this);
            clientName = user;
            System.out.println(user + " registered.");
            return true;
        }
            return false;
    }

    private void sendMessage(String username, String message){
        if(portDummies.containsKey(username)){
            PortDummy recip = portDummies.get(username);
            recip.getSocketOutput().println(clientName + ": " + message);
        }
        else{
            socketOut.write("User " + username + " doesn't exist.");
        }
    }

    public PrintWriter getSocketOutput(){
        return socketOut;
    }
}

客户端类:

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.io.IOException;

public class Client {

    protected String username;

    public static void main(String[] args){
        try{
            Socket serverSocket = new Socket("localhost", 8000);
            BufferedReader socketIn = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
            PrintWriter socketOut = new PrintWriter(serverSocket.getOutputStream(), true);

            Scanner keyboardInputScanner = new Scanner(System.in);
            String keyboardInput, serverInput;
            System.out.println("Welcome to Chris Volkernick's Server IM Client! \n" +
                    "Type '/register <username>' to register, '/list' to list connected users," +
                    "\n or '/tell <username> <message>' to send a user a message. '/help' lists these commands. (Type '/exit' to sign out.)");
            while((keyboardInput = keyboardInputScanner.nextLine()) != null){
                System.out.println("Input '" + keyboardInput + "' read on client side.");
                if(keyboardInput.equals("/exit")){
                    socketOut.println("/exit");
                    socketOut.close();
                    socketIn.close();
                    serverSocket.close();
                }else{
                    socketOut.println(keyboardInput);

                    while((serverInput = socketIn.readLine()) != null){
                        System.out.println(serverInput);
                    }
                }           
            }       
            keyboardInputScanner.close();

        }catch(IOException e){
            System.out.println("IOException!");
            e.printStackTrace();
        }       
    }
}

我在 wait() 和/或 notification() 方面做错了什么吗?

编辑:我还尝试将实现 Runnable 更改为扩展Thread,然后将服务器中的 .run() 更改为 .start() ,但这给了我这个错误:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at java.net.ServerSocket.<init>(ServerSocket.java:97)
    at server.PortDummy.run(PortDummy.java:28)

编辑2:它似乎正在按应有的方式工作现在就启动新线程而言。但是,我现在遇到了另一个问题:在任何给定客户端的客户端输入命令后,我无法输入其他命令。第一个命令可以正常工作(减去 /exit;还没有完全弄清楚它应该如何工作),只是之后无法执行任何操作。例如,我可以注册(登录),但之后就什么都没有了。我可以进入客户端的另一个实例并列出所有当前用户(有效),但同样,此后我无法输入其他命令。知道可能发生什么原因导致这种情况吗?

I'm trying to handle multiple connections on the same port of my server. I'm doing this by instantiating an Object and passing it into the constructor for another class, which implements Runnable. Then I set up a socket in the Runnable class and call notify() on the passed Object after a Client connects on the port. This should then allow the server to restart its loop, creating another instance of the Runnable class after being notified. However, currently the wait() isnt being reached until after the client is closed. Here are the 3 relevant classes I have:

Server class:

   package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;

public class Server {

    public static void main(String[] args){
        HashMap<String, PortDummy> portDummies = new HashMap<String, PortDummy>();
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8000);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        for(;;){
            Object block = new Object();
            PortDummy dummy = new PortDummy(serverSocket, block, portDummies);
            System.out.println("Running dummy port...");
            dummy.start();
            try {
                synchronized(block){
                    System.out.println("Waiting...");
                    block.wait();
                    System.out.println("Block notified.");
                }
            } catch (InterruptedException e) {
                System.out.println("Can't be interrupted!");
                e.printStackTrace();
            }
        }
    }
}

PortDummy (Runnable) class:

   package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class PortDummy extends Thread {

    private Object block;
    private HashMap<String, PortDummy> portDummies;
    private String clientName = null;
    ServerSocket serverSocket;
    BufferedReader socketIn;
    PrintWriter socketOut;

    public PortDummy(ServerSocket serverSocket, Object block, HashMap<String, PortDummy> portDummies){
        this.block = block;
        this.portDummies = portDummies;
        this.serverSocket = serverSocket;
    }

    @Override
    public void run() {
        try {
            System.out.println("Starting dummy port...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Connection made.");
            synchronized(block){
                System.out.print("Notifying...");
                block.notify();
                System.out.println("...done.");
            }

            socketIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            socketOut = new PrintWriter(clientSocket.getOutputStream(), true);

            String inContent;

            boolean loggedIn = false;
            while((inContent = socketIn.readLine()) != null){   
                socketOut.println("Server Echo: " + inContent);
                if(inContent.startsWith("/exit")){
                    if(loggedIn){
                        portDummies.remove(clientName);
                        System.out.println(clientName + " signed out. Removed from portDummies.");
                    }
                    else{
                        System.out.println("Closing...");
                    }
                }
                else if(inContent.startsWith("/register")){
                    System.out.println("/register accepted");
                    if(!loggedIn){
                        if(registerUser(inContent)){
                            System.out.println("Successfully registered.");
                            socketOut.println(clientName + " successfully registered.");
                            loggedIn = true;
                        }else{
                            socketOut.print("That user already exists.");
                        }
                    }
                    else{
                        socketOut.print("Already logged in.");
                    }
                }
                else if(inContent.startsWith("/tell")){
                    if(!loggedIn){
                        socketOut.println("You need to log in.");
                    }
                    else{
                        String[] parts = inContent.split("\\w");
                        String[] withoutCommand = new String[parts.length-1];
                        for(int i = 1; i<parts.length-1; i++){
                            withoutCommand[i] = parts[i];
                        }
                        String[] messageParts = new String[withoutCommand.length-1];
                        String message = "";
                        for(int j = 1; j<withoutCommand.length-1; j++){
                            message += withoutCommand[j] + " ";
                        }

                        String recipient = withoutCommand[0];
                        sendMessage(recipient, message);
                    }
                }
                else if(inContent.startsWith("/help")){
                    socketOut.print("/help ~~~~~~~ List all commands. \n " +
                            "/register <username> ~~~~~~~ Register a new user with 'username'. \n " +
                            "/tell <username> <message> ~~~~~~~ Send 'username' text 'message'. \n " +
                            "/exit ~~~~~~~ Log out.");
                }
            }

            System.out.println("Shutting down client connections...");
            socketOut.close();
            socketIn.close();
            clientSocket.close();
            serverSocket.close();

        } catch (IOException e) {
            System.out.println("IOException!");
            e.printStackTrace();
        }       
    }

    private boolean registerUser(String text){
        System.out.println("Registering user...");
        String user = text.substring(10);
        if((user != null) && !(portDummies.containsKey(user))){
            portDummies.put(user, this);
            clientName = user;
            System.out.println(user + " registered.");
            return true;
        }
            return false;
    }

    private void sendMessage(String username, String message){
        if(portDummies.containsKey(username)){
            PortDummy recip = portDummies.get(username);
            recip.getSocketOutput().println(clientName + ": " + message);
        }
        else{
            socketOut.write("User " + username + " doesn't exist.");
        }
    }

    public PrintWriter getSocketOutput(){
        return socketOut;
    }
}

Client class:

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.io.IOException;

public class Client {

    protected String username;

    public static void main(String[] args){
        try{
            Socket serverSocket = new Socket("localhost", 8000);
            BufferedReader socketIn = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
            PrintWriter socketOut = new PrintWriter(serverSocket.getOutputStream(), true);

            Scanner keyboardInputScanner = new Scanner(System.in);
            String keyboardInput, serverInput;
            System.out.println("Welcome to Chris Volkernick's Server IM Client! \n" +
                    "Type '/register <username>' to register, '/list' to list connected users," +
                    "\n or '/tell <username> <message>' to send a user a message. '/help' lists these commands. (Type '/exit' to sign out.)");
            while((keyboardInput = keyboardInputScanner.nextLine()) != null){
                System.out.println("Input '" + keyboardInput + "' read on client side.");
                if(keyboardInput.equals("/exit")){
                    socketOut.println("/exit");
                    socketOut.close();
                    socketIn.close();
                    serverSocket.close();
                }else{
                    socketOut.println(keyboardInput);

                    while((serverInput = socketIn.readLine()) != null){
                        System.out.println(serverInput);
                    }
                }           
            }       
            keyboardInputScanner.close();

        }catch(IOException e){
            System.out.println("IOException!");
            e.printStackTrace();
        }       
    }
}

Am I doing something wrong with the wait() and/or notify()?

EDIT: I also tried changing the implements Runnable to extends Thread then changing the .run() in the server to .start(), but that gives me this error:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at java.net.ServerSocket.<init>(ServerSocket.java:97)
    at server.PortDummy.run(PortDummy.java:28)

EDIT 2: It seems to be working the way it should now in terms of starting new threads. However, I'm now running into another problem: After I enter a command on the client side of any given client, I can't enter additional commands. The first command will work fine (minus /exit; haven't quite figured out how that should work yet), just can't do anything after that. For example, I can register (sign in) but after that nothing else. I can go into another instance of Client and list all the current users (works), but again, after that I cannot enter additional commands. Any idea what may be happening to cause this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-12-17 00:17:20

问题是您的子线程正在尝试侦听端口 8000,但父线程已经在这样做。您需要做的是通过接受来自原始套接字的连接,然后将其交给子线程。我不太确定如何在 Java 中执行此操作,但我怀疑这只是......

将其放入您的主线程中:

ServerSocket serverSocket = new ServerSocket(8000);
Socket clientSocket = serverSocket.accept();

然后一旦您得到它,将 clientSocket 传递给您的 Thread< /代码>。

这样,只有一个套接字侦听端口 8000,但您可以让子线程处理每个连接。

The problem is that your child threads are trying to listen on port 8000, but the parent thread is already doing that. What you need to do is pass accept a connection from the original socket and then give it to the child thread. I'm not exactly sure how to do this in Java, but I suspect it's just..

Put this in your main thread:

ServerSocket serverSocket = new ServerSocket(8000);
Socket clientSocket = serverSocket.accept();

And then once you get that, pass clientSocket to your Thread.

That way there's only one socket listening on port 8000, but you can make child threads handle each connection.

紫瑟鸿黎 2024-12-17 00:17:20

使用等待和通知时,请注意通知不会排队,因此如果通知发生在等待发生之前,您将永远不会退出等待。因此,您永远不应该执行裸等待,即应该始终测试某些条件来确定是否应该等待。

sychronized(block) {
   while (!available) {
       block.wait();
   }
}

synchronized(block) {
    available = true;
    block.notifyAll();
}

等等

When using wait and notify, realize that notifies aren't queued, so if the notify happens before the wait occurs, you will never exit the wait. Therefore you should never perform naked waits, that is there should always be some condition you test to see if you should wait.

sychronized(block) {
   while (!available) {
       block.wait();
   }
}

and

synchronized(block) {
    available = true;
    block.notifyAll();
}

etc

峩卟喜欢 2024-12-17 00:17:20
package so_7775790;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;


/**
 * barebones server -- ctl-C to kill it ;)
 */
public class Server implements Runnable {

    final static Logger log = Logger.getLogger(Server.class.getSimpleName());


    public static void main(String[] args) {
        final int port = 8000;
        final String tname = "my-server-thread";
        final Server server = new Server(port);

        try {
            Thread tserver = new Thread(server, tname);
            tserver.start();
            tserver.join();
        } catch (Exception e) {
            log.severe(e.getMessage());
        }
    }


    // -------------------------------------------------
    // server
    // -------------------------------------------------

    final int port;
    public Server(int port) {
        this.port = port;
    }
    public void run() {
        try{
            final ServerSocket srvsocket = new ServerSocket(port);
            log.info(String.format("Server started @ %s\n", srvsocket));

            while(!Thread.currentThread().isInterrupted()){
                Socket newclient = srvsocket.accept();

                // spawn thread and hand off new client to handler
                new Thread(new ClientHandler(newclient)).start();
            }
        }
        catch (Exception e) {
            log.severe(e.getMessage());
        }
        log.info("server stopped");
    }

    // -------------------------------------------------
    // client handler
    // -------------------------------------------------
    static class ClientHandler implements Runnable {
        final Socket socket;
        public ClientHandler(final Socket socket) {
            assert socket != null : "serverthread is null";
            this.socket = socket;
        }
        @SuppressWarnings("unused")
        @Override final
        public void run() {
            log.info(String.format("new client @ %s\n", socket.getRemoteSocketAddress()));
            try {
                final InputStream in = socket.getInputStream();
                final OutputStream out = socket.getOutputStream();

                // NOTE: this is just a stub busy loop!
                for(;;) {
                    /* your protocol impl here .. */
                }

            } catch (Exception e) {
                log.severe(e.getMessage());
            }
            finally {
                try {
                    socket.close();
                }
                catch (Exception e) {
                    log.severe(e.getMessage());
                }
            }
        }
    }
}
package so_7775790;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;


/**
 * barebones server -- ctl-C to kill it ;)
 */
public class Server implements Runnable {

    final static Logger log = Logger.getLogger(Server.class.getSimpleName());


    public static void main(String[] args) {
        final int port = 8000;
        final String tname = "my-server-thread";
        final Server server = new Server(port);

        try {
            Thread tserver = new Thread(server, tname);
            tserver.start();
            tserver.join();
        } catch (Exception e) {
            log.severe(e.getMessage());
        }
    }


    // -------------------------------------------------
    // server
    // -------------------------------------------------

    final int port;
    public Server(int port) {
        this.port = port;
    }
    public void run() {
        try{
            final ServerSocket srvsocket = new ServerSocket(port);
            log.info(String.format("Server started @ %s\n", srvsocket));

            while(!Thread.currentThread().isInterrupted()){
                Socket newclient = srvsocket.accept();

                // spawn thread and hand off new client to handler
                new Thread(new ClientHandler(newclient)).start();
            }
        }
        catch (Exception e) {
            log.severe(e.getMessage());
        }
        log.info("server stopped");
    }

    // -------------------------------------------------
    // client handler
    // -------------------------------------------------
    static class ClientHandler implements Runnable {
        final Socket socket;
        public ClientHandler(final Socket socket) {
            assert socket != null : "serverthread is null";
            this.socket = socket;
        }
        @SuppressWarnings("unused")
        @Override final
        public void run() {
            log.info(String.format("new client @ %s\n", socket.getRemoteSocketAddress()));
            try {
                final InputStream in = socket.getInputStream();
                final OutputStream out = socket.getOutputStream();

                // NOTE: this is just a stub busy loop!
                for(;;) {
                    /* your protocol impl here .. */
                }

            } catch (Exception e) {
                log.severe(e.getMessage());
            }
            finally {
                try {
                    socket.close();
                }
                catch (Exception e) {
                    log.severe(e.getMessage());
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文