Results 1 to 2 of 2
  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default start server button

    Hi,

    I have created a server that connects to the client. However i have just built a simple gui for the server. What i would like is for when the user clicks a button the server would then start.

    At the moment the gui just freezes until a client is connected. I have set up the start server button to call on the startConnection method in the server class.

    below is the code for my server.

    Java Code:
    import java.io.* ;
    import java.net.* ;
    
    public class MultiServer
        { 
        public static final int PORT = 8080 ;
        ServerSocket server = null;
        Socket socket = null;
        public void startConnection() throws IOException
            {
            server = new ServerSocket(PORT) ;
            System.out.println("Server Started") ;
        }
        public void listenSocket() throws IOException{
            try {
                while(true)
                    {
                    socket = server.accept() ;
                    try {
                        new ServerThread(socket, false) ;
                        }
                    catch(IOException ex)
                        {
                        System.err.println("IOException: " + ex) ;
                        socket.close() ;
                        }
                    }
                }
            finally
                {
                server.close();
                }
        }   } 
        // end of MultiServer class
    many thanks
    gisler

  2. #2
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    I managed to solve my own problem. I had the button call upon the startConnection method but made it at the end start a new thread that would listen for clients. The code looks like this, maybe it will help someone else out.

    Java Code:
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import java.io.* ;
    import java.net.* ;
    
    public class MultiServer implements Runnable
        { 
        public static final int PORT = 8080 ;
        ServerSocket server = null;
        Socket socket = null;
        public void startConnection() throws IOException
            {
            server = new ServerSocket(PORT) ;
            JOptionPane.showMessageDialog(null, "server started");
               Thread thisThread = new Thread(this) ;
                thisThread.start() ;
        }
        public void run(){
            try {
                while(true)
                    {
                    socket = server.accept() ;
                    try {
                        new ServerThread(socket, false) ;
                        }
                    catch(IOException ex)
                        {
                        System.err.println("IOException: " + ex) ;
                        socket.close() ;
                        }
                    }
                }
            catch (IOException ex) {
                Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
            }        finally
                {
                try {
                    server.close();
                } catch (IOException ex) {
                    Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
                }
                }
        }   } 
        // end of MultiServer class

Similar Threads

  1. Replies: 5
    Last Post: 03-20-2009, 11:04 PM
  2. smtp server configuration with jboss server
    By vilas_patil in forum Java Servlet
    Replies: 0
    Last Post: 01-05-2009, 02:18 PM
  3. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 05:30 PM
  4. linux command to start a java web server
    By lazarus in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-24-2008, 04:30 PM
  5. Unable to start WEbSphere server using RAD IDE
    By uppaluru in forum Other IDEs
    Replies: 1
    Last Post: 02-13-2008, 08:51 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
  •