Windows客户端与Android服务端的Socket通信
2010-06-04 14:09:00 来源:WEB开发网{
e.printStackTrace();
}
}
private void openUrl(String url)
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class G3Exp extends Activity{
/** Called when the activity is first created. */
//定义侦听端口号
final int SERVER_PORT = 10086;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//开一个新的线程来侦听客户端连接及发来的信息和打开相应网站
new Thread(){
public void run(){
startServer();
}
}.start();
}
private void startServer()
{
try
{
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
//循环侦听客户端连接请求
while (true)
{
Socket client = serverSocket.accept();
try
{
//等待客户端发送打开网站的消息
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
openUrl(str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
client.close();
}
Thread.sleep(3000);
}
}
catch (Exception e)
{
更多精彩
赞助商链接