简体   繁体   English

发送/接收的套接字错误

[英]Socket error with sending/receiving

If I send a message, it will only be printed out by the server if I run it in a loop. 如果我发送一条消息,则仅当我循环运行该消息时,它才会被服务器打印出来。

I tried to send the message after 1 minute, send it 50 times, but nothing will print it out on the server side. 我尝试在1分钟后发送消息,将其发送50次,但没有任何内容可以在服务器端将其打印出来。

What is my error? 我怎么了

Here is my code, it was written in Netbeans: 这是我的代码,它是用Netbeans编写的:

package me.jackboyplay.sockets_client;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JackboyPlay
 */
public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = null;
        try {
            socket = new Socket("localhost", 5001);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(socket == null){
            return;
        }
        DataInputStream dis = null;
        PrintWriter dos = null;
        try {
            dis = new DataInputStream(socket.getInputStream());
            dos = new PrintWriter(socket.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        int x = 0;
        Long l = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
        for(int i = 0; i < 50; i++){
            dos.write("X: " + i);
        }
    }

}

package me.jackboyplay.jc_cloudsystem.mechaniken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JackboyPlay
 */
public class SocketClient extends Thread {

    private Socket socket;

    public SocketClient(Socket socket){
        this.socket = socket;
        System.out.println("Ein Client wurde akzeptiert.");
    }

    @Override
    public void run() {
        BufferedReader br;
        PrintWriter pw;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream());

            while(true){
                String line = br.readLine();
                System.out.println(line);
            }

        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Class 1 is the Client that sends a message to the server. 类1是向服务器发送消息的客户端。

Class 2 is the Server that will print out the message. 类2是将打印出消息的服务器。

The server is using BufferedReader.readLine() , but the client is not sending any line breaks, so readLine() never returns: 服务器正在使用BufferedReader.readLine() ,但是客户端没有发送任何换行符,因此readLine()从不返回:

Reads a line of text. 读取一行文本。 A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed. 一行被认为由换行符('\\ n'),回车符('\\ r')或回车符后立即换行符中的任何一个终止。

The client should be using PrintWriter.println() instead of PrintWriter.write() : 客户端应该使用PrintWriter.println()而不是PrintWriter.write()

Terminates the current line by writing the line separator string. 通过写入行分隔符字符串来终止当前行。 The line separator string is defined by the system property line.separator , and is not necessarily a single newline character ('\\n'). 行分隔符字符串由系统属性line.separator定义,并且不一定是单个换行符('\\ n')。

dos.println("X: " + i);

Prints a String and then terminates the line. 打印一个字符串,然后终止该行。 This method behaves as though it invokes print(String) and then println() . 此方法的行为就像先调用print(String)然后调用println()

Alternatively, use PrintWriter.print() to write the values as needed, and then call PrintWriter.println() when finished: 或者,使用PrintWriter.print()根据需要写入值,然后在完成时调用PrintWriter.println()

dos.print("X: ");
dos.print(i);
dos.println();

Either way, you may also need to call PrintWriter.flush() to actually send the output over the wire: 无论哪种方式,您都可能需要调用PrintWriter.flush()来通过电线实际发送输出:

dos.flush();

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

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