当前位置:编程学习 > wap >>

安卓socket连接测试

初学
这个程序为何会崩

安卓端:
package com.example.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try
{
Socket client = new Socket("192.168.1.100", 30000);
} catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}




电脑端:
import java.io.IOException;
import java.net.ServerSocket;


public class Server
{

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub

ServerSocket server = new ServerSocket(30000);
while(true)
{
System.out.println("waiting client!!!");
server.accept();
System.out.println("client has been linked!!!");
}

}

}
--------------------编程问答-------------------- 第一你加网络限权没有?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

第二你是不是用4.0以上机子测试的?

你开一个线程把   Socket client = new Socket("192.168.1.100", 30000);放进去

因为4.0及其以上版本,主线程不允许访问网络。。。 --------------------编程问答--------------------
引用 1 楼 KPRF2009 的回复:
第一你加网络限权没有?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

第二你是不是用4.0以上机子测试的?

你开一个线程把   Socket client = new Socket("192.168.1.100", 30000);放进去

……


权限加了
是用4.2的机子。。。。。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,