I am getting frustrated over a java.net.SocketOutputStream that just refuses to flush properly! I must be overlooking the obvious. In my Groovy script below I attempt to connect to a simple socket server running on my Galaxy Nexus Android 4.0.2 phone and it gives me a socket closed exception right after my 5 second timeout expires.
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!"
}
I’ve tried all different means of flushing, including direct flush of the output stream to indirect wrapping with a writer and flushing. No data appears on the server until the stream closes which causes the socket to close which crashes any attempt to read from the socket. What am I doing wrong? I’ll inline my Android code below as well. It’s a simple activity that launches a socket server.
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();
}
}
I am wondering…. you have “it << ‘echo testing'” there. Using that on a Writer means afaik that no line end is written. Since your server uses readLine, it expects a line ending. Thus the data may arrive, but readLine is not finishing, because it waits for the end of the line. The fix would then be to do for example “it << ‘echo testing\n'” or “it << ‘echo testing’ << ‘\n'”.
Now I don’t know the Android API so much, but your server may have the same problem in “writer.print(line);”, because I think that a line read with readLine, does not contain the line ending anymore. But since that is not read again, it should be of no problem at the moment.