package com.mutouyihao; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; /** * udp通信所需类:DatagramSocket、DatagramPacket * * DatagramSocket:创建接受发送数据包的socket,接受方法:public DatagramSocket(int receviePort) throws SocketException * 发送数据包方法:public DatagramSocket() throws SocketException * 所有的端口、目的地址和数据需要有DatagramPacket指定 * 接受数据方法:receive(DatagramPacket data),获取的数据放在data * 发送方法:send(DatagramPacket data),发送的端口、目的地址和数据都在data中 * * DatagramPacket:数据报的载体,接收数据创建方法:public DatagramPacket(byte []buf, int length),buf存放数据,length接收最大长度 * 发送数据报创建方法:public DatagramPacket(byte[] buf, int length, InetAddress addr目的地址, int port目的端口) * public byte[] getData():获取存放在数据报中的数据 * public int getLength(): 获取数据的长度 * public InetAddress getAddress():获取数据报中 的ip * public int getPort():获取数据报中端口号 * public void steData(byte []buf):设置数据报中内容为buf中存储的内容 */ public class UDP implements Runnable { Thread thread = null; DatagramPacket sendPack, receivePack; DatagramSocket sendSocket, receiveSocket; InetAddress sendIp; int sendPort, receivePort; byte inBuf[], outBuf[]; static final int BUFSIZE = 1024; public UDP() { try { sendSocket = new DatagramSocket(); sendIp = InetAddress.getByName("127.0.0.1"); } catch (SocketException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } catch (UnknownHostException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } sendPort = 6000; } @Override public void run() { String msgStr; while (true) { try { receiveSocket.receive(receivePack); msgStr = new String(receivePack.getData(), 0, receivePack.getLength()); System.out.println("recevied msg: " + msgStr); } catch (Exception e) { // TODO: handle exception } } } public void sendMsg(String msg) { outBuf = msg.getBytes(); // compose the data packet to send sendPack = new DatagramPacket(outBuf, outBuf.length, sendIp, sendPort); try { sendSocket.send(sendPack); } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } System.out.println("202 send content to " + sendIp + " with " + msg); } private void readRecv() { try { receivePort = 7000; inBuf = new byte[BUFSIZE]; receivePack = new DatagramPacket(inBuf, BUFSIZE); receiveSocket=new DatagramSocket(receivePort); thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } catch (SocketException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } public static void main(String[] args) { UDP one = new UDP(); one.readRecv(); while (true) { System.out.println("输入发送信息:"); Scanner s = new Scanner(System.in); String msg = s.nextLine(); one.sendMsg(msg); } } } package com.mutouyihao; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; public class UDPTest implements Runnable { Thread thread = null; DatagramPacket sendPack, receivePack; DatagramSocket sendSocket, receiveSocket; InetAddress sendIp; int sendPort, receivePort; byte inBuf[], outBuf[]; static final int BUFSIZE = 1024; public UDPTest() { try { sendSocket = new DatagramSocket(); sendIp = InetAddress.getByName("127.0.0.1"); } catch (SocketException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } catch (UnknownHostException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } sendPort = 7000; } @Override public void run() { String msgStr; while (true) { try { receiveSocket.receive(receivePack); msgStr = new String(receivePack.getData(), 0, receivePack.getLength()); System.out.println("recevied msg: " + msgStr); } catch (Exception e) { // TODO: handle exception } } } public void sendMsg(String msg) { outBuf = msg.getBytes(); // compose the data packet to send sendPack = new DatagramPacket(outBuf, outBuf.length, sendIp, sendPort); try { sendSocket.send(sendPack); } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } System.out.println("202 send content to " + sendIp + " with " + msg); } private void readRecv() { try { receivePort = 6000; inBuf = new byte[BUFSIZE]; receivePack = new DatagramPacket(inBuf, BUFSIZE); receiveSocket=new DatagramSocket(receivePort); thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } catch (SocketException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } public static void main(String[] args) { UDPTest one = new UDPTest(); one.readRecv(); while (true) { System.out.println("输入发送信息:"); Scanner s = new Scanner(System.in); String msg = s.nextLine(); one.sendMsg(msg); } } } 两个java一块开着对行