当前位置: 代码迷 >> Android >> Android的TCP跟UDP传输 简单程序
  详细解决方案

Android的TCP跟UDP传输 简单程序

热度:50   发布时间:2016-04-28 02:22:05.0
Android的TCP和UDP传输 简单程序

TCP UDP时常用的网络通信方式,具体内容及传输方式可以百度搜索这里不再赘述,

我做的主要是把源码发出来,给大家参考。

首先,TCP建立连接之后,通信双方都同时可以进行数据的传输,其次,他是全双工的;在保证可靠性上,采用超时重传和捎带确认机制。

常用的tcp连接图

服务器端代码

    try {                  Boolean endFlag = false;                  ServerSocket ss = new ServerSocket(12345);                  while (!endFlag) {                      // 等待客户端连接                      Socket s = ss.accept();                      BufferedReader input = new BufferedReader(newInputStreamReader(s.getInputStream()));                      //注意第二个参数据为true将会自动flush,否则需要需要手动操作output.flush()                      PrintWriter output = newPrintWriter(s.getOutputStream(),true);                      String message = input.readLine();                      Log.d("Tcp Demo", "message from Client:"+message);                      output.println("message received!");                      //output.flush();                      if("shutDown".equals(message)){                          endFlag=true;                      }                      s.close();                  }                  ss.close();                     } catch (UnknownHostException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              } 
客户端代码:


    try {                  Socket s = new Socket("localhost", 12345);                  // outgoing stream redirect to socket                  OutputStream out = s.getOutputStream();                  // 注意第二个参数据为true将会自动flush,否则需要需要手动操作out.flush()                  PrintWriter output = new PrintWriter(out, true);                  output.println("Hello IdeasAndroid!");                  BufferedReader input = new BufferedReader(newInputStreamReader(s                          .getInputStream()));                  // read line(s)                  String message = input.readLine();                  Log.d("Tcp Demo", "message From Server:" + message);                  s.close();                     } catch (UnknownHostException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              } 
UDP连接图

UDP服务器端代码:

    // UDP服务器监听的端口              Integer port = 12345;              // 接收的字节大小,客户端发送的数据不能超过这个大小              byte[] message = new byte[1024];              try {                  // 建立Socket连接                  DatagramSocket datagramSocket = new DatagramSocket(port);                  DatagramPacket datagramPacket = new DatagramPacket(message,                          message.length);                  try {                      while (true) {                          // 准备接收数据                          datagramSocket.receive(datagramPacket);                          Log.d("UDP Demo", datagramPacket.getAddress()                                  .getHostAddress().toString()                                  + ":" + new String(datagramPacket.getData()));                      }                  } catch (IOException e) {                      e.printStackTrace();                  }              } catch (SocketException e) {                  e.printStackTrace();              } 

客户端代码:

    public static void send(String message) {              message = (message == null ? "Hello IdeasAndroid!" : message);              int server_port = 12345;              DatagramSocket s = null;              try {                  s = new DatagramSocket();              } catch (SocketException e) {                  e.printStackTrace();              }              InetAddress local = null;              try {                  // 换成服务器端IP                  local = InetAddress.getByName("localhost");              } catch (UnknownHostException e) {                  e.printStackTrace();              }              int msg_length = message.length();              byte[] messagemessageByte = message.getBytes();              DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,                      server_port);              try {                  s.send(p);              } catch (IOException e) {                  e.printStackTrace();              }          } 



  相关解决方案