public class SendThread implements Runnable{
private DatagramSocket ds;
public SendThread(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while((line = br.readLine()) != null){
if("22".equals(line)){
break;
}
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.255"), 12580);
ds.send(dp);
}
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class SendThread implements Runnable{
private DatagramSocket ds;
public SendThread(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while((line = br.readLine()) != null){
if("over".equals(line)){
break;
}
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.255"), 12580); // 这里用"192.168.1.255"在本机实现聊天是不 是还要进行其他的配置啊? 我用本机IP地址试是没有问题的.
ds.send(dp);
}
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ChatDemo {
public static void main(String[] args) throws IOException {
DatagramSocket sds = new DatagramSocket();
DatagramSocket rds = new DatagramSocket(12580);
SendThread st = new SendThread(sds);
ReceiveThread rt = new ReceiveThread(rds);
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
t1.start();
t2.start();
}
}
运行结果:
当IP设置为本机地址192.168.0.100 时
dfd
192.168.0.100 : dfd
df
192.168.0.100 : df
当IP设置为 192.168.1.255 时
dfd
df
用"192.168.1.255"在本机实现聊天是不是还要进行其他的配置啊?
|
|