绑定端口

来源:互联网 发布:php 工厂模式 代码 编辑:程序博客网 时间:2024/05/17 06:27

/**
 *  [ 文件实现功能 ]  :绑定端口
*/
package com.education.view;

import java.net.ServerSocket;
import java.net.Socket;


public class InstanceControl extends Thread {
    private boolean isRun = false;
    public void run() {
        try {
            Socket sock = new Socket("127.0.0.1", 44444);
            isRun = true;
        } catch (Exception e) {
            try {
                ServerSocket server = new ServerSocket(44444);
                while (true) {
                    server.accept();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @return the isRun
     */
    public boolean isRun() {
        return isRun;
    }
}

 

  //NetBeans中的java Swing组件 JFrame

  //主函数

 public static void main(String args[]) {

        try {//显示风格设定为Windows风格
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
        }
        //绑定端口,防止登陆多个驻留程序
        InstanceControl ic = new InstanceControl();
        ic.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
        }
        if (ic.isRun()) {
            javax.swing.JFrame jf = new JFrame();
            jf.setUndecorated(true);
            jf.setAlwaysOnTop(true);
            utils.CenterFrame(jf);
            JOptionPane.showMessageDialog(null, "同一台电脑不能登陆多个帐号!");
            jf.dispose();
            System.exit(0);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginFrame().setVisible(true);
            }
        });
    }

 

原创粉丝点击