使用UDP協議,通過發送數據報給對方,每次啟動時需要設定接收數據的端口號,發送到的ip地址和端口可以在啟動后修改。
打包好的代碼:
http://115.com/file/e7jb7noz#
QQ.zip
主要思路很簡單:
1.設置自己的接收端口
2.設置對方IP和端口
3.發送數據和接收數據
下面是主要代碼:
import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class QQFrame extends JFrame implements Runnable { private JTextArea sendArea = new JTextArea(20,20); private JTextArea showArea = new JTextArea(10,20); private JButton sendButton = new JButton("發送"); private MenuBar menuBar = new MenuBar(); private Menu setMenu = new Menu("設置"); private Menu helpMenu = new Menu("幫助"); private MenuItem aboutItem = new MenuItem("關於"); private MenuItem setItem = new MenuItem("配置參數"); private Thread thread = new Thread(this); private Config config = new Config(); private JDialog dialog = new JDialog(this,"參數配置",true); private int myPort; QQFrame(int in_myPort) { super("局域網聊天工具"); setResizable(false); myPort = in_myPort; setItem.addActionListener(new SetDialog(dialog,this)); aboutItem.addActionListener(new AboutDialog(this)); initLayout(); sendButton.addActionListener(new SendListener(this,sendArea,showArea,config)); thread.start(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } void initLayout() { int w,h; w = 400; h = 500; setBounds(100,100,w,h); setVisible(true); setLayout(new GridLayout(3,1,10,15)); JPanel jp1,jp2; jp1 = new JPanel(); jp2 = new JPanel(); JLabel jl1; jl1 = new JLabel("聊天記錄框:"); jp1.setLayout(null); jp1.add(jl1); jl1.setBounds(0,0,w,20); jp1.add(showArea); showArea.setBounds(0,30,w,h/3-20); JLabel jl2; jp2.setLayout(null); jl2 = new JLabel("發送框:"); jp2.add(jl2); jl2.setBounds(0,0,w,20); sendArea.setBounds(0,30,w,h/3-20); jp2.add(sendArea); JPanel jp3 = new JPanel(); add(jp1); add(jp2); add(jp3); jp3.add(sendButton); setMenuBar(menuBar); menuBar.add(setMenu); menuBar.add(helpMenu); helpMenu.add(aboutItem); setMenu.add(setItem); dialog.setBounds(120,150,200,250); JLabel IPLabel = new JLabel("對方IP"); JLabel yourPortLabel = new JLabel("對方接收端口"); JLabel nameLabel = new JLabel("本機名"); JTextField IPTextField = new JTextField(15); JTextField yourPortTextField = new JTextField(8); JTextField nameTextField = new JTextField(15); IPTextField.setText("127.0.0.1"); yourPortTextField.setText("879"); nameTextField.setText("hanxi"); dialog.setLayout(new GridLayout(4,2,10,10)); dialog.add(IPLabel); dialog.add(IPTextField); dialog.add(yourPortLabel); dialog.add(yourPortTextField); dialog.add(nameLabel); dialog.add(nameTextField); dialog.add(new Panel()); JButton sureButton = new JButton("確定"); dialog.add(sureButton); sureButton.addActionListener(new SureListener(IPTextField,yourPortTextField,nameTextField,dialog,config)); } public void run() { DatagramPacket dp = null; DatagramSocket ds = null; byte[] buf = new byte[1024]; try { dp = new DatagramPacket(buf,buf.length); ds = new DatagramSocket(myPort); System.out.println("myport="+myPort); } catch (Exception e) {} while (true) { try { ds.receive(dp); int length = dp.getLength(); InetAddress address = dp.getAddress(); int port = dp.getPort(); String message = new String(dp.getData(),0,length); showArea.append("收到數據長度:"+length+"\n"); showArea.append("收到數據來自:"+address+"端口:"+port+"\n"); showArea.append("收到數據是:"+message+"\n"); } catch (Exception e) { } } } } //發送事件 class SendListener implements ActionListener { private JFrame QQFrame; private JTextArea sendArea; private JTextArea showArea; private Config config; SendListener(JFrame in_QQFrame, JTextArea in_sendArea, JTextArea in_showArea, Config in_config) { QQFrame = in_QQFrame; sendArea = in_sendArea; showArea =in_showArea; config = in_config; System.out.println("in_port:"+config.getyourPort()); QQFrame.validate(); } public void actionPerformed(ActionEvent e) { System.out.println("你點擊了發送按鈕"); byte[] buf = sendArea.getText().trim().getBytes(); try { InetAddress address = InetAddress.getByName(config.getIP()); DatagramPacket dp = new DatagramPacket(buf,buf.length,address,Integer.parseInt(config.getyourPort())); DatagramSocket ds = new DatagramSocket(); showArea.append("數據包目標地址:"+dp.getAddress()+"\n"); showArea.append("數據包目標端口號:"+dp.getPort()+"\n"); showArea.append("數據包長度:"+dp.getLength()+"\n"); ds.send(dp); } catch (Exception ee) {} } } //配置設置 class SetDialog implements ActionListener { private JDialog dialog; private JFrame frame; SetDialog(JDialog in_dialog,JFrame parent) { dialog = in_dialog; frame = parent; } public void actionPerformed(ActionEvent e) { dialog.validate(); int x = frame.getX(); int y = frame.getY(); dialog.setBounds(x+50,y+50,200,250); dialog.setVisible(true); } } //關於對話框 class AboutDialog extends JDialog implements ActionListener { private JFrame frame; private JLabel jl = new JLabel("感謝您的使用,作者:涵曦"); AboutDialog(JFrame parent) { frame = parent; add(jl); } public void actionPerformed(ActionEvent e) { validate(); int x = frame.getX(); int y = frame.getY(); setBounds(x+50,y+50,200,250); setVisible(true); } } //對話框的確定按鈕事件 class SureListener implements ActionListener { JTextField IPTextField; JTextField yourPortTextField; JTextField nameTextField; private Dialog dialog; private Config config; SureListener(JTextField in_IPTextField,JTextField in_yourPortTextField,JTextField in_nameTextField,JDialog in_dialog,Config in_config) { IPTextField = in_IPTextField; yourPortTextField = in_yourPortTextField; nameTextField = in_nameTextField; dialog = in_dialog; config = in_config; } public void actionPerformed(ActionEvent e) { config.set(IPTextField.getText(),yourPortTextField.getText(),nameTextField.getText()); System.out.println("config:yourPort:"+config.getyourPort()); dialog.setVisible(false); } } //參數 class Config { private String IP="127.0.0.1"; private String yourPort="666"; private String name="hanxi"; public void set(String in_IP, String in_yourPort,String in_name) { IP = in_IP; yourPort = in_yourPort; name = in_name; } public String getIP() { return IP; } public String getyourPort() { return yourPort; } public String getName() { return name; } }
下面是主方法代碼:
public class QQ { public static void main(String args[]) { if (args.length == 0) { System.out.println("請在QQ后面輸入接收數據的端口號(需要空格)!"); System.exit(0); } System.out.println(Integer.parseInt(args[0])); QQFrame frame = new QQFrame(Integer.parseInt(args[0])); } }
設置端口和IP的圖片
下面是聊天界面
感謝 我是一林風 提供的錄屏,請下載觀看http://pan.baidu.com/s/1o6Hkero