【问题标题】:Cannot read from Socket无法从 Socket 读取
【发布时间】:2013-09-26 15:39:43
【问题描述】:

大家! 我正在尝试使用套接字制作我的第一个 Java 应用程序。而且我无法从 Socket 读取数据。 这是一个 ServerThread 的源代码:

public class ServerThread extends Thread{
private final int SERVER_PORT = 444;
private ServerSocket serv;
public ServerThread(){

    try {
        serv = new ServerSocket(SERVER_PORT);
    } catch (IOException ex) {
        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void run(){
    while(true){
        try {
            Socket client = serv.accept();
            client.setSoTimeout(0);
            client.setTcpNoDelay(true);
            client.setSendBufferSize(65536);
            if(client.isConnected()){
                new Thread(new MsgSrv(client)).start();
                System.out.println("Client connected");
            }
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
} }   

这是一个处理客户端连接的类:

public class MsgSrv extends Thread{
private BufferedWriter out;
private BufferedReader in;
private boolean isFinished = false;
private String buff;
private Socket sock;
public MsgSrv(Socket sc){
    try {
        sock = sc;
        in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
    } catch (IOException ex) {
        Logger.getLogger(MsgSrv.class.getName()).log(Level.SEVERE, null, ex);
    }

}
public void run(){
    while(!isFinished){
        try {
            buff=in.readLine();
            System.out.println(buff);
            out.write("Hello");
        } catch (IOException ex) {
            Logger.getLogger(MsgSrv.class.getName()).log(Level.SEVERE, null, ex);
            isFinished=true;
        }
    }
}}

客户端应用程序向服务器发送一个字符串,并尝试读取答案。但什么也没有发生。 in.readLine 什么也不返回,永远等待服务器的答复。 你能帮我解决一个问题吗? 为了读取数据,我已经使用了 DataInputStream、BufferedReader 类。 客户端应用的来源:

public class JavaApplication4 {

/**
 * @param args the command line arguments
 */
public static Socket connectSock;
public static PrintWriter out;
public static DataInputStream in;
public static void main(String[] args) {
    try {

        connectSock = new Socket("127.0.0.1", 444);
        if(connectSock.isConnected()){
            in = new DataInputStream(connectSock.getInputStream());
            out = new PrintWriter(connectSock.getOutputStream(), true);
            out.println("!hello!");
            out.flush();
            String s;
            while(true){
                s = in.readUTF();
                System.out.println(s);
            }

               }else
        {
            System.out.println("bad socket");
            System.exit(0);
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(JavaApplication4.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication4.class.getName()).log(Level.SEVERE, null, ex);
    }

}}

【问题讨论】:

  • isConnected() 测试都毫无意义。套接字不可能在这些点上没有连接。
  • isConnected 方法没有问题。服务器从客户端接收字符串并将其打印到控制台。但是客户端无法从服务器接收字符串。
  • 我的评论是评论,而不是答案。在这些地方调用该方法仍然毫无意义。不要编写无意义的代码。它只是混淆了这个问题。

标签: java sockets


【解决方案1】:

就像您使用PrintWriter 对象发送字符串一样,使用Scanner 对象来读取字符串。像这样。

input = server.getInputStream();
Scanner scanner = new Scanner(input);

while (scanner.hasNextLine()) {
     s = (String) scanner.nextLine();
}

编辑:

如果您像这样更改您的客户端,并确保在使用 scanner.nextLine(); 时在字符串 ("\n") 的末尾有一个换行符

if(connectSock.isConnected()){           
        Scanner scanner = new Scanner(connectSock.getInputStream());
        out = new PrintWriter(connectSock.getOutputStream(), true);
        out.println("!hello!");
        out.flush();
        String s;
        while (scanner.hasNextLine()) {
            s = (String) scanner.nextLine();

        }

           }else
    {

【讨论】:

  • 你确定有连接吗?
  • 是的,我确定。因为服务器从客户端接收字符串。
  • 使用服务器上的 PrintWriter 和 Scanner 来
  • 还是一无所获。顺便一提。如果我通过 telnet 连接到服务器,一切正常。服务器回答我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多