我对拒绝正确刷新的 java.net.SocketOutputStream 感到沮丧!我必须忽略显而易见的事情。在下面的 Groovy 脚本中,我尝试连接到在我的 Galaxy Nexus Android 4.0.2 手机上运行的简单套接字服务器,它在我的 5 秒超时到期后立即给我一个套接字关闭异常。
SocketClient.groovy
import java.net.*
socket = new Socket()
socket.connect(new InetSocketAddress(InetAddress.getByAddress([172,17,57,21] as byte[]), 58789), 5000)
println "Connected!"
socket.withStreams { input, output ->
println "Processing ${input.class.name} and ${output.class.name} streams..."
output.withWriter {
it << 'echo testing\n\n';
it.flush();
output.flush()
def readerThread = Thread.start {
println "Request written, reading response..."; println input.text
}
println "Waiting 5 secs for read to complete."
readerThread.join(5000)
}
println "Stream closed!"
}
我尝试了所有不同的刷新方式,包括直接刷新输出流到使用写入器的间接包装和刷新。在流关闭之前服务器上不会出现任何数据,这会导致套接字关闭,这会导致任何从套接字读取的尝试崩溃。我究竟做错了什么?我也会在下面内联我的 Android 代码。这是一个启动套接字服务器的简单活动。
MyServer.java
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MyServer extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RawSocketServer rawSocketServer = new RawSocketServer();
String port = rawSocketServer.getLocalPort();
TextView textView = (TextView) this.findViewById(R.id.mainTextView);
textView.setText("Server bound to port " + port);
rawSocketServer.start();
}
}
RawSocketServer.java
package com.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by IntelliJ IDEA.
* User: cliff
* Date: 5/9/12
* Time: 4:03 PM
*/
public class RawSocketServer {
public static final int PORT = 0;
private ServerSocket serverSocket;
public RawSocketServer() {
this(PORT);
}
public RawSocketServer(int port) {
try {
this.serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("server cannot bind to port " + port);
}
}
public void start() {
new Thread(new Runnable() {
public void run() {
go();
}
}).start();
}
private void go() {
Socket socket = null;
while (true) {
try {
socket = this.serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("cannot accept from server socket" + e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not get input stream from socket. " + e);
}
PrintWriter writer = null;
try {
writer = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not get output stream from socket. " + e);
}
try {
for (String line = reader.readLine(); line !=null; line = reader.readLine()) {
writer.print(line);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error reading/writing from/to socket. " + e);
}
}
}
public String getLocalPort() {
return "" + serverSocket.getLocalPort();
}
}