Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java how to listen on a port?

    How can I listen on a port with Java?

  • #2
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Help please? I need to know how I can listen on a port.

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,805
    Thanks
    41
    Thanked 199 Times in 198 Posts

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    Does not work, can you please create me a code that listens on the port
    PHP Code:
    43594 
    Thanks.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The TCPServer code looks to me that it will listen on port 6789 and wait for client input. Change the port to match the one you need.
    When you say it doesn't work, what specifically do you mean? When you run the app, does it show as listening on port 6789 (or 43594 when you modify it) when you run a netstat?

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The TCPServer code looks to me that it will listen on port 6789 and wait for client input. Change the port to match the one you need.
    When you say it doesn't work, what specifically do you mean? When you run the app, does it show as listening on port 6789 (or 43594 when you modify it) when you run a netstat?
    I tried it, it's not want I want

    Basically I only want the listening part nothing else

    no client connecting just the listening part.

    Thanks.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Why? If you can't respond to listening on the port, then there isn't really a need to establish it.
    That's what you pretty much have to do. Set up a server socket, then while true accept from it. That's all you need to do to listen on a port.

  • #8
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Why? If you can't respond to listening on the port, then there isn't really a need to establish it.
    That's what you pretty much have to do. Set up a server socket, then while true accept from it. That's all you need to do to listen on a port.
    I'm a newbie, it's going to be used for a server status type of thing, can you make me a class that just listens on the port via sockets?

    And that's it?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Just use the example. All you need is to open the socket, then issue an .accept() within the while(true). Maybe sleep it for a couple of seconds. That's all you got to do. Technically you don't even need to accept if you don't plan on doing anything with it:
    PHP Code:
        public static void main(String[] argvthrows IOExceptionInterruptedException
        
    {
            
    ServerSocket welcomeSocket = new ServerSocket(43594);
            while (
    true)
            {
                
    Thread.sleep(2000);
                
    System.out.println("Nothing to do; take'n a nap.");
            }
        } 
    That's it.

  • #10
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Just use the example. All you need is to open the socket, then issue an .accept() within the while(true). Maybe sleep it for a couple of seconds. That's all you got to do. Technically you don't even need to accept if you don't plan on doing anything with it:
    PHP Code:
        public static void main(String[] argvthrows IOExceptionInterruptedException
        
    {
            
    ServerSocket welcomeSocket = new ServerSocket(43594);
            while (
    true)
            {
                
    Thread.sleep(2000);
                
    System.out.println("Nothing to do; take'n a nap.");
            }
        } 
    That's it.
    Eh, I'm very new to Java

    Why isn't this working?

    PHP Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package port.listen;

    /**
     *
     * @author TestingPHP
     */


    class PortListen {
        
     
    java.net.Socket.Serversocket welcomeSocket = new Serversocket(43594);

        
    /**
         * @param args the command line arguments
         */
        
    public static void main(String[] args) {
            
    // TODO code application logic here
        
    }


  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You don't have anything in your main. You have to put an infinite loop in there.
    Edit:
    Also, if you are doing this you then need to invoke the object since the ServerSocket is a part of the class members and not a part of the main. At least I think so, let me double check.
    Edit:
    Yep, you need to invoke it then. You also need to override the constructor to add a checked exception for the IOException since it won't handle it otherwise.

    Last edited by Fou-Lu; 06-13-2012 at 05:20 PM.

  • #12
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You don't have anything in your main. You have to put an infinite loop in there.
    Edit:
    Also, if you are doing this you then need to invoke the object since the ServerSocket is a part of the class members and not a part of the main. At least I think so, let me double check.
    Edit:
    Yep, you need to invoke it then. You also need to override the constructor to add a checked exception for the IOException since it won't handle it otherwise.

    I'#m using
    Code:
    import java.io.*;
    import java.net.*;
    
    /**
     *
     * @author TestingPHP
     */
    
    
    public class PortListen {
        
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
              try {
    
                        ServerSocket myServerSocket = new ServerSocket(43594);
                        
                        System.out.println("Server is waiting for an incoming connection on host-" + InetAddress.getLocalHost().getCanonicalHostName() + " port-" + myServerSocket.getLocalPort());
                        System.out.println("");
    
                            while (true) {
    
                                Socket skt = myServerSocket.accept();
    
                                BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    
                                String buf = myInput.readLine();
    
                                if (buf != null) {
    
                                        System.out.println("Server read: [" + buf + "]");
                                    System.out.println("");
    
                                }
    
                            }
    
                    } catch (IOException e) {
    
                            e.printStackTrace();
                            System.out.println("Whoops, something bad happened.");
    
                    }
    
            }
    
    }
    How can I get it to work on linux? i'm getting

    Code:
    [root@V-4836 ~]# java -Xmx1G -Xms1G PortListen
    Exception in thread "main" java.lang.UnsupportedClassVersionError: PortListen : Unsupported major.minor version 51.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: PortListen. Program will exit.
    [root@V-4836 ~]#
    While using the cmd
    PHP Code:
    java -Xmx1G -Xms1G PortListen 

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    According to this error you've compiled PortListen using a version of java that is greater than the version of your JRE. What's the result of a java -version?

  • #14
    New Coder
    Join Date
    Jun 2012
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    According to this error you've compiled PortListen using a version of java that is greater than the version of your JRE. What's the result of a java -version?
    oh, i compiled it on 15 & using 14 on my vps. (JDK).

    Could you compile it for me then?

  • #15
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,805
    Thanks
    41
    Thanked 199 Times in 198 Posts
    I am failing to see the point... If you are not going to do anything with it- and use it as a 'status' type of indicator, the only thing it is going to tell you is whether or not that port is open... for example it would return 'off' if the program failed to start, if it threw a fatal error and crashed, if something else established connection with the socket and is holding it, if you happen to attempt to connect during the sleep interval (which is the most likely situation)... it really isn't any valid test- it would be like saying "well I am sick if I feel tired" ... you can feel tired for a lot of reasons, you can be sick and not tired, etc etc...

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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