欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Socket编程实例(二)- UDP基本使用

程序员文章站 2024-03-12 13:31:26
一.服务端代码: import java.io.*; import java.net.*; public class udpechoserver {...

一.服务端代码:

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 中文参考手册》,也希望大家多多支持。