Java中UDP的簡單使用
阿新 • • 發佈:2019-01-29
服務端:
/** * Created by cuboo on 2016/10/12. */ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class udpserver { private static DatagramSocket socket; private DatagramPacket packet; private static int port = 8001; public static void main(String[] args) { udpserver server = new udpserver(); try { socket = new DatagramSocket(port); while (true){ DatagramPacket packet = server.receiveMessage(socket); server.sendMessage(socket,packet); } } catch (SocketException e) { e.printStackTrace(); } } public void sendMessage(DatagramSocket socket,DatagramPacket receive_packet){ try { byte[] datas = "hello this is udpserver echo".getBytes(); //receive_packet接受到的客服端的ip地址和埠 packet = new DatagramPacket(datas,datas.length,receive_packet.getAddress(),receive_packet.getPort()); socket.send(packet); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public DatagramPacket receiveMessage(DatagramSocket socket){ try { byte[] data = new byte[100]; packet = new DatagramPacket(data,data.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); } catch (IOException e) { e.printStackTrace(); } //返回接受到的客服端的ip地址和埠 return packet; } }
客戶端:
import java.io.IOException; import java.net.*; /** * Created by cuboo on 2016/10/12. */ public class udpclient { private static DatagramSocket socket; private DatagramPacket packet; private int port = 8001; public static void main(String agrs[]){ udpclient client = new udpclient(); try { socket = new DatagramSocket(); while (true){ client.sendMessage(); client.receivMessage(); } } catch (SocketException e) { e.printStackTrace(); } } public void sendMessage(){ try { byte[] datas = "hello this is client echo".getBytes(); packet = new DatagramPacket(datas,datas.length, InetAddress.getLocalHost(),port); socket.send(packet); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void receivMessage(){ try { byte[] data = new byte[100]; packet = new DatagramPacket(data,data.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); } catch (IOException e) { e.printStackTrace(); } } }