无法从Mac上的Java Socket读取

时间:2015-10-07 08:32:55

标签: java macos sockets

我正在用Java教授网络通信,我有一个非常简单的客户端和服务器作为演示,使用套接字和ServerSockets。该应用程序在Windows和Linux下按预期工作。所有学生都使用Java 8(这是一个JavaFX应用程序)。

有几个学生有Mac。在Mac上,客户端在尝试从套接字读取时挂起。即使将readline()方法替换为单字符read()方法也是如此。

服务器的答案是一个简单的文本:HTTP 200状态,然后发回客户端的原始请求。所有行都以“\ n”结尾。客户端和服务器都在学生的Mac上运行,使用端口8080(或更高 - 没有区别)。奇怪的是,服务器在读取套接字时没有问题,即使代码是相同的。

以下是客户端中的相关代码:

    StringBuffer urlContent = new StringBuffer();

    // Set up the socket
    s = new Socket(ipAddress, port);

    // Send our request, using the HTTP 1.0 protocol
    out = new OutputStreamWriter(s.getOutputStream());
    out.write("GET / HTTP/1.0\n");
    out.write("User-Agent: Browser0\n");
    out.write("Host: " + ipAddress + ":" + port + "\n");
    out.write("Accept: text/html, */*\n\n");
    out.flush();

    // Set up the reader classes
    InputStream in1 = s.getInputStream();
    InputStreamReader in2 = new InputStreamReader(in1);
    inReader = new BufferedReader(in2);

    while ((lineIn = inReader.readLine()) != null) {  // Macs hang here
        urlContent.append(lineIn + "\n");
    }

在Mac上使用Java套接字有什么特别之处吗?

0 个答案:

没有答案
相关问题