Android的TCP和UDP傳輸 簡單程式

來源:互聯網
上載者:User

標籤:tcp   udp   socket   android 安卓   

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();              }          } 



Android的TCP和UDP傳輸 簡單程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.