建立ServerSocket连接

时间:2012-11-20 09:31:05

标签: android serversocket

我在我的应用程序中使用了这段代码 -

 public class BorderCastList extends Activity {
    private VideoView video;
    private ServerSocket server;
    private MediaController media;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //  Settings window for translucent state
    //   this.getWindow().setFormat(PixelFormat.TRANSLUCENT);
    this.setContentView(R.layout.video);
                   new Thread(new Runnable() {

    @Override
    public void run() {
    try {
    Log.i("test", "before info ServerSocket");
    server = new ServerSocket(8050)
    Log.i("test", "after info ServerSocket");
    Log.i("ip:" + server.getLocalSocketAddress() + "----port: "
    + server.getLocalPort(), "");
    Log.i("service ip: " + server.getInetAddress(), "");
    Log.i("Server build success************", "");
    //   Socket socket = server.accept();

    } catch (Exception e) {
    Log.i("aaa", "bbbb");
    e.printStackTrace();
    }
    }
    }).start();
    )

为什么我会获得日志输出Log.i("test", "before info ServerSocket");而不是Log.i("test", "after info ServerSocket");

 Log.i("ip:" + server.getLocalSocketAddress() + "----port: "+ server.getLocalPort(), "");
 Log.i("server ip: " + server.getInetAddress(), "");
 Log.i("Server build success************", "");

为什么它不会在new ServerSocket(8050)之后执行?

1 个答案:

答案 0 :(得分:1)

在Galaxy Nexus Android 4.1.2上,您的代码可以毫无问题地创建和绑定套接字。你的清单中是否有use-permission android:name =“android.permission.INTERNET”?否则只是期望堆栈跟踪以及 bbbb 关于 aaa