简体   繁体   English

套接字服务器将无法正确接收

[英]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. 每当我键入“ hi”时,它都会说“ no”而不是我希望它执行的“ hi”。 I found this on A java website. 我在A java网站上找到了这个。 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. 您的Java程序是正确的。

I've tried your code, just added System.out.printf("[%s]", inputLine); 我已经尝试过您的代码,只是添加了System.out.printf("[%s]", inputLine); as first line in the while loop to ensure, what I get from putty. 作为while循环中的第一行,以确保我从腻子中得到什么。 I guess your problem is the protocol putty uses to connect. 我想您的问题是腻子用于连接的协议。 It worked with RAW for me. 它适用于RAW。 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. 如果您的计算机上已经绑定了8080,则可能需要更改此处使用的端口。 Also you might want to have the server running on another computer then the client. 另外,您可能希望服务器在客户端上然后在另一台计算机上运行。 In this case you need to change "localhost". 在这种情况下,您需要更改“ localhost”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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