//Shanghai.javaimportjava.net.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassShanghai{publicstaticvoidmain(Stringargs[]){ShanghaiFrameshanghaiWi...
//Shanghai.java
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shanghai {
public static void main(String args[]) {
ShanghaiFrame shanghaiWin=new ShanghaiFrame();
}
}
class ShanghaiFrame extends JFrame implements Runnable,ActionListener
{ JTextField out_message=new JTextField(10);
JTextArea in_message=new JTextArea();
JButton send=new JButton("发送");
ShanghaiFrame() {
setTitle("我是上海");
setSize(400,200);
setVisible(true);
send.addActionListener(this);
JPanel pSouth=new JPanel();
pSouth.add(out_message);
pSouth.add(send);
add(pSouth,"South");
add(new JScrollPane(in_message),"Center");
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread thread=new Thread(this);
thread.start();//线程负责接收数据包
}
public void actionPerformed(ActionEvent event) { //点击按扭发送数据包
byte buffer[]=out_message.getText().trim().getBytes();
try{ InetAddress address=InetAddress.getByName("192.168.1.108");//127.0.0.1
DatagramPacket data_pack=
new DatagramPacket(buffer,buffer.length, address,888);
DatagramSocket mail_data=new DatagramSocket();
mail_data.send(data_pack);
String str = out_message.getText();
in_message.append(str+"\n");
out_message.setText(null);
}
catch(Exception e){}
}
public void run() { // 接收数据包
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte data[]=new byte[8192];
try{ pack=new DatagramPacket(data,data.length);
mail_data=new DatagramSocket(666);
}
catch(Exception e){}
while(true) {
if(mail_data==null) break;
else
try{ mail_data.receive(pack);
String message=new String(pack.getData(),0,pack.getLength());
in_message.append("收到数据是:"+message+"\n");
}
catch(Exception e){}
}
}
}
还有一个java文件为 Beijing.java 代码内容基本一样 除了这句端口号改为666 DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length, address,666); 这两个java 文件在同一台电脑运行后 两个窗口可以互发信息 我想问的是 如果这两个程序分别运行在不同电脑 还能实现互发信息吗?如果不行应该怎么修改或是电脑要怎么设置?
展开