日期:2014-05-20 浏览次数:20845 次
//服务器端 import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class TestServer { public static void main(String[] args) throws IOException { DatagramSocket chatUDPSocket = new DatagramSocket(8888); while(true) { byte[] recvBuf = new byte[100]; DatagramPacket recvPacket = new DatagramPacket(recvBuf , recvBuf.length); chatUDPSocket.receive(recvPacket); System.out.println(recvPacket.getSocketAddress().toString()); } } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class TestClient { public static void main(String[] args) throws IOException { DatagramSocket clienttoclient = new DatagramSocket(); String sendStr = "hello"; byte[] sendBuf = sendStr.getBytes(); InetAddress addr = InetAddress.getByName("125.34.69.243"); int port = 8888; DatagramPacket sendPacket = new DatagramPacket(sendBuf ,sendBuf.length , addr , port); clienttoclient.send(sendPacket); new ReceiveMessages(clienttoclient); System.out.println("输入要联系的IP地址"); Scanner in = new Scanner(System.in); String userIP = in.next(); System.out.println("输入要联系的端口号"); int userPort = in.nextInt(); byte[] chatsendBuf = "hello".getBytes(); InetAddress chatAddr = InetAddress.getByName(userIP); DatagramPacket chatsendPacket = new DatagramPacket(chatsendBuf,chatsendBuf.length,chatAddr,userPort); clienttoclient.send(chatsendPacket); System.out.println("test"); } } class ReceiveMessages extends Thread { private DatagramSocket clienttoclient; public ReceiveMessages(DatagramSocket clienttoclient) { this.clienttoclient = clienttoclient; this.start(); } public void run() { System.out.println("启动监听客户端接收客户端发送的消息"); byte[] recvBuf = new byte[100]; DatagramPacket recvPacket = new DatagramPacket(recvBuf , recvBuf.length); try { clienttoclient.receive(recvPacket); } catch (IOException e) { e.printStackTrace(); } String recvStr = new String(recvPacket.getData() , 0 ,recvPacket.getLength()); System.out.println("收到:" + recvStr); } }