Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使Java服务器从变量而不是控制台输入中读取?_Java_Sockets_Server - Fatal编程技术网

如何使Java服务器从变量而不是控制台输入中读取?

如何使Java服务器从变量而不是控制台输入中读取?,java,sockets,server,Java,Sockets,Server,我正在创建一个简单的套接字程序,可以读取用户输入并将其存储在变量中。它还允许向服务器发送消息 客户端输入其详细信息(名称等),然后输入连接详细信息。我已经创建了发送这些变量的方法,但它们不能正常工作 下面的预期结果:将playerName从客户端类存储到服务器类中的playerName。 实际结果:连接后的第一个输入存储到服务器的playerName变量中 //服务器客户端类 public static String playerName = "Client"; public static

我正在创建一个简单的套接字程序,可以读取用户输入并将其存储在变量中。它还允许向服务器发送消息

客户端输入其详细信息(名称等),然后输入连接详细信息。我已经创建了发送这些变量的方法,但它们不能正常工作

下面的预期结果:将
playerName
从客户端类存储到服务器类中的
playerName
。 实际结果:连接后的第一个输入存储到服务器的
playerName
变量中

//服务器客户端类

public static String playerName = "Client";


public static void playerDetails() {
        Scanner playerInfo = new Scanner(System.in);

        System.out.println("Welcome! Please Enter Your Player Name:");
        playerName = playerInfo.nextLine();
}

public void sendNameToServer(String pName) throws IOException {
        if (this.clientSocket == null || this.output == null)
            throw new SocketException("Socket does not exist");
        this.output.writeObject(playerName);
    }

    public void sendMessageToServer(String msg) throws IOException {
        if (this.clientSocket == null || this.output == null)
            throw new SocketException("socket does not exist");

        this.output.writeObject(msg);
    }


public void runClient() {
        try {
            BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
            String message = null;

            while (true) {
                message = fromConsole.readLine();
                handleUserInput(message);
                if(message.equals("over"))

                    break;
            }



// server client Manager Class

try {
                this.out = new ObjectOutputStream(this.clientSocket.getOutputStream());
                this.in = new ObjectInputStream(this.clientSocket.getInputStream());            
            }



            String pName = null;
            String msg = "";

            //Name

            pName = (String)this.in.readObject();
            this.server.sendNameToServer(pName, this);



            msg = (String)this.in.readObject();
            this.server.handleMessagesFromClient(msg, this);


//Abstract Methods
public abstract class ServerAbstractComponents {
    public abstract void handleMessagesFromClient(String msg, ServerClientManager clientmgr);
    public abstract void sendMessageToClient(String msg, ServerClientManager clientmgr);
    public abstract void sendNameToServer(String pName, ServerClientManager clientmgr);

//Server Class
public  String playerName;
        public synchronized void sendNameToServer(String pName, ServerClientManager client) {
            playerName = pName;
            }


public synchronized void handleMessagesFromClient(String msg, ServerClientManager client) {

            // format the client message before displaying in server's terminal output. 
             String formattedMessage = String.format("[client %d] : %s", client.getClientID(), msg); 

                if(msg.equals(new String("test"))) {
                    System.out.println("your name is:" + playerName);
                } else 

            display(formattedMessage);
}


以下是我是如何做到这一点的

//Writes player Name as an object to the output Stream
        System.out.println("Welcome, Enter your name :");
        try {
            Scanner playerInfo = new Scanner(System.in);
            playerName2 = playerInfo.nextLine();
            this.output.writeObject(playerName2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } System.out.println("Welcome to The Game! " + playerName2);
//ServerClientManager类

            //Name2
            try {
            pName2 = (String)this.in.readObject();
        } catch (ClassNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        this.server.sendNameToServer2(pName2, this);
//抽象类

public abstract void sendNameToServer(String pName2, ServerClientManager clientmgr);
//服务器类

public String playerName2;
        public synchronized void sendNameToServer2(String pName2, ServerClientManager client) {
            client.playerName2 = pName2;
        }

我是这样做的

//Writes player Name as an object to the output Stream
        System.out.println("Welcome, Enter your name :");
        try {
            Scanner playerInfo = new Scanner(System.in);
            playerName2 = playerInfo.nextLine();
            this.output.writeObject(playerName2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } System.out.println("Welcome to The Game! " + playerName2);
//ServerClientManager类

            //Name2
            try {
            pName2 = (String)this.in.readObject();
        } catch (ClassNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        this.server.sendNameToServer2(pName2, this);
//抽象类

public abstract void sendNameToServer(String pName2, ServerClientManager clientmgr);
//服务器类

public String playerName2;
        public synchronized void sendNameToServer2(String pName2, ServerClientManager client) {
            client.playerName2 = pName2;
        }

您发布的代码太多,无法浏览-请查看并相应地编辑您的问题。然而,我猜问题是
playerName
static
。请尝试从
playerName
(方法和变量)中删除
static
关键字。您好,我已尝试删除static关键字,但实例化该类时遇到问题,我已尝试创建该类的实例以使程序运行<代码>服务器客户端myClient=新服务器客户端();myClient.playerDetails()但是我现在无法在main中引用非静态方法。我没有看到任何ServerSocket或Socket的实例。您发布了太多代码,无法浏览-请相应地查看和编辑您的问题。然而,我猜问题是
playerName
static
。请尝试从
playerName
(方法和变量)中删除
static
关键字。您好,我已尝试删除static关键字,但实例化该类时遇到问题,我已尝试创建该类的实例以使程序运行<代码>服务器客户端myClient=新服务器客户端();myClient.playerDetails()但是我现在无法在main中引用非静态方法,因为我没有看到任何ServerSocket或Socket的实例。