请求高人帮帮忙!谢谢!!
两个程序之间不能互发消息,想知道为什么不行?
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();
}
}
}
}