Java客户端/服务器不通过Socket / ServerSocket进行通信

时间:2014-04-14 18:50:11

标签: java sockets bufferedreader

我有一个简单的socket / serversocket示例,我试图让它运行,但客户端和服务器都在BufferedReader尝试读取时挂起。以下是每个代码:

服务器

package picturePerfect;
--imports--

public class PictureServer implements Runnable{
static ServerSocket serverSocket;

public static void main(String[] args) throws IOException {
    serverSocket = new ServerSocket(2342);
    Thread firstSessionThread = new Thread(new PictureServer());
    firstSessionThread.start();
}

@Override
public void run() {
    Socket socket = null;
    try {
        socket = serverSocket.accept();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String clientRequest = bufferedReader.readLine();
        System.out.println(clientRequest);
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        printWriter.println("Sent from server!");
    } catch (IOException e) {
        e.printStackTrace();
    }       
}

}

客户端

package picturePerfect;
--imports--

public class PictureClient {
    public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
        Socket socket = new Socket("localhost", 2342);
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        printWriter.write("Sent from client!");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String response = bufferedReader.readLine();
        System.out.println(response);
        socket.close();
    }
}

这是我可以简化我的代码。我有一个我一直关注的示例程序,看起来几乎完全相同。这是示例服务器和客户端( 工作):

SAMPLE SERVER

--imports--

public class Server implements Runnable{
    static ServerSocket ss;

    public static void main(String[] args) throws IOException {
        ss = new ServerSocket(3142);
        Thread thread = new Thread(new Server());
        thread.start();
    }

    @Override
    public void run() {
        while ( true ) {
            Socket s = null;

            try {
                s = ss.accept();
                BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String operands = br.readLine();
                System.out.println(operands + " was received");
                PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
                pw.println(operands + " right back!");
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }
}

示例客户

--imports--

public class Server implements Runnable{
    static ServerSocket ss;


    public static void main(String[] args) throws IOException {
        ss = new ServerSocket(3142);
        Thread thread = new Thread(new Server());
        thread.start();
    }

    @Override
    public void run() {
        while ( true ) {
            Socket s = null;

            try {
                s = ss.accept();
                BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String operands = br.readLine();
                System.out.println(operands + " was received");
                PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
                pw.println(operands + " right back!");
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }
}

我已经尝试将while循环放入我的服务器并将我的客户端和服务器移动到默认包中,但都没有帮助。我还尝试使用read()代替readLine(),并使用\r\n结束printWriter的行,但在那里却不成功。

为什么我的代码会挂在readLine()上,特别是当示例代码没有?

2 个答案:

答案 0 :(得分:2)

readline()做什么是等到它看到一个新的行字符,直到它返回,因此readLine()。

在您的客户端中,您不会写新行。你使用:

printWriter.write("Sent from client!");

相反,使用println,

将换行符写入流中
printWriter.println("Sent from client!");

答案 1 :(得分:2)

服务器期望读取行,因此您需要在按摩后添加行分隔符。要做到这一点而不是

printWriter.write("Sent from client!");

在客户端使用

printWriter.println("Sent from client!");
//          ^^^^^^^

printWriter.write("Sent from client!"+System.lineSeparator());
printWriter.flush();

您需要flush自己,因为PrintWriter中的autoflush仅适用于printlnprintfformat,而不适用于write方法