CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Java how to listen on a port? (http://www.codingforums.com/showthread.php?t=264715)

TestingPHP 06-13-2012 12:53 PM

Java how to listen on a port?
 
How can I listen on a port with Java?

TestingPHP 06-13-2012 01:30 PM

Help please? I need to know how I can listen on a port.

alykins 06-13-2012 02:00 PM

example

TestingPHP 06-13-2012 02:31 PM

Quote:

Originally Posted by alykins (Post 1240222)

Does not work, can you please create me a code that listens on the port
PHP Code:

43594 

Thanks.

Fou-Lu 06-13-2012 02:53 PM

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?

TestingPHP 06-13-2012 03:03 PM

Quote:

Originally Posted by Fou-Lu (Post 1240248)
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.

Fou-Lu 06-13-2012 03:36 PM

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.

TestingPHP 06-13-2012 04:02 PM

Quote:

Originally Posted by Fou-Lu (Post 1240258)
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?

Fou-Lu 06-13-2012 04:41 PM

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.

TestingPHP 06-13-2012 05:05 PM

Quote:

Originally Posted by Fou-Lu (Post 1240300)
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
    
}



Fou-Lu 06-13-2012 05:17 PM

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.


TestingPHP 06-13-2012 05:26 PM

Quote:

Originally Posted by Fou-Lu (Post 1240327)
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 


Fou-Lu 06-13-2012 05:47 PM

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?

TestingPHP 06-13-2012 05:49 PM

Quote:

Originally Posted by Fou-Lu (Post 1240339)
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?

alykins 06-13-2012 06:12 PM

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...


All times are GMT +1. The time now is 06:51 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.