无法从Socket读取

时间:2013-09-26 06:18:50

标签: java sockets

大家好! 我正在尝试使用套接字创建我的第一个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);
    }

}}

1 个答案:

答案 0 :(得分:0)

就像使用PrintWriter对象发送String一样,使用Scanner对象来读取String。像这样。

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
    {