문제

I have found an example of a simple program in java that suppose to work which implements a client-server example in Java.

Client code:

import java.io.*;
import java.net.*;

public class ClientDemo {

    public static void main(String[] args) {

        try {
            Socket sock = new Socket ("127.0.0.1", 4321);

            DataInputStream input = new DataInputStream(sock.getInputStream());

            boolean more_data = true;

            while (more_data) {

                String line = input.readLine();

                if(line==null) {
                    more_data = false;
                }
                else {
                    System.out.println(line);
                }            
            }
        }
        catch (IOException e) {
        }    
    }    
}

Server code:

import java.io.*;
import java.net.*;

public class SimpleServer
{

public static void main(String[] args)
{
    try
{
        ServerSocket server = new ServerSocket (4321);            
        Socket sock = server.accept();
        DataInputStream inp = new DataInputStream (sock.getInputStream());
        PrintStream out = new PrintStream (sock.getOutputStream());
        output.println("server message");
        output.println("QUIT to Quit");

        boolean more_data = true;

            while (more_data)
    {
                String line = inp.readLine();

                if(line==null)
        {
                    more_data = false;
                }
                else
                {
                    output.println("Server:" +line+"\n");
                    if(line.trim().equals("Exit")
        {
                        more_data = false;
                    }
                }            
            }
            sock.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }    
    }    
}

when running the server first and then the client, I get the welcome message, but when entering a text in the client console then pressing enter, nothing happens.

I tried replacing the DataInputStream in BufferedInputStream as in the docs it said readline was deprecated, but the same behavior, plus I tried to change to scanner object, with nextLine, bt the same behavior there too.

your input would be appreciated very much.

도움이 되었습니까?

해결책

Try this client. And have a look at your server. It stop with Exit not with QUIT

UPDATE

I have changed the code and now I have to change my name ;-)

The client is very, very simple and nobody should use it. The client have to know how many lines the server will send. There is no way to wait of an unknown count of lines without another thread.

import java.io.*;
import java.net.*;

public class ClientDemo {

    public static void main(String[] args) {

        try {
            Socket sock = new Socket("127.0.0.1", 4321);
            // Build a Buffered Reader - it is easier to read complete line
            BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            // After connecting we read the first 2 line
            // We know that the server send 2 lines
            System.out.println(input.readLine());
            System.out.println(input.readLine());
            // Wen must have a reader for our inputs
            InputStreamReader consoleReader = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(consoleReader);
            while (true) {
                // read one line from the console
                String inline = in.readLine();
                inline += "\n";
                // send the line to the server
                sock.getOutputStream().write(inline.getBytes());
                sock.getOutputStream().flush();
                // Wait for server response - we know that we get 2 lines
                for (int i = 0; i < 2; i++) {
                    String response = input.readLine();
                    if (response == null || inline.equals("Exit")) {
                        break;
                    } else {
                        System.out.println(response);
                    }

                }
            }
        } catch (IOException e) {
        }
    }
}

다른 팁

That is because your client doesn't even try to read from the console. It only reads from the socket.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top