Socket connection timeout on Android phone

I am trying to do programming on a server / juice. Server code is written in plain Java on an Ubuntu machine. Client code is written for my LG Thrill. The server seems to be working fine. I tested the server by writing a simple test client in simple Java, which basically uses a try / catch block with the ip address "localhost". I checked if ip was able to access the server machine from my phone (yes, this is available).

My server code looks like this:

package org.vsector.server; import java.io.IOException; import java.net.ServerSocket; public class SceneFlowServer { int port; Thread running = null; CameraHandler handler = null; public SceneFlowServer( int port, CameraHandler handler ){ this.port = port; this.handler = handler; } public void start(){ if( handler == null ) return; try{ running = new Thread(){ public void run(){ try{ ServerSocket listen = new ServerSocket( port ); while( true ){ System.out.print( "I am waiting on port " + port + "... " ); handler.accept( listen.accept() ); System.out.println( "Got someone!" ); Thread spinoff = new Thread( handler ); spinoff.start(); } }catch( IOException e ){ System.err.println( "Could not listen on port " + port + "!" ); System.err.flush(); return; } } }; running.start(); running.join(); }catch( Exception e ){ System.err.println( "Could not start thread!" ); System.err.flush(); } } } 

My CameraHandler Code:

 package org.vsector.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class CameraHandler implements Runnable { Socket client = null; public CameraHandler(){ } public void accept( Socket client ){ this.client = client; } @Override public void run() { // TODO Auto-generated method stub BufferedReader in; try { in = new BufferedReader( new InputStreamReader( client.getInputStream() )); System.out.println( in.readLine() ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

My client code looks like this:

 package org.vsector.client; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ProcessingClientActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView text = (TextView) findViewById( R.id.textBox ); Socket sock; try { text.append( "\nOpening and writing to socket... " ); sock = new Socket(); sock.connect( new InetSocketAddress( "some ip", 4444 ), 30000 ); PrintWriter out = new PrintWriter( sock.getOutputStream(), true ); out.println( "Hello!" ); out.close(); sock.close(); text.append( "Done!" ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); text.append( "Error: " + e + "\n" ); text.append( e.getMessage() ); } } } 

The job is allowed, but it still leads to the error "Error: java.net.SocketTimeoutException: connection timeout".

I can’t understand what causes this! Any ideas?

Edit:

Nevermind The problem with the network. I ran the code on my home network and everything works a-ok.

+4
source share
2 answers

No problem with the code, but everything with the network I was on.

After I moved to my home network, everything works.

+1
source

dude, try adding flush () between them:

 out.println( "Hello!" ); out.flush(); //add this line out.close(); 

your socket closure for quick maybe.

+1
source

All Articles