WEB开发网
开发学院手机开发Android 开发 Windows客户端与Android服务端的Socket通信 阅读

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

{

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)

{

上一页  1 2 3 4  下一页

Tags:Windows 客户端 Android

编辑录入:coldstar [复制链接] [打 印]
赞助商链接