//我这么写是因为都是在127.0.0.1这一台机器上所以出现了冲突所以无法运行的还是为什么?
//还是根本就不能这么写?我是想实现一下dos的聊天,用一台电脑能实现么?如果ok应该是什么样的?用UDP的
import java.net.*;
import java.io.*;
class er
{
public static void main(String[] args)throws Exception
{
jieshou();
fasong();
}
public static void jieshou()throws Exception
{
DatagramSocket ds=new DatagramSocket(10001);
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());
int port=dp.getPort();
System.out.println(ip+"::::"+data+":::::"+port);
}
}
public static void fasong()throws Exception
{
DatagramSocket ds=new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);
ds.send(dp);
}
}
}
class yi
{
public static void main(String[] args)throws Exception
{
jieshou();
fasong();
}
public static void jieshou()throws Exception
{
DatagramSocket ds=new DatagramSocket(10002);
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());
int port=dp.getPort();
System.out.println(ip+"::::"+data+":::::"+port);
}
}
public static void fasong()throws Exception
{
DatagramSocket ds=new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10001);
ds.send(dp);
}
}
}
|