Results 1 to 2 of 2
  1. #1
    miu44 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default how to run server-client program in desktop application??

    Hi, I will try to explain my problems as best as possible.

    I have built a server-client GUI using java SE application by building a separate Frame and separate main class and it works fine.
    But then I have to use dialog Frame[jdialog] in my GUI..and because i could not add jdialog in existing GUI, I tried to build all over in Java Desktop Application. The problems is I do not know how to launch the runServer method in Java Desktop Application.
    I tried to create a separate main class in the GUI but it didnt launch the runServer method.

    Below is my code in the frame view

    Java Code:
    public class WiportView extends FrameView {
    
        ServerSocket server = null;
        Socket client = null;
        BufferedReader in = null;
        PrintWriter out = null;
        String line;
        int portNumber = 4444;
    
        int i = 0;
        public Object[] rowData = new Object[3];
        public int[][] sendData = new int[256][2];
    
    public void setPortNumber(int newPortNumber) {
            portNumber = newPortNumber;
        }
    
         public void runServer() {
            try {
                try {
                    server = new ServerSocket(portNumber);
                    jLabel1.setText("LISTENING | ポート:" + portNumber);
                    portLabel.setText("現在のオーペンポート:" + server.getLocalPort());
                }catch (IOException e) {
                    System.exit(-1);
                }
                listenSocket();
            }catch (IOException ex) {
                Logger.getLogger(WiportView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
         public void reopenSocket() {
            runServer();
            try {
                listenSocket();
            } catch (IOException ex) {
                Logger.getLogger(WiportView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
         public void disconnect() {
            try {
                out.flush();
                in.close();
                out.close();
                client.close();
                server.close();
                server = null;
                client = null;
                jLabel1.setText("CLIENT DISCONNECTED | ポート:" + portNumber);
                changePortButton.setEnabled(true);
                newPortNumberText.setEnabled(true);
            }catch (IOException e) {
                System.exit(-1);
            }
        }
    
          public void listenSocket() throws IOException {
            try {
                client = server.accept();
                jLabel1.setText("CLIENT CONNECTED | ポート:" + client.getLocalPort());
            }catch (IOException e) {
                System.exit(-1);
            }
    
            try {
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                out = new PrintWriter(client.getOutputStream(), true);
    
                if (in != null) {
                    display.setText("CLIENT " + client.getInetAddress() + ": < CONNECTED >\n");
                    changePortButton.setEnabled(false);
                    newPortNumberText.setEnabled(false);
                }
            } catch (IOException e) {
                System.exit(-1);
            }
            while (true) {
    
                line = in.readLine();
    
                if (line.equals("<DISCONNECTED>")) {
                    display.setText("CLIENT " + client.getInetAddress() + ": " + line);
    
                    disconnect();
    
                    reopenSocket();
                    jLabel1.setText("");
                    jLabel1.setText("OFFLINE | ポート:" + portNumber);
                }else if (line != null) {
                    display.setText("CLIENT " + client.getInetAddress() + ": " + line);
                    display.setText("\n");
                }
            }
        }
    
        private void formWindowClosed(java.awt.event.WindowEvent evt) {
    
            line = "OFF";
            out.println(line);
            disconnect();
        }
    }
    and i tried to call runServer by building a separate main class

    Java Code:
    public class WiportMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            WiportView wv = new WiportView();
            wv.setVisible(true);
    
            wv.runServer();
        }
    
    }
    but i didn't works and I tried searching the web but don't find the answer yet,
    can anyone give me some suggestion here? thanx for advance.:)

  2. #2
    miu44 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    To anyone who is viewing and trying to solve,thanx a lot.
    But to anyone who is wondering what the answers is,Netbeans desktop application launching the GUI as a whole,so I resolved it by added 2 jbutton(connect and disconnect),
    and rewrite the code into launching the server after the GUI launching the application.

Similar Threads

  1. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 02:05 AM
  2. Replies: 0
    Last Post: 10-18-2010, 05:18 AM
  3. Replies: 2
    Last Post: 11-10-2009, 04:29 PM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •