Android作为完全开源的手机平台,已经被非常多的开发者视作未来最有潜力的智能手机操作系统。
而且短时间内已经有大量的第三方应用程序出现在Android Market供用户下载和使用,
其中有一些程序增强了手机的应用功能,而另外一些会充分的发挥手机与其它电子设备的互动。
今天将主要通过两个例子讲解如何实现Android与PC通讯。
1. Android与PC通讯的第一个例子:
PC: Java Server
Android:Java Client
Java Server 源代码:
public class TCPDesktopServer implements Runnable{ public static final String SERVERIP = "192.168.0.149"; public static final int SERVERPORT = 51706; public void run() { try { System.out.println("S: Connecting..."); ServerSocket serverSocket = new ServerSocket(SERVERPORT); while (true) { Socket client = serverSocket.accept(); System.out.println("S: Receiving..."); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = in.readLine(); System.out.println("S: Received: '" + str + "'"); } catch(Exception e) { System.out.println("S: Error"); e.printStackTrace(); } finally { client.close(); System.out.println("S: Done."); } } } catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } } public static void main (String a[]) { Thread desktopServerThread = new Thread(new TCPDesktopServer()); desktopServerThread.start(); } }
对上边的源代码做一些介绍:
指定Server监听的端口和服务器IP地址。
public static final String SERVERIP = "192.168.0.149";
public static final int SERVERPORT = 51706;
应用之前所指定的IP和Port创建一个ServerSocket对象。
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
用于侦听和捕捉通过Socket连接的客户端。
Socket client = serverSocket.accept();
应用Socket创建BufferedReader对象,用于接收Socket Stream中的数据。
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
Android Client 源代码:
InetAddress serverAddr = InetAddress.getByName("192.168.0.149");//TCPServer.SERVERIP Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, 51706); String message = "AndroidRes,Where is my Pig (Android)?"; try { Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { socket.close(); }
对上边的源代码做一些介绍:
指定Server的IP地址。
InetAddress serverAddr = InetAddress.getByName("192.168.0.149");
应用Server的IP和端口建立Socket对象。
Socket socket = new Socket(serverAddr, 51706);
根据已经建立的Socket来创建PrintWriter,将信息通过这个对象来发送给Server,其中包含了三个部分:
OutputStreamWriter
BufferedWriter
PrintWriter
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
以上是Android与运行在PC上的Java Server通讯的例子。来源由Anddev.org提供
2. Android与PC通讯的第二个例子:
PC: CSharp Server
Android:Java Client
通过上边的例子,本人通过一些转换,实现了Android与CSharp Server的通讯方法。原理没有太大差异,仅仅是应用的方法稍有不同。
CSharp Server 源代码:
TcpClient ConnectedClient = ServerListener.AcceptTcpClient(); NetworkStream netStream = ConnectedClient.GetStream(); BinaryReader br = new BinaryReader(netStream); byte[] tempByte = new byte[8192]; tempByte = br.ReadBytes(8192); string RecevieString = System.Text.Encoding.Default.GetString(tempByte);
《附》超时处理:
socket = new Socket(); isa = new InetSocketAddress(HOST,PORT); socket.connect(isa,TIMEOUT); //TIMEOUT为超时时间