客户端代码:
package cn.luoxue.chat1; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPClient { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); //定义服务器的地址,端口号,数据 InetAddress address = InetAddress.getByName("localhost"); int port = 8800; DatagramPacket packet; DatagramPacket packet2; //创建DatagramSocket,实现数据发送和接收 DatagramSocket socket = new DatagramSocket(); System.out.println("您好,美女小兰为您服务!"); String str = ""; while(!str.equals("886")) { System.out.print("用户说:"); str = input.next(); byte[] data = str.getBytes(); //创建数据报 packet = new DatagramPacket(data, data.length, address, port); //向服务器端发送数据报 socket.send(packet); //接收服务器响应数据 byte[] data2 = new byte[1024]; packet2 = new DatagramPacket(data2, data2.length); socket.receive(packet2); String info = new String(data2, 0, packet2.getLength()); System.out.println("客服说:"+info); } socket.close(); } }
服务端:
package cn.luoxue.chat1; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPServer { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(8800); DatagramPacket packet = null; byte[] data = null; int count = 0; System.out.println("***客服静候您的咨询***"); while(true){ data = new byte[1024];//创建字节数组,指定接收的数据包的大小 packet = new DatagramPacket(data, data.length); socket.receive(packet);//此方法在接收到数据报之前会一直阻塞 Thread thread = new Thread(new UDPThread(socket, packet)); thread.start(); count++; System.out.println("服务器端被连接过的次数:"+count); InetAddress address = packet.getAddress(); System.out.println("当前客户端的IP为:"+address.getHostAddress()); } } }
多线程代码:
package cn.luoxue.chat1; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPThread implements Runnable{ DatagramSocket socket = null; DatagramPacket packet = null; public UDPThread(DatagramSocket socket,DatagramPacket packet) { this.socket = socket; this.packet = packet; } @Override public void run() { String info = null; InetAddress address = null; int port = 8800; byte[] data2 = null; DatagramPacket packet2 = null; try { info = new String(packet.getData(), 0, packet.getLength()); System.out.println("我是服务器,客户端说:"+info); address = packet.getAddress(); port = packet.getPort(); if(info.equals("886")){ data2 = "好的,回聊".getBytes(); }else if(info.equals("美女你好问个问题")){ data2 = "先生,你想问什么呢".getBytes(); }else if(info.equals("你认为谁最帅")){ data2 = "那还用说,当然是小军了,人家小军玉树临风、一表人才,简直帅的掉渣!".getBytes(); }else if(info.equals("那你认为谁最美")){ data2 = "毫无争议绝对是小举,人家小举闭月羞花、天生丽质,美的不要不要的!".getBytes(); }else if(info.equals("谁俩更般配呢")){ data2 = "你懂的".getBytes(); }else{ data2 = "你说啥?风大听不到!".getBytes(); } packet2 = new DatagramPacket(data2, data2.length, address, port); socket.send(packet2); } catch (IOException e) { e.printStackTrace(); } //socket.close();不能关闭 } }
代码参考自:http://www.cnblogs.com/zhaozihan/archive/2016/11/12/6057118.html