简体   繁体   中英

Socket Server will not receive properly

I am trying to make a socket server, I am connecting through putty to this server. Whenever I type "hi" it says "no" rather than "hi" which I want it to do. I found this on A java website. If you could tell me what I am doing wrong that would be great. Thanks!

int port = 12345;
ServerSocket sock = new ServerSocket(port);
System.out.println("Server now active on port: " + port);

Socket link = sock.accept();
System.out.println("Interface accepted request, IP: " + link.getInetAddress());

BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
PrintWriter output = new PrintWriter(link.getOutputStream(), true);

output.println("ISEEYOU");
String inputLine;

Thread.sleep(1500);

while((inputLine = input.readLine()) != null) {
    if(inputLine.equals("hi")) {
        output.println("hi");
    }else{
        output.println("no");
    }
}

Your Java program is correct.

I've tried your code, just added System.out.printf("[%s]", inputLine); as first line in the while loop to ensure, what I get from putty. I guess your problem is the protocol putty uses to connect. It worked with RAW for me. See below the session setting I've used:

PUTTY会话设置

EDIT: According to your comment I added some code for a simple client, that reads the line from console, sends it to the server and prints the echo back to console.

public void Client() throws IOException {
    // Client that closes the communication when the user types "quit"
    Socket socket = new Socket("localhost", 8080);
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintStream ps = new PrintStream(socket.getOutputStream()); 

    BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
    String line;
    while(!(line = user.readLine()).equals("quit")) {
        ps.println(line); // Write to server
        System.out.println(reader.readLine()); // Receive echo
    }
    socket.shutdownOutput(); // Send EOF to server
    socket.close();
}

The corresponding server would look like this:

public void server() throws IOException {
    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = serverSocket.accept();
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintStream ps = new PrintStream(socket.getOutputStream()); 

    // Just read a line and echo it till EOF
    String line;
    while((line = reader.readLine()) != null) ps.println(line);
}

You might need to change the port I used here, if 8080 is already binded on your machine. Also you might want to have the server running on another computer then the client. In this case you need to change "localhost".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM