请求高人帮帮忙!谢谢!!

songcan 2006-02-07 06:41:22
两个程序之间不能互发消息,想知道为什么不行?
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class Net extends JFrame implements ActionListener{
JTextArea txtDisplay;
JTextArea txtSend;
JButton btnClose;
JButton btnSend;
public static void main(String args[]){
final Net f=new Net("我de聊天室1");

try{
Thread.currentThread().sleep(10);
}
catch(InterruptedException ee){
ee.printStackTrace();
}
new Thread(new Runnable(){ public void run(){
try{
while(true){
DatagramSocket dsn= new DatagramSocket(3000);
byte buff[]=new byte[1024];
DatagramPacket dpn= new DatagramPacket(buff,1024);
dsn.receive(dpn);
String recv=new String(dpn.getData(),0,dpn.getLength());
f.txtDisplay.setText(recv);
dsn.close();
}
}
catch(Exception g){
g.printStackTrace();
}
}}).start();
f.setSize(300,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);

}
public Net(String s){
super(s);
Container content=getContentPane();
content.setLayout(new BorderLayout());
JPanel ptitle= new JPanel();
JPanel pcenter= new JPanel();
JPanel pend= new JPanel();
JPanel p= new JPanel();
JScrollPane pane= new JScrollPane();
JScrollPane pane1= new JScrollPane();
txtDisplay= new JTextArea(12,20);
txtSend= new JTextArea(5,20);
pane.getViewport().add(txtDisplay);
pane1.getViewport().add(txtSend);
btnClose= new JButton("关闭");
btnSend= new JButton("发送");
btnSend.addActionListener(this);
btnClose.addActionListener(this);
txtDisplay.setEditable(false);
ptitle.add(pane);
pcenter.add(pane1);
p.add(btnSend);
p.add(btnClose);
pend.add(p);
content.add(ptitle,BorderLayout.NORTH);
content.add(pcenter,BorderLayout.CENTER);
content.add(pend,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("关闭")){
// System.out.print("Hello");
this.dispose();
System.exit(0);

}
else{
try{
DatagramSocket ds= new DatagramSocket();
String str=txtSend.getText();
DatagramPacket dp= new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getLocalHost(),8000);
// System.out.print(InetAddress.getLocalHost().getHostAddress());
ds.send(dp);
ds.close();
}
catch(Exception g){
g.printStackTrace();
}

}

}
}




import java.awt.*;
import javax.swing.*;
import java.net.*;
public class UdpRecv extends JFrame implements ActionListener{
JTextArea txtDisplay;
JTextArea txtSend;
JButton btnClose;
JButton btnSend;
public static void main(String args[]){
final Net f=new Net("我de聊天室2");

try{
Thread.currentThread().sleep(10);
}
catch(InterruptedException ee){
ee.printStackTrace();
}
new Thread(new Runnable(){ public void run(){
try{
while(true){
DatagramSocket dsn= new DatagramSocket(8000);
byte buff[]=new byte[1024];
DatagramPacket dpn= new DatagramPacket(buff,1024);
dsn.receive(dpn);
String recv=new String(dpn.getData(),0,dpn.getLength());
f.txtDisplay.setText(recv);
dsn.close();
}
}
catch(Exception g){
g.printStackTrace();
}
}}).start();
f.setSize(300,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);

}
public UdpRecv(String s){
super(s);
Container content=getContentPane();
content.setLayout(new BorderLayout());
JPanel ptitle= new JPanel();
JPanel pcenter= new JPanel();
JPanel pend= new JPanel();
JPanel p= new JPanel();
JScrollPane pane= new JScrollPane();
JScrollPane pane1= new JScrollPane();
txtDisplay= new JTextArea(12,20);
txtSend= new JTextArea(5,20);
pane.getViewport().add(txtDisplay);
pane1.getViewport().add(txtSend);
btnClose= new JButton("关闭");
btnSend= new JButton("发送");
btnSend.addActionListener(this);
btnClose.addActionListener(this);
txtDisplay.setEditable(false);
ptitle.add(pane);
pcenter.add(pane1);
p.add(btnSend);
p.add(btnClose);
pend.add(p);
content.add(ptitle,BorderLayout.NORTH);
content.add(pcenter,BorderLayout.CENTER);
content.add(pend,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("关闭")){
// System.out.print("Hello");
this.dispose();
System.exit(0);

}
else{
try{
DatagramSocket ds= new DatagramSocket();
String str=txtSend.getText();
DatagramPacket dp= new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getLocalHost(),3000);
// System.out.print(InetAddress.getLocalHost().getHostAddress());
ds.send(dp);
ds.close();
}
catch(Exception g){
g.printStackTrace();
}

}

}
}
...全文
51 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧