package cn.net06;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class ClientB {
public static void main(String[] args) throws IOException {
// 创建一个datagram socket用于结束clientA 传入的数据
DatagramSocket ds = new DatagramSocket(9000);
while (true) {
// System.out.println("正在接收信息...");
// --receive msg from clinetA--
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
// 从dp中获取接收的内容
String msg = new String(buf, 0, dp.getLength());
System.out.println("clientA说:" + msg);
// --send msg to clientA--
Scanner sc = new Scanner(System.in);
String sendMsg = sc.nextLine();
byte[] sendBuf = sendMsg.getBytes();
InetAddress toIp = InetAddress.getLocalHost();
DatagramPacket sendPd = new DatagramPacket(sendBuf, sendBuf.length, toIp, 8000);
// DatagramSocket sendDs = new DatagramSocket(9000);
ds.send(sendPd);
}
}
}
|