服务器套接字编程中的readLine为空

时间:2018-06-10 17:05:33

标签: null bufferedreader readline serversocket outputstream

我正在研究java服务器套接字程序,而BufferedReader中的in.readLine()为null。我尝试成功调试程序,但无法找到为什么BufferedReader in.readline()在两个类中都保持返回null。

package clientserver;  

import java.io.*;  
import java.net.*;  
import java.util.logging.Level;  
import java.util.logging.Logger;  
/**  
*  
* @author carsongedeus  
*/  

客户端类

public class Client {

    public static void main(String[] args) throws IOException {

        try {
            InetAddress iA = InetAddress.getLocalHost();
            String hostAddr = iA.getHostAddress();
            String inputLine;
            int portNumber = 5001;

            Socket socket = new Socket(hostAddr, portNumber);
            Socket is instantiated and data should be read in ASCII encoding
            try (BufferedReader in = new BufferedReader(new 
                InputStreamReader(socket.getInputStream(), "US-ASCII"))) {
                System.out.println("Echo from client: " + in.readLine());

                PrintWriter out = new PrintWriter(new 
                OutputStreamWriter(socket.getOutputStream()), true);
                out.write("Message from client: " + socket.getInetAddress());

                while((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

问题:in.readLine等于null,但应该是字符串值 由于上述行中的条件不为真,因此while循环不能开始/发生。

package clientserver;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author carsongedeus
*/

服务器类

public class Server implements Runnable {

    private final ServerSocket serverSocket;

    public Server(String hostAddr, int portNumber) throws IOException {
        serverSocket = new ServerSocket(portNumber);
        serverSocket.setSoTimeout(60000);
    }

    @Override
    public void run() {

        try {
            Socket socket = this.serverSocket.accept();
            System.out.println("Connected to " + socket.getInetAddress() + 
                    " on port " + socket.getLocalPort());

            try (BufferedReader in = new BufferedReader(new 
            InputStreamReader(socket.getInputStream(), "US-ASCII"))) {
            System.out.println("Echo from client: " + in.readLine());

                PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                out.write("Message from server: " + socket.getInetAddress());
            }
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) throws IOException {

        InetAddress iA = InetAddress.getLocalHost();
        String hostAddr = iA.getHostAddress();
        int portNumber = 5001;

        Server server = new Server(hostAddr, portNumber);

        Thread t = new Thread(server);
        t.setDaemon(false);
        t.start();
    }
}

使用run方法

启动线程

0 个答案:

没有答案