Android TCP协议与UDP协议的实现

来源:互联网 发布:mac谜案处小说百度云 编辑:程序博客网 时间:2024/05/21 10:34

TCP协议:

安卓真机与PC之间可以通过ServerSocket 与 Socket进行通信,使用的是TCP/IP协议。(TCP对网络要求很高,具有可靠的安全性)

服务器:public class Server {  public static void main(String[] args) {new Server().Server(9090);}  public void Server(int port){   try{ ServerSocket server=new ServerSocket(port); System.out.println("服务器已经启动。。。。等待客户端连接"); Socket connect=server.accept(); System.out.println("客户端连上,IP:"+connect.getInetAddress());  }catch (Exception e) {        e.printStackTrace();}    }}
客户端:
                        final EditText ed2=(EditText)rootView.findViewById(R.id.login_user);final EditText ed1=(EditText)rootView.findViewById(R.id.login_pwd);Button login_button=(Button)rootView.findViewById(R.id.login_button);//文件流 -------TCP-----//File file=new File("/storage/sdcard/first.txt");try{//读文件FileInputStream fls=new FileInputStream(file);BufferedInputStream bis=new BufferedInputStream(fls);byte[] bytes=new byte[bis.available()];bis.read(bytes);String st=new String(bytes);int i=st.indexOf("a");String user=st.substring(0,i);String pwd=st.substring(i+1);ed2.setText(user);ed1.setText(pwd);}catch(Exception e){e.printStackTrace();}login_button.setOnClickListener(new OnClickListener() {public void onClick(View v) {String user=ed2.getText().toString();String pwd=ed1.getText().toString();if("1".equals(user)&&"1".equals(pwd)){File file=new File("/storage/sdcard/first.txt");   if(!file.exists())try{   file.createNewFile();   }catch (Exception e) {   e.printStackTrace();} try{ FileOutputStream fos=new FileOutputStream(file); String st1=ed2.getText().toString()+"a"+ed1.getText().toString(); byte[] byte1=st1.getBytes(); fos.write(byte1); } catch(Exception e){ e.printStackTrace(); } 
服务器连接成功,真机就可以和服务器交互,发送信息。
<img src="http://img.blog.csdn.net/20160708004000066?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

UDP协议:是一种不安全的协议,在电脑QQ上可以找到,在网络良好的情况下,会使用TCP,网络差的情况会自动切换成UDP,但容易丢失信息。   



 客户端:   public void testClient(){          try{              DatagramSocket ds = new DatagramSocket();              System.out.println("客户端socket已经创建完成...");              byte[] buf = "a a a a   a a a a".getBytes();              String host = "192.168.215.1";              InetAddress ia = InetAddress.getByName(host);              DatagramPacket dp = new DatagramPacket(buf,buf.length,ia,9090);              System.out.println("客户端数据包创建完成...");              ds.send(dp);              System.out.println("客户端数据包已经发送给服务器");              ds.close();              System.out.println("客户端socket已经关闭...");          }catch(Exception e){              e.printStackTrace();          }      }  
服务器:
    public void CreateServer(){          try{              DatagramSocket ds = new DatagramSocket(9090);              System.out.println("服务端连接已经建立...");              byte[] buf = new byte[16];              DatagramPacket dp = new DatagramPacket(buf,buf.length);              System.out.println("创建数据包完成...");              ds.receive(dp);              System.out.println("已经接收数据包...");              String data = new String(dp.getData());              String host = dp.getAddress().getHostAddress();              int port = dp.getPort();              System.out.println("data:\t"+data+"  host:\t"+host+"  port:\t"+port);              ds.close();          }catch(Exception e){              e.printStackTrace();          }      }UDP实现的效果图: 


总结:

   自己对TCP和UDP的了解还不是太深刻,什么情况,具体怎么去用还是不太清晰,以后还需要进行深刻的认知与了解。

  希望此博客能帮助到遇到问题的朋友!

1 0
原创粉丝点击