如何打开用户java输入的端口

时间:2016-12-26 14:04:34

标签: java swing netbeans port

我有两个疑问

第一

我正在netbeans中创建桌面应用程序,我想打开 端口 由用户输入。我已经在一个文件中为用户创建了两个文件获取端口数字处理就是第二个文件。

我有该类的已创建 对象,用于获取用户输入的端口号未显示任何错误,但我没有检查使用tcp视图

的那个号码上的端口未打开

这是第一个文件的代码

Server.java

 package server;
    import java.io.*;
    import java.net.*;
    import javax.swing.JFrame;

    public class Server extends Thread {
      public static int SERVERPORT;
      private boolean running = false;
      public volatile boolean stop = false;
      public Socket client = null;

    public static void main(String[] args) {
        mainFrame frame = new mainFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        SERVERPORT = frame.portnum;//here i am getting port number from 2nd file
    }

    @Override
    public void run() {
        super.run();
        running = true;
        try {
            System.out.println("Server Has Started........ \n Waiting for client........");
            ServerSocket serverSocket = new ServerSocket(SERVERPORT);
            try {
                while (!stop && running) {
                    client = serverSocket.accept();
                    System.out.println("Connection Accepted......");
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    String usercmnd = in.readLine();
                    if (usercmnd != null) {
                        Runtime runtime = Runtime.getRuntime();
                        Process p = runtime.exec(usercmnd);
                    }
                }

这是第二个文件,用户必须输入端口号

mainFrame.java

package server;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.net.*;
//import server.Server;


/**
 *
 * @author admin
 */
public class mainFrame extends JFrame {
private Server  mServer;
public int portnum;
   public mainFrame() {
        super("mainFrame");
        mServer = new Server();
        initComponents();
        setIcon();labInfo.setText("Not Connected");
        try{ ipAdd.setText(String.valueOf(InetAddress.getLocalHost().getHostAddress()));
        }catch(Exception e){labInfo.setText(e.getMessage());}

    }


    private void connActionPerformed(java.awt.event.ActionEvent evt) {                                     
        if(port.getText().equals(""))
        {
           labInfo.setText("Port number cannot be empty!!");
        }else{
            portnum=Integer.parseInt(port.getText());//here i am getting user's entered port number
            conn.setEnabled(false);port.setEditable(false);
            labInfo.setText("Waiting for Connection.....");
            mServer.start();
        }
    }                                    

    private void disconnActionPerformed(java.awt.event.ActionEvent evt) {                                        
        mServer.requestStop();
        labInfo.setText("Not Connected");
        port.setEditable(true);
        conn.setEnabled(true);        
    }                                       

SECOND

有没有办法检查输入的端口号目前是否正在使用,那么我们可以提醒用户使用另一个端口号... ???

1 个答案:

答案 0 :(得分:1)

我建议您先运行mainFrame类,然后单击Server调用Button类,以检索{{1}中输入的端口号}}。我似乎无法帮助完整代码,但您可以将TextField方法更改为

connActionPerformed()

然后你可以实现这样的构造函数:

   private void connActionPerformed(java.awt.event.ActionEvent evt) {
        boolean success = false;
        do {
            try {
                if (port.getText().equals("")) {
                    labInfo.setText("Port number cannot be empty!!");
                } else {
                    portnum = Integer.parseInt(port.getText());//here i am getting user's entered port number
                    ServerSocket serverSocket = new ServerSocket(portnum);
                    System.out.println("Connected to Server");
                    mServer = new Server(serverSocket);//Which means you need to implement a constructor with a Server argument.

                    //conn.setEnabled(false);
                    //port.setEditable(false);
                    //abInfo.setText("Waiting for Connection.....");
                    mServer.start();
                    success = true;
                }

            } catch (BindException ex) {
                System.out.println("Port in use");
            }
        } while (success);

    }

注意:由于此代码不完整,请根据您的需要进行操作。 希望这会有所帮助,谢谢。