4

I'm trying to do some server/socket programming. The server code is written in plain java on an Ubuntu machine. The client code is written for my LG Thrill. The server appears to work fine. I've tested the server by writing a simple test client in plain java that basically uses the try/catch block with ip of "localhost". I checked if the ip was reachable by ssh'ing into the server machine from my phone (yes, it's reachable).

My server code looks like:

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:

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() );
        } 

    }
}

Permission is set, but this still results in "Error: java.net.SocketTimeoutException: Connection timed out."

I can't figure out what's causing it! Any ideas?

Edit:

Nevermind. Network issue. I ran the code on my home network and everything works a-ok.

6
  • I've added the server side code that I've written. Commented Jun 18, 2012 at 21:55
  • Try to run your test client but instead of "localhost", use the server IP and see if it still works. Commented Jun 18, 2012 at 22:14
  • the code looks fine. Are you sure that you are using right server address and the same port? Commented Jun 18, 2012 at 22:42
  • Sorry for the late response. I've tried using the client code on the ip and it still works, but running on the android phone doesn't... Commented Jun 26, 2012 at 17:53
  • It's funny that it works the other way around: android phone as the server and the pc as the client. Commented Jun 26, 2012 at 18:19

2 Answers 2

1

dude, try to add flush() between them:

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

your'e closing the socket to fast maybe..

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the tip, but it doesn't appear to be connecting at all. So there's no socket connection to write to or close.
tnx, (check 'up' ?) what about your pc, are you listening to this port on your pc? reachable is not listening... you need s sreverSocket or another 3dp party app like netcat (what i use for testing...)
Yes, my pc is listening to this port, I've written the server side code in pure java. I've tested to check if the pc is reachable via ssh from the phone and I've tested if the port is open via running the code on the same pc. I don't know if the phone cannot connect to that particular port or something.
btw, just learned that according to the api, close() also performs flush()
1

There's no actual problem with the code, but everything with the network I was on.

After I moved to my home network, everything works.

1 Comment

But application is not sending or reciving data then what is solution for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.