您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 爱华对Android Socket 传输的理解
1AndroidSocketTransferenceR&D——爱华个人项目中遇到的情况。。Index1,理论.............................................................................................................................11,PCtoPC.....................................................................................................................32,AndroidToPC.........................................................................................................53AndroidtoAndroid................................................................................................9Android开发入门——Socket编程..........................................................................15浅谈JavaSocket编程及应用......................................................................................191,理论从来没有碰过这些玩意,或者就是一无所知,结果今天花了半天,看了两个列子,立马明白了。。看来学习程序,从应用开始,再回归理论,再应用。。首先,socket和serverSocket两个封装完美的类,剩下的就是简单的I/O流操作。。Client与server都是相对的。。如果两者互发,那么可以说两者都兼两个角色。。我们就举最简单的例子:单发,clientServer.那么单发,clientServer.client端server端具体怎么mapping呢?如下:很多代码类似ClientsideServersideFieldimportjava.net.Socket;staticSocketclient;importjava.net.ServerSocket;importjava.net.Socket;建立sideclient=newSocket(127.0.0.1,8800);\客户端要写清服务端的ip和客户端portServerSocketserver=newServerSocket(8800);\服务端只要客户端port拿到对方对象Socketclient=server.accept();一旦客户端发送请求,服务端通过accept就完全得到客户端的client.如何发消息给对方1,\建立向server消息的工具PrintWriterout=newPrintWriter(client.getOutputStream());\建立向server的消息内容——申请输入操作1,\建立向clientside发消息的工具PrintWriterout=newPrintWriter(client.getOutputStream());2BufferedReaderwt=newBufferedReader(newInputStreamReader(System.in));2,while(true){Stringstr=wt.readLine();out.println(str);\这个就是发送消息出去out.flush();\这个就是刷新}2,while(true){out.println(“serversidereceived”);\这个就是发送消息出去out.flush();\这个就是刷新}如何接收对方消息1,\建立接受server消息的工具BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));2,\打印接受server的外部消息System.out.println(in.readLine());//Readtheserver-side,writetotheclient“hasreceiving”1,\建立接受clientside消息的工具BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));2,\打印接受server的外部消息while(true){Stringstr=in.readLine();System.out.println(str);//Readclient,towriteserver-sideif(str.equals(end))break;后事操作client.close();client.close();双发client–server端:(与单发非常相似)ClientsideServersideFieldimportjava.net.Socket;staticSocketclient;importjava.net.ServerSocket;importjava.net.Socket;建立sideclient=newSocket(127.0.0.1,8800);\客户端要写清服务端的ip和客户端port程序执行这一部,就等于向本机的4700端口(Server)发出客户请求ServerSocketserver=newServerSocket(8800);\服务端只要客户端port拿到对方对象Socketclient=server.accept();一旦客户端发送请求,服务端通过accept就完全得到客户端的client.如何发消息给对方1,\建立向server消息的工具PrintWriterout=newPrintWriter(client.getOutputStream());\建立向server的消息内容——申请输入操作BufferedReadersysIn=newBufferedReader(newInputStreamReader(System.in));2,while(true){Stringstr=sysIn.readLine();out.println(str);\这个就是发送消息出去out.flush();\这个就是刷新}1,\建立向clientside发消息的工具PrintWriterout=newPrintWriter(client.getOutputStream());//由系统标准输入设备构造BufferedReader对象BufferedReadersin=newBufferedReader(newInputStreamReader(System.in));32,line=sin.readLine();//从标准输入读入一字符串while(!line.equals(bye)){//如果该字符串为bye,则停止循环os.println(line);//向客户端输出该字符串os.flush();//刷新输出流,使Client马上收到该字符串line=sin.readLine();}如何接收对方消息1,\建立接受server消息的工具BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));2,\打印接受server的外部消息System.out.println(in.readLine());//Readtheserver-side,writetotheclient“hasreceiving”1,\建立接受clientside消息的工具BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));2,\打印接受server的外部消息while(true){Stringstr=in.readLine();System.out.println(str);//Readclient,towriteserver-sideif(str.equals(end))break;后事操作out.close();//关闭Socket输出流in.close();//关闭Socket输入流client.close();//关闭Socketos.close();//关闭Socket输出流is.close();//关闭Socket输入流socket.close();//关闭Socketserver.close();//关闭ServerSocket分为以下几种:ClientServer1,PCtoPC2,PCtoAndroid3,AndroidtoPC4,AndroidtoAndroid1,PCtoPC,代码如下:PC-Client:importjava.io.*;importjava.net.Socket;publicclassTalkClient{publicstaticvoidmain(String[]args){try{Socketsocket=newSocket(127.0.0.1,4700);//向本机的4700端口发出客户请求sendclientrequesttoport4700BufferedReadersin=newBufferedReader(newInputStreamReader(System.in));4//由系统标准输入设备构造BufferedReader对象PrintWriteros=newPrintWriter(socket.getOutputStream());//由Socket对象得到输出流,并构造PrintWriter对象BufferedReaderis=newBufferedReader(newInputStreamReader(socket.getInputStream()));//由Socket对象得到输入流,并构造相应的BufferedReader对象Stringreadline;readline=sin.readLine();//从系统标准输入读入一字符串while(!readline.equals(bye)){//若从标准输入读入的字符串为bye则停止循环os.println(readline);//将从系统标准输入读入的字符串输出到Serveros.flush();//刷新输出流,使Server马上收到该字符串System.out.println(inTalkeClient,Client:+readline);//在系统标准输出上打印读入的字符串System.out.println(inTalkeClient,Server:+is.readLine());//从Server读入一字符串,并打印到标准输出上readline=sin.readLine();//从系统标准输入读入一字符串}//继续循环os.close();//关闭Socket输出流is.close();//关闭Socket输入流socket.close();//关闭Socket}catch(Exceptione){//TODO:handleexception}}}PC–Server:importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava
本文标题:爱华对Android Socket 传输的理解
链接地址:https://www.777doc.com/doc-1910724 .html