一个简单的java客户端服务器程序 [英] A simple java client server program

查看:159
本文介绍了一个简单的java客户端服务器程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I did this client server program in java for my college mini project. Note that this is just a small module of a big project I'm working on. I need a string to be sent from the client to the server. The server will return back the string as it is back to the client. (The code will be modified later such that the string is processed before sending back). The client will send a string whenever needed to the server. Thus it means it is compulsory for the server to be running for indefinite time.

我面对的问题是,我的服务器只在客户端发送字符串时才第一次正常工作。如果我第二次使用不同的字符串运行客户端,我回到我以前发送到服务器的相同的字符串!

The problem I face here is that my server works perfectly only for the first time when the client sends a string. If I run the client the second time with a different string, I get back the same string I sent to the server previously!

这是我的服务器程序:

        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();
    }
}

任何人都可以帮助我找到这里的问题是什么?

Can anyone help me find what the problem here is?

推荐答案

while (x) {
   in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
   reply = in.readLine();
   if (reply != null) {
      x = false;
   }
}

您的服务器在客户端第一次连接时进入此循环,并且将回复String设置为来自客户端的某个输入。但是,它永远不会再进入这个循环,因为x的值永远不会变回true。

Your server enters this loop the first time a client connects, and it sets the reply String to some input from the client. However, it never enters this loop again, as x's value never changes back to true.

这篇关于一个简单的java客户端服务器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆