package NetAdressDC;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class liaotianshi2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("kasiihi");
DatagramSocket ds = new DatagramSocket(6000);
DatagramSocket ds1 = new DatagramSocket(7000);
new Thread(new send2(ds)).start();
new Thread(new rece2(ds1)).start();
}
}
class send2 implements Runnable {
private DatagramSocket ds;
public send2(DatagramSocket ds) {
super();
this.ds = ds;
}
public void run() {
// TODO Auto-generated method stub
System.out.println("聊天开始");
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str=bufr.readLine())!=null)
{
if("over".equals(str))
{break;}
byte[] by = str.getBytes();
//System.out.println(str);
DatagramPacket dg = new DatagramPacket(by, by.length,InetAddress.getByName("172.24.118.1"),7000);
ds.send(dg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class rece2 implements Runnable {
private DatagramSocket ds1;
public rece2(DatagramSocket ds1) {
super();
this.ds1 = ds1;
}
public void run() {
// TODO Auto-generated method stub
while(true){
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
try {
ds1.receive(dp);
String ip = dp.getAddress().getHostAddress();
String text = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+text);
if("88".equals(text)){
System.out.println(ip+".....离开聊天室!!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我是想在一台电脑上开几个控制台模拟进行多台电脑进行局域网之间的交流,但是不行,感觉应该是端口设置或者ip地址的写法有问题,求大神解,应该怎么写才能实现