A simple java client server program

2019-08-04 05:28发布

问题:

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!

Here is my server program:

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

        }
    }

Here is my Client program:

    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?

回答1:

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

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.



回答2:

When you accept a request, the x will be set false and never become true. Please initial the x when you enter the loop. What's more,if you use a socket between client and server, please move the

echoSocket = serverSocket.accept();

out of the first loop.And you can use echoSocket to communicate.Then you will keep the long connection.