本帖最后由 杨剑 于 2013-3-4 20:21 编辑
import java.io.*;
import java.net.*;
class Send implements Runnable{
private DatagramSocket ds;
Send(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=br.readLine())!=null){
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.121"),10003);
ds.send(dp);
if("886".equals(line))
break;
}
}catch(Exception e){
throw new RuntimeException("发送端失败");
}
}
}
class Rece implements Runnable{
private DatagramSocket ds;
Rece(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
while(true){
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
if("886".equals(data))
break;
}
}catch(Exception e){
throw new RuntimeException("接收端失败");
}
}
}
class ChatDemo{
public static void main(String[] args) throws Exception{
DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket(10003);
Send s = new Send(ds1);
Rece r = new Rece(ds2);
Thread t1 = new Thread(s);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
为什么我写的这个聊天程序不显示出现的内容呢?原因出在哪里呢,请大家看看
|
|