import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class khd
{
public static void main(String[] args)
{
//new kehuduan();
DatagramSocket fs=null;
DatagramSocket js=null;
Frame ck=new Frame("A聊天室");
Button an=new Button("发送");
TextArea wbk1=new TextArea(10,40);
TextArea wbk2=new TextArea(10,40);
ck.setLayout(new FlowLayout());
ck.setBounds(100,200,350,420);
ck.add(wbk1);
ck.add(wbk2);
ck.add(an);
ck.setVisible(true);
try
{
fs=new DatagramSocket();
js=new DatagramSocket(33333);
}
catch (IOException e)
{
throw new RuntimeException("端口被占用");
}
jieshou s=new jieshou(js,wbk1);
Thread t=new Thread(s);
t.start();
new kehuduan(ck,an,wbk2,fs);
}
}
class jieshou implements Runnable
{
private DatagramSocket khd;
private TextArea wbk1;
jieshou(DatagramSocket khd,TextArea wbk1)
{
this.khd=khd;
this.wbk1=wbk1;
}
public void run()
{
byte y[]=new byte[1024];
DatagramPacket bao=new DatagramPacket(y,0,y.length);
try
{
while(true)
{
khd.receive(bao);
String id=bao.getAddress().getHostAddress();
int dk=bao.getPort();
String nr=new String(bao.getData(),0,bao.getLength());
wbk1.append("IP:"+id+":"+dk+",说:"+nr);
}
}
catch (IOException a)
{
throw new RuntimeException("接收失败");
}
}
}
class kehuduan
{
private Frame ck;
private Button an;
private TextArea wbk2;
private DatagramSocket khd;
kehuduan(Frame ck,Button an,TextArea wbk2,DatagramSocket khd)
{
this.ck=ck;
this.an=an;
this.wbk2=wbk2;
this.khd=khd;
jiemian();
}
public void jiemian()
{
event();
}
private void event()//事件方法
{
ck.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){
System.exit(0);
}});
an.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
fs();
}});
}
private void fs()//发送按钮点击后 聊天窗口发生的事情
{
try
{
String ls=null;
BufferedReader buf=new BufferedReader(new StringReader(wbk2.getText()));
wbk2.setText("");
while ((ls=buf.readLine())!=null)//把文本框2的内容发送到对方主机
{
try
{
byte lin[]=ls.getBytes();
DatagramPacket bao=new DatagramPacket(lin,lin.length,InetAddress.getByName("127.0.0.1"),12345);
khd.send(bao);
}
catch (IOException p)
{
throw new RuntimeException("发送失败");
}
finally
{
try
{
buf.close();
}
catch (IOException c)
{
throw new RuntimeException("关闭失败");
}
}
}
}
catch (IOException a)
{
throw new RuntimeException("读取失败");
}
}
}
运行后一点发送就报这样的异常
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: 读取失败
at kehuduan.fs(khd.java:142)
at kehuduan.access$000(khd.java:73)
at kehuduan$2.actionPerformed(khd.java:99)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我真找不到 代码哪里错了 求指导~~~~~~~!!!!!!!!!!!!!!!!:Q:Q:Q要疯了
|
|