日期:2014-05-20 浏览次数:20933 次
package test; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.util.regex.*;; public class PointToPointUDPChat { Frame f = new Frame("聊天室"); Label lbRemoteIP = new Label("目标IP");// 对方IP Label lbRemotePort = new Label("目标端口"); Label lbLocalSendPort = new Label("本地发送端口"); Label lbLocalReceivePort = new Label("本地接收端口"); TextField tfRemoteIP = new TextField(15);// 要发送数据的目标IP TextField tfRemotePort = new TextField(15);// 要发送数据的目标端口 TextField tfLocalSendPort = new TextField(15);// 使用此端口发送数据 TextField tfLocalReceivePort = new TextField(15);// 使用此端口发送数据 String remoteIP = null; int remotePort = 0; int localSendPort = 0; int localReceivePort = 0; TextArea allChatContent = new TextArea(); TextField sendChatContent = new TextField(20); Button connect = new Button("连接"); Button disConnect = new Button("断开"); Button bt = new Button("发送"); Thread receiveThread = null; public PointToPointUDPChat() { initFrame(); } public static void main(String args[]) { new PointToPointUDPChat(); } public void initFrame() { f.setSize(400, 500); f.setLayout(new BorderLayout()); Panel p = new Panel(); Panel p2 = new Panel(); p.setLayout(new GridLayout(5, 5)); p.add(lbRemoteIP); p.add(tfRemoteIP); p.add(lbRemotePort); p.add(tfRemotePort); p.add(lbLocalSendPort); p.add(tfLocalSendPort); p.add(lbLocalReceivePort); p.add(tfLocalReceivePort); p.add(connect); p.add(disConnect); f.add("North", p); connect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { remoteIP = tfRemoteIP.getText(); remotePort = Integer.parseInt(tfRemotePort .getText()); localSendPort = Integer.parseInt(tfLocalSendPort .getText()); localReceivePort = Integer .parseInt(tfLocalReceivePort.getText()); } catch (Exception exception) { prompt("连接信息不能为空或格式错误"); return; } if (!checkIP(remoteIP)) { prompt("目标IP设置错误"); return; } if (!checkPort(remotePort)) { prompt("目标端口设置错误"); return; } if (!checkPort(localSendPort)) { prompt("本地发送端口设置错误"); return; } if (!checkPort(localReceivePort)) { prompt("本地接收端口设置错误"); return; } prompt("连接成功"); tfRemoteIP.setEditable(false); tfRemotePort.setEditable(false); tfLocalReceivePort.setEditable(false); tfLocalSendPort.setEditable(false); receiveMessage(); } }); disConnect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tfRemoteIP.setEditable(true); tfRemotePort.setEditable(true); tfLocalReceivePort.setEditable(true); tfLocalSendPort.setEditable(true); tfLocalReceivePort.setText(""); tfLocalSendPort.setText(""); tfRemoteIP.setText(""); tfRemotePort.setText(""); remoteIP = null; remotePort = 0; localSendPort = 0; localReceivePort = 0; receiveThread.stop(); prompt("断开成功"); } }); f.add("Center", allChatContent); p2.setLayout(new FlowLayout()); p2.add(bt); p2.add(sendChatContent); f.add("South", p2); f.setVisible(true); f.setResizable(false); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.setVisible(false); f.dispose(); System.exit(0); } }); bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendMessage(); } }); sendChatContent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendMessage(); } }); } public void sendMessage() {// // 定义ds DatagramSocket ds = null; try { ds = new DatagramSocket(localSendPort); } catch (SocketException e1) { prompt("本地发送端口已被使用"); return; } String sendMessage = sendChatContent.getText().trim(); if (sendMessage.equals("")) { prompt("无效信息"); return; } byte[] buf = sendMessage.getBytes();// // InetAddress inetAddress = null; try { inetAddress = InetAddress.getByName(tfRemoteIP.getText().trim()); } catch (UnknownHostException e) { prompt("非法远程IP地址"); return; } DatagramPacket dp = new DatagramPacket(buf, 0, buf.length, inetAddress, remotePort); try { ds.send(dp); } catch (IOException e) { prompt("网络故障,发送失败"); return; } sendChatContent.setText(""); allChatContent.append(new java.text.SimpleDateFormat( "yy-MM-dd HH:mm:ss").format(new Date()) + " send to Remote(" + dp.getAddress() + " " + dp.getPort() + ") :\n"); allChatContent.append(sendMessage + "\n"); WriteLog.print("c:/tempLog",tfRemoteIP.getText());//暂时存放在C:/tempLog.txt中 ds.close(); } // 接收数据 class MyRunnable implements Runnable { byte buf[] = new byte[1024]; DatagramSocket ds = null; DatagramPacket dp = null; public void run() { dp = new DatagramPacket(buf, 0, 1024); try { ds = new DatagramSocket(localReceivePort); } catch (SocketException e1) { prompt("本地接收端口已被使用"); return; } while (true) { try { ds.receive(dp); } catch (IOException e) { ds.close(); e.printStackTrace(); } String receiveMessage = new String(dp.getData(), 0, dp .getLength()); allChatContent.append(new java.text.SimpleDateFormat( "yy-MM-dd HH:mm:ss").format(new Date())// + " from remote(" + dp.getAddress().getHostAddress() + " " + dp.getPort() + ") :\n" + receiveMessage + "\n"); sendChatContent.setCaretPosition(sendChatContent.getText() .length()); WriteLog.print("c:/tempLog.txt",receiveMessage);//暂时存放在C:/tempLog.txt中 } } } public void receiveMessage() {// receiveThread = new Thread(new MyRunnable()); receiveThread.start(); } // 异常处理 public void prompt(String promptMessage) { JOptionPane.showConfirmDialog(null, promptMessage, "友情提示", JOptionPane.WARNING_MESSAGE); } public boolean checkPort(int port) { return String.valueOf(port).matches("\\d+") && port > 1024 && port <= 65535; } public boolean checkPort(String port) { return port.matches("\\d+") && Integer.parseInt(port) > 1024 && Integer.parseInt(port) <= 65535; } public static boolean isDigit(String text) { return text.matches("\\d+"); } public boolean checkIP(String ip) { java.util.regex.Matcher m = Pattern.compile( "(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})").matcher(ip); if (m.find()) { for (int i = 1; i <= 4; i++) if (Integer.parseInt(m.group(i)) < 0 || Integer.parseInt(m.group(i)) > 255) return false; return true; } return true; } } /** * 写入日志 filePath 日志文件的路径 code 要写入日志文件的内容 */ class WriteLog { public static boolean print(String filePath, String code) { FileWriter fw = null; BufferedWriter bw = null; PrintWriter pw = null; try { File tofile = new File(filePath); fw = new FileWriter(tofile, true); bw = new BufferedWriter(fw); pw = new PrintWriter(bw); pw.println(code); pw.close(); bw.close(); fw.close(); return true; } catch (IOException e) { return false; } finally { try { if (pw == null) pw.close(); if (bw == null) bw.close(); if (fw == null) fw.close(); } catch (Exception e) { } } } }