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_Client Server_Bufferedreader_Printwriter - Fatal编程技术网

一个简单的java客户机-服务器程序

一个简单的java客户机-服务器程序,java,sockets,client-server,bufferedreader,printwriter,Java,Sockets,Client Server,Bufferedreader,Printwriter,所以我用java为我的大学迷你项目做了这个客户机-服务器程序。请注意,这只是我正在进行的一个大项目的一个小模块。我需要一个字符串从客户端发送到服务器。服务器将在返回到客户端时返回字符串。稍后将修改代码,以便在发回之前处理字符串。客户机将在需要时向服务器发送字符串。因此,这意味着服务器必须无限期运行 我在这里面临的问题是,只有当客户端发送字符串时,我的服务器才能第一次正常工作。如果我第二次使用不同的字符串运行客户端,我会返回之前发送给服务器的相同字符串 这是我的服务器程序: pub

所以我用java为我的大学迷你项目做了这个客户机-服务器程序。请注意,这只是我正在进行的一个大项目的一个小模块。我需要一个字符串从客户端发送到服务器。服务器将在返回到客户端时返回字符串。稍后将修改代码,以便在发回之前处理字符串。客户机将在需要时向服务器发送字符串。因此,这意味着服务器必须无限期运行

我在这里面临的问题是,只有当客户端发送字符串时,我的服务器才能第一次正常工作。如果我第二次使用不同的字符串运行客户端,我会返回之前发送给服务器的相同字符串

这是我的服务器程序:

        public class Server {

        public static boolean x = true;
        public static String reply;

        public static void main(String a[]) throws Exception {

        System.out.println("Entered server console..");
            Socket echoSocket = null;
            ServerSocket serverSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;

            System.out.println("Initializing Connection..");

            boolean runFlag = true;
            try {
                serverSocket = new ServerSocket(77);

                while (runFlag) {
                    echoSocket = serverSocket.accept();

                    out = new PrintWriter(echoSocket.getOutputStream(), true);

                    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

                    while (x) {

                        in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
                        reply = in.readLine();
                        if (reply != null) {
                            x = false;
                        }
                    }
                    System.out.println("received: " + reply);

                    out.println(reply);

                    System.out.println("sent back: " + reply);
                    stdIn.close();
                }

            } catch (Exception e) {
                System.out.println("Exception in starting server: " + e.getMessage());
            } finally {
                out.close();
                in.close();
                echoSocket.close();
            }

        }
    }
这是我的客户程序:

    public class Client {
    public static String reply,temp;
    public static boolean x=true;

    public Client()
    {
        temp="lala";
    }
    public Client(String t)
    {
        temp=t;
    }

    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket("localhost", 77);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: localhost.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        temp="lala"; //this is the string to be sent

        out.println(temp);

        while (x) {
            reply= in.readLine();
            if(reply!=null)
            {
                x=false;
            }
        }

        System.out.println("reply: "+reply);

        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
    }
}
有人能帮我找到问题所在吗

while (x) {
   in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
   reply = in.readLine();
   if (reply != null) {
      x = false;
   }
}
您的服务器在客户机第一次连接时进入此循环,并将应答字符串设置为来自客户机的某些输入。然而,它再也不会进入这个循环,因为x的值永远不会变回true


您的服务器在客户机第一次连接时进入此循环,并将应答字符串设置为来自客户机的某些输入。但是,它再也不会进入这个循环,因为x的值永远不会变回true。

当您接受请求时,x将被设置为false,并且永远不会变为true。 请在进入循环时在x上加上首字母。 此外,如果在客户端和服务器之间使用套接字,请移动

echoSocket = serverSocket.accept();
在第一个循环之外。您可以使用echoSocket进行通信。然后您将
保持长连接。

当您接受请求时,x将设置为false,并且永远不会变为true。 请在进入循环时在x上加上首字母。 此外,如果在客户端和服务器之间使用套接字,请移动

echoSocket = serverSocket.accept();
在第一个循环之外。您可以使用echoSocket进行通信。然后您将
保持长连接。

似乎您总是将拉拉发送到服务器。看看Apache Mina框架,他们在echo服务器上也有一个很好的示例,似乎您总是将拉拉发送到服务器。看看Apache Mina框架,他们在echo服务器上也有一个很好的示例,非常感谢!在循环开始之前,我将x定义为true,现在它就可以工作了!谢谢!在循环开始之前,我将x定义为true,现在它就可以工作了!