注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liangxh2008的博客

 
 
 

日志

 
 

Android与PC通讯的第一个例子  

2010-03-24 18:46:35|  分类: pc sync |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
文章分类:移动开发

1. Android与PC通讯的第一个例子:

  • PC: Java Server
  • Android:Java Client

Java Server 源代码:

DE<01DE< DE<publicDE< DE<classDE< DE<TCPDesktopServer DE<DE<implementsDE< DE<Runnable{ DE<
DE<02DE< DE< DE< 
DE<03DE< DE<    DE<DE<publicDE< DE<staticDE< DE<finalDE< DE<String SERVERIP = DE<DE<"192.168.0.149"DE<DE<; DE<
DE<04DE< DE<    DE<DE<publicDE< DE<staticDE< DE<finalDE< DE<intDE< DE<SERVERPORT = DE<DE<51706DE<DE<; DE<
DE<05DE< DE< DE< 
DE<06DE< DE<    DE<DE<publicDE< DE<voidDE< DE<run() { DE<
DE<07DE< DE<         DE<DE<tryDE< DE<{ DE<
DE<08DE< DE<             DE<DE<System.out.println(DE<DE<"S: Connecting..."DE<DE<); DE<
DE<09DE< DE< DE< 
DE<10DE< DE<             DE<DE<ServerSocket serverSocket = DE<DE<newDE< DE<ServerSocket(SERVERPORT); DE<
DE<11DE< DE<             DE<DE<whileDE< DE<(DE<DE<trueDE<DE<) { DE<
DE<12DE< DE< DE< 
DE<13DE< DE<                  DE<DE<Socket client = serverSocket.accept(); DE<
DE<14DE< DE<                  DE<DE<System.out.println(DE<DE<"S: Receiving..."DE<DE<); DE<
DE<15DE< DE< DE< 
DE<16DE< DE<                  DE<DE<tryDE< DE<{ DE<
DE<17DE< DE<                      DE<DE<BufferedReader in = DE<DE<newDE< DE<BufferedReader(DE<DE<newDE< DE<InputStreamReader(client.getInputStream())); DE<
DE<18DE< DE<                      DE<DE<String str = in.readLine(); DE<
DE<19DE< DE<                      DE<DE<System.out.println(DE<DE<"S: Received: '"DE< DE<+ str + DE<DE<"'"DE<DE<); DE<
DE<20DE< DE<                    DE<DE<} DE<DE<catchDE<DE<(Exception e) { DE<
DE<21DE< DE<                        DE<DE<System.out.println(DE<DE<"S: Error"DE<DE<); DE<
DE<22DE< DE<                        DE<DE<e.printStackTrace(); DE<
DE<23DE< DE<                    DE<DE<} DE<DE<finallyDE< DE<{ DE<
DE<24DE< DE<                        DE<DE<client.close(); DE<
DE<25DE< DE<                        DE<DE<System.out.println(DE<DE<"S: Done."DE<DE<); DE<
DE<26DE< DE<                    DE<DE<} DE<
DE<27DE< DE< DE< 
DE<28DE< DE<             DE<DE<} DE<
DE<29DE< DE< DE< 
DE<30DE< DE<         DE<DE<} DE<DE<catchDE< DE<(Exception e) { DE<
DE<31DE< DE<             DE<DE<System.out.println(DE<DE<"S: Error"DE<DE<); DE<
DE<32DE< DE<             DE<DE<e.printStackTrace(); DE<
DE<33DE< DE<         DE<DE<} DE<
DE<34DE< DE<    DE<DE<} DE<
DE<35DE< DE< DE< 
DE<36DE< DE<    DE<DE<publicDE< DE<staticDE< DE<voidDE< DE<main (String a[]) { DE<
DE<37DE< DE<        DE<DE<Thread desktopServerThread = DE<DE<newDE< DE<Thread(DE<DE<newDE< DE<TCPDesktopServer()); DE<
DE<38DE< DE<        DE<DE<desktopServerThread.start(); DE<
DE<39DE< DE<    DE<DE<} DE<
DE<40DE< DE<}DE<

对上边的源代码做一些介绍:
指定Server监听的端口和服务器IP地址。

DE<1DE< DE<publicDE< DE<staticDE< DE<finalDE< DE<String SERVERIP = DE<DE<"192.168.0.149"DE<DE<; DE<
DE<2DE< DE<publicDE< DE<staticDE< DE<finalDE< DE<intDE< DE<SERVERPORT = DE<DE<51706DE<DE<;DE<

应用之前所指定的IP和Port创建一个ServerSocket对象。

DE<1DE< DE<ServerSocket serverSocket = DE<DE<newDE< DE<ServerSocket(SERVERPORT);DE<

用于侦听和捕捉通过Socket连接的客户端。

DE<1DE< DE<Socket client = serverSocket.accept();DE<

应用Socket创建BufferedReader对象,用于接收Socket Stream中的数据。

DE<1DE< DE<BufferedReader in = DE<DE<newDE< DE<BufferedReader(DE<DE<newDE< DE<InputStreamReader(client.getInputStream()));DE<

Android Client 源代码:

DE<01DE< DE<InetAddress serverAddr = InetAddress.getByName(DE<DE<"192.168.0.149"DE<DE<);DE<DE<//TCPServer.SERVERIP DE<
DE<02DE< DE<Log.d(DE<DE<"TCP"DE<DE<, DE<DE<"C: Connecting..."DE<DE<); DE<
DE<03DE< DE<Socket socket = DE<DE<newDE< DE<Socket(serverAddr, DE<DE<51706DE<DE<); DE<
DE<04DE< DE<String message = DE<DE<"AndroidRes,Where is my Pig (Android)?"DE<DE<; DE<
DE<05DE< DE<tryDE< DE<{ DE<
DE<06DE< DE<    DE<DE<Log.d(DE<DE<"TCP"DE<DE<, DE<DE<"C: Sending: '"DE< DE<+ message + DE<DE<"'"DE<DE<); DE<
DE<07DE< DE<    DE<DE<PrintWriter out = DE<DE<newDE< DE<PrintWriter( DE<DE<newDE< DE<BufferedWriter( DE<DE<newDE< DE<OutputStreamWriter(socket.getOutputStream())),DE<DE<trueDE<DE<); DE<
DE<08DE< DE<        DE<DE<out.println(message); DE<
DE<09DE< DE<} DE<DE<catchDE<DE<(Exception e) { DE<
DE<10DE< DE<        DE<DE<Log.e(DE<DE<"TCP"DE<DE<, DE<DE<"S: Error"DE<DE<, e); DE<
DE<11DE< DE<} DE<DE<finallyDE< DE<{ DE<
DE<12DE< DE<    DE<DE<socket.close(); DE<
DE<13DE< DE<}DE<

对上边的源代码做一些介绍:
指定Server的IP地址。

DE<1DE< DE<InetAddress serverAddr = InetAddress.getByName(DE<DE<"192.168.0.149"DE<DE<);DE<

应用Server的IP和端口建立Socket对象。

DE<1DE< DE<Socket socket = DE<DE<newDE< DE<Socket(serverAddr, DE<DE<51706DE<DE<);DE<

根据已经建立的Socket来创建PrintWriter,将信息通过这个对象来发送给Server,其中包含了三个部分:

DE<1DE< DE<PrintWriter out = DE<DE<newDE< DE<PrintWriter( DE<DE<newDE< DE<BufferedWriter( DE<DE<newDE< DE<OutputStreamWriter(socket.getOutputStream())),DE<DE<trueDE<DE<);DE<

以上是Android与运行在PC上的Java Server通讯的例子。来源由Anddev.org提供

2. Android与PC通讯的第二个例子:

  • PC: CSharp Server
  • Android:Java Client

通过上边的例子,本人通过一些转换,实现了Android与CSharp Server的通讯方法。原理没有太大差异,仅仅是应用的方法稍有不同。
CSharp Server 源代码:

DE<01DE< DE<TcpClient ConnectedClient = ServerListener.AcceptTcpClient(); DE<
DE<02DE< DE< DE< 
DE<03DE< DE<NetworkStream netStream = ConnectedClient.GetStream(); DE<
DE<04DE< DE< DE< 
DE<05DE< DE<BinaryReader br = DE<DE<newDE< DE<BinaryReader(netStream); DE<
DE<06DE< DE< DE< 
DE<07DE< DE<byteDE<DE<[] tempByte = DE<DE<newDE< DE<byteDE<DE<[8192]; DE<
DE<08DE< DE< DE< 
DE<09DE< DE<tempByte = br.ReadBytes(8192); DE<
DE<10DE< DE< DE< 
DE<11DE< DE<stringDE< DE<RecevieString = System.Text.Encoding.Default.GetString(tempByte);DE<

对上边的源代码做一些介绍:
通过TcpListener获得的Client信息创建一个TcpClient对象。

DE<1DE< DE<TcpClient ConnectedClient = ServerListener.AcceptTcpClient();DE<

从TcpClient对象中提取NetworkStream对象,其目的是建立Socket数据流。

DE<1DE< DE<NetworkStream netStream = ConnectedClient.GetStream();DE<

建立BinaryReader对象为刚刚创建的NetworkStream提供一个接口,借助于这个接口可以非常方便的读取信息。

DE<1DE< DE<BinaryReader br = DE<DE<newDE< DE<BinaryReader(netStream);DE<

读取Stream中的byte array数据,转化为string字符串。

DE<1DE< DE<byteDE<DE<[] tempByte = DE<DE<newDE< DE<byteDE<DE<[8192]; DE<
DE<2DE< DE< DE< 
DE<3DE< DE<tempByte = br.ReadBytes(8192); DE<
DE<4DE< DE< DE< 
DE<5DE< DE<stringDE< DE<RecevieString = System.Text.Encoding.Default.GetString(tempByte);DE<

Android Client 源代码:
这个Android Client所应用的方法与第一个例子中有很大区别,请各位特别注意(通过本人的反复验证,这个算是Java 与CSharp间可行的通讯方法)。

DE<1DE< DE<String message = DE<DE<"AndroidRes,Where is my Pig (Android)?"DE<DE<; DE<
DE<2DE< DE<Socket socket = DE<DE<newDE< DE<Socket(DE<DE<"192.168.0.149"DE<DE<, DE<DE<51706DE<DE<); DE<
DE<3DE< DE<OutputStream ops=socket.getOutputStream(); DE<
DE<4DE< DE<DataOutputStream dos=DE<DE<newDE< DE<DataOutputStream(ops); DE<
DE<5DE< DE<dos.write(message.getBytes()); DE<
DE<6DE< DE<dos.close();DE<

对上边的源代码做一些介绍:
同样是建立Socket对象。

DE<1DE< DE<Socket socket = DE<DE<newDE< DE<Socket(DE<DE<"192.168.0.149"DE<DE<, DE<DE<51706DE<DE<);DE<

根据Socket对象创建OutputStream对象,根据名称可以很容易的理解这个对象的目的,它是建立一个输出的NetworkStream。

DE<1DE< DE<OutputStream ops=socket.getOutputStream();DE<

DataOutputStream类似于BinaryWriter,是为OutputStream建立一个接口,可以通过这个接口向Socket另外一端写数据。

DE<1DE< DE<DataOutputStream dos=DE<DE<newDE< DE<DataOutputStream(ops);DE<

最后这两行代码是实施写的过程。

DE<1DE< DE<dos.write(message.getBytes()); DE<
DE<2DE< DE<dos.close();DE<

此片为转载

  评论这张
 
阅读(1332)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017