如何使用线程JAVA从套接字读取输入

时间:2015-03-08 09:25:00

标签: java multithreading sockets client inputstream

我正在编写一个使用服务器和一个客户端的简单套接字编程应用程序。我正在尝试启动一个线程(来自客户端文件),该线程从套接字输入流中读取输入,以便我可以将消息写入服务器并将其读回并同时将它们打印到屏幕上。但是,当我运行我的代码时,我的代码卡在

message = in.readLine();
InputReader.java file中的

并且没有输入?

我的代码如下,请帮助。

SimpleServer1.java

public class SimpleServer1 {
public static void main(String args[]){
    int portNumber = Integer.parseInt(args[0]);

    ServerSocket serverSocket = null;
    Socket clientConnection = null;

    try{
        //setup sockets
        serverSocket = new ServerSocket(portNumber);
        clientConnection = serverSocket.accept();
        System.out.println("Connected to" + clientConnection.getInetAddress());

        //setup streams
        BufferedReader in = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
        PrintWriter out = new PrintWriter(clientConnection.getOutputStream());
        out.flush();

        //read input from stream
        String message;
        while((message = in.readLine()) != null){
            //return message to client
            out.println("Echo: " + message);

            System.out.println(clientConnection.getInetAddress() + ": " + message);
            if (message.equalsIgnoreCase("bye")){
                System.out.println("Closing connection...");
                break;
            }
        }
        //close streams
        out.close();
        in.close();
        clientConnection.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

SimpleClient1.java

public class SimpleClient1{
public static void main(String args[]){
    String hostName = (args[0]);
    int portNumber = Integer.parseInt(args[1]);

    try{
        Socket serverConnection = new Socket(hostName, portNumber);
        PrintWriter out = new PrintWriter(serverConnection.getOutputStream(), true);
        out.flush();

        Thread listener = new Thread(new InputReader(serverConnection));
        listener.start();

        Scanner keyboard = new Scanner(System.in);
        String userInput;
        while((userInput = keyboard.nextLine()) != null){
            out.println(userInput);
            if (userInput.equalsIgnoreCase("bye")){
                break;
            }

        }
        //closing streams
        out.close();
        serverConnection.close();

    }catch(Exception e){
        e.printStackTrace();
    }   
}

InputReader.java< - 我想用

运行我的线程
public class InputReader implements Runnable{
private Socket serverConnection;
private BufferedReader in;
public InputReader(Socket socket){
    serverConnection = socket;
    try{
        in = new BufferedReader(new InputStreamReader(serverConnection.getInputStream()));
    }catch(IOException ioE){ioE.printStackTrace();}
}

@Override
public void run() {
    try{
        String message;
        while(true){
            System.out.println("done");
            message = in.readLine();
            System.out.println(message);
        }
    }catch(IOException ioE){ioE.printStackTrace();}

}

最终,我想使用线程从套接字流中读取和写入。

提前致谢:)

Cobezz

2 个答案:

答案 0 :(得分:0)

我相信在你写完之后你必须刷新你正在写的流。您似乎在创建流后立即刷新流,但不会产生任何影响。

答案 1 :(得分:0)

在SimpleServer1.java中

你必须添加out.flush();

while((message = in.readLine())!= null){

out.println(" Echo:" + message);

<强>了out.flush();

System.out.println(clientConnection.getInetAddress()+&#34;:&#34; + message);

if(message.equalsIgnoreCase(&#34; bye&#34;)){

System.out.println(&#34; Closing connection ...&#34;);

中断;

}

}