java – Android手机上的套接字连接超时

我正在尝试做一些服务器/套接字编程.服务器代码在Ubuntu机器上用普通
java编写.客户端代码是为我的LG Thrill编写的.服务器似乎工作正常.我通过在普通java中编写一个简单的测试客户端来测试服务器,该客户端基本上使用带有“localhost”ip的try / catch块.我检查了是否可以通过手机ssh进入服务器机器来访问ip(是的,它是可以访问的).

我的服务器代码如下:

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

        }

    }

}

我的CameraHandler代码:

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

    }

}

我的客户端代码如下:

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

    }
}

权限已设置,但仍会导致“错误:java.net.SocketTimeoutException:连接超时”.

我无法弄清楚是什么导致了它!有任何想法吗?

编辑:

没关系.网络问题.我在我的家庭网络上运行代码,一切正常.

最佳答案 老兄,尝试在它们之间添加flush():

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

你可以快速关闭插座……

点赞