packageNet.UDPchat;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;import java.net.*;import javax.swing.*;public class UDPchat implementsActionListener,Runnable {
JFrame jf;
JLabel jl1,jl2,jl3;
JTextField recPortText,sendPortText,IPText,msgText;
JButton startBtn,sendBtn;
JTextArea showArea;
JScrollPane jsp;
JPanel jp1,jp2;
Container con;
Thread thread= null;
DatagramSocket receiveSocket,sendSocket;
DatagramPacket receivePack,sendPack;privateInetAddress sendIP;private intsendPort,recPort;private byteinBuf[],outBuf[];private static final int BUFSIZE = 1024;publicUDPchat(){
jf= new JFrame("聊天————UDP协议");
jl1= new JLabel("接收端口号:");
jl2= new JLabel("发送端口号:");
jl3= new JLabel("对方的地址:");
recPortText= newJTextField();
recPortText.setColumns(5);
sendPortText= newJTextField();
sendPortText.setColumns(5);
IPText= newJTextField();
IPText.setColumns(8);
msgText= newJTextField();
msgText.setColumns(40);
msgText.setEditable(false);
msgText.addActionListener(this);
startBtn= new JButton("开始");
startBtn.addActionListener(this);
sendBtn= new JButton("发送");
sendBtn.setEnabled(false);
sendBtn.addActionListener(this);
showArea= newJTextArea();
showArea.setEditable(false);
showArea.setLineWrap(true); //自动换行
jsp = newJScrollPane(showArea);
jp1= newJPanel();
jp2= newJPanel();
jp1.setLayout(newFlowLayout());
jp2.setLayout(newFlowLayout());
jp1.add(jl1);
jp1.add(recPortText);
jp1.add(jl2);
jp1.add(sendPortText);
jp1.add(jl3);
jp1.add(IPText);
jp1.add(startBtn);
jp2.add(msgText);
jp2.add(sendBtn);
con=jf.getContentPane();
con.add(jp1, BorderLayout.NORTH);
con.add(jsp, BorderLayout.CENTER);
con.add(jp2, BorderLayout.SOUTH);
jf.setSize(600, 400);
jf.setLocation(300, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//在线程中接收数据
public voidrun() {
String str;while(true){try{
receiveSocket.receive(receivePack);
str= new String(receivePack.getData(),0,receivePack.getLength());
showArea.append("对方说:"+str+"\n");
}catch(IOException e) {
showArea.append("接收数据出错!\n");
}
}
}public voidactionPerformed(ActionEvent e) {try{if(e.getSource()==startBtn){
inBuf= new byte[BUFSIZE];
sendPort=Integer.parseInt(sendPortText.getText());
recPort=Integer.parseInt(recPortText.getText());
sendIP=InetAddress.getByName(IPText.getText());
sendSocket= newDatagramSocket();//创建接收数据包
receivePack = newDatagramPacket(inBuf,BUFSIZE);//指定接收数据的端口
receiveSocket = newDatagramSocket(recPort);//创建线程准备接收对方的信息
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
msgText.setEditable(true);
sendBtn.setEnabled(true);
startBtn.setEnabled(false);
}else{ //按下了“发送”按钮或回车键
outBuf =msgText.getText().getBytes();//组装要发送的数据
sendPack = newDatagramPacket(outBuf,outBuf.length,sendIP,sendPort);//发送数据
sendSocket.send(sendPack);
showArea.append("我说:"+msgText.getText()+"\n");
msgText.setText(null);
}
}catch(NumberFormatException e1) {
e1.printStackTrace();
}catch(UnknownHostException e1) {
showArea.append("无法连接到指定地址\n");
}catch(SocketException e1) {
showArea.append("无法打开指定端口\n");
}catch(IOException e1) {
showArea.append("发送数据失败!\n");
}
}public static voidmain(String[] args) {newUDPchat();
}
}