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

    Default serversocket listen connection

    Hi,

    I have been building my whole client/server program on one machine using localhost for the client.

    However i have now tried to connect the client to the server across a local network.

    i am not sure how i set my server to listen on my default connection.
    this is my server code
    Java Code:
    public class MultiServer implements Runnable {
    
        public static final int PORT = 8080;        //set the port
        ServerSocket server = null;      //create a server socket variable
        Socket socket = null;       //create a new socket variable
    
        public void startConnection() throws IOException {
         
            server = new ServerSocket(PORT);//creates a new server socket called server
            JOptionPane.showMessageDialog(null, "server started"); //displays a message that the server has been started
            Thread thisThread = new Thread(this); //creates a new thread
            thisThread.start();   //starts the new thread
        }
    
        public void run() {
            try {
                while (true) {
                    socket = server.accept(); //listens and accepts clients
                    try {
                        new ServerThread(socket, false);  //start serverThread
                        } catch (IOException ex) //catch IOexception if serverthread has errors
                    {
                        System.err.println("IOException: " + ex);  //display error
                        socket.close();   //close connection
                        }
                }
            } catch (IOException ex) {   //catch IOexception if client cant connec to server
                Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    server.close();      //close server
                } catch (IOException ex) {
                    Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    // end of MultiServer class

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    By default, your ServerSocket will listen on ALL IP addresses defined for the host. You can specify an address using the three parameter constructor or by using bind() method.

Similar Threads

  1. Replies: 3
    Last Post: 05-22-2011, 04:16 PM
  2. Listen for changes in a JTextField
    By matzahboy in forum AWT / Swing
    Replies: 5
    Last Post: 10-13-2009, 05:15 PM
  3. Cannot Listen TAB key
    By sh4dyPT in forum AWT / Swing
    Replies: 5
    Last Post: 07-16-2009, 02:45 PM
  4. how to listen for buttons between jpanels?
    By pjr5043 in forum New To Java
    Replies: 17
    Last Post: 09-29-2008, 04:37 AM
  5. problem with ServerSocket on Linux
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:46 PM

Posting Permissions

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