程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Socket編程實例(二)- UDP根本應用

Java Socket編程實例(二)- UDP根本應用

編輯:關於JAVA

Java Socket編程實例(二)- UDP根本應用。本站提示廣大學習愛好者:(Java Socket編程實例(二)- UDP根本應用)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Socket編程實例(二)- UDP根本應用正文


一.辦事端代碼:

import java.io.*; 
import java.net.*; 
 
public class UDPEchoServer { 
 
  private static final int ECHOMAX = 255; // Maximum size of echo datagram 
 
  public static void main(String[] args) throws IOException { 
 
    int servPort = 5500; // Server port 
 
    DatagramSocket socket = new DatagramSocket(servPort); 
    DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX); 
 
    while (true) { // Run forever, receiving and echoing datagrams 
      socket.receive(packet); // Receive packet from client 
      System.out.println("Handling client at " + packet.getAddress().getHostAddress() + " on port " + packet.getPort()); 
      socket.send(packet); // Send the same packet back to client 
      packet.setLength(ECHOMAX); // Reset length to avoid shrinking buffer 
    } 
    /* NOT REACHED */ 
  } 
} 

二.客戶端代碼:

import java.net.*; 
import java.io.*; 
 
public class UDPEchoClientTimeout { 
 
  private static final int TIMEOUT = 3000; // Resend timeout (milliseconds) 
  private static final int MAXTRIES = 5; // Maximum retransmissions 
 
  public static void main(String[] args) throws IOException { 
    InetAddress serverAddress = InetAddress.getByName("127.0.0.1"); // Server address 
    int servPort = 5500; // Server port 
    // Convert the argument String to bytes using the default encoding 
    byte[] bytesToSend = "Hi, World".getBytes(); 
 
    DatagramSocket socket = new DatagramSocket(); 
    socket.setSoTimeout(TIMEOUT); // Maximum receive blocking time(milliseconds) 
    // Sending packet 
    DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length, serverAddress, servPort); 
 
    DatagramPacket receivePacket = // Receiving packet 
    new DatagramPacket(new byte[bytesToSend.length], bytesToSend.length); 
 
    int tries = 0; // Packets may be lost, so we have to keep trying 
    boolean receivedResponse = false; 
    do { 
      socket.send(sendPacket); // Send the echo string 
      try { 
        socket.receive(receivePacket); // Attempt echo reply reception 
 
        if (!receivePacket.getAddress().equals(serverAddress)) {// Check 
                                    // source 
          throw new IOException("Received packet from an unknown source"); 
        } 
        receivedResponse = true; 
      } catch (InterruptedIOException e) { // We did not get anything 
        tries += 1; 
        System.out.println("Timed out, " + (MAXTRIES - tries) + " more tries..."); 
      } 
    } while ((!receivedResponse) && (tries < MAXTRIES)); 
 
    if (receivedResponse) { 
      System.out.println("Received: " + new String(receivePacket.getData())); 
    } else { 
      System.out.println("No response -- giving up."); 
    } 
    socket.close(); 
  }
} 

上述代碼的UDP辦事端是單線程,一次只能辦事一個客戶端。

以上就是本文的全體內容,檢查更多Java的語法,年夜家可以存眷:《Thinking in Java 中文手冊》、《JDK 1.7 參考手冊官方英文版》、《JDK 1.6 API java 中文參考手冊》、《JDK 1.5 API java 中文參考手冊》,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved