Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2012, 12:53 PM   PM User | #1
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
Java how to listen on a port?

How can I listen on a port with Java?
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 01:30 PM   PM User | #2
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
Help please? I need to know how I can listen on a port.
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 02:00 PM   PM User | #3
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
example
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 06-13-2012, 02:31 PM   PM User | #4
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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.
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 02:53 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Old 06-13-2012, 03:03 PM   PM User | #6
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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.
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 03:36 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 06-13-2012, 04:02 PM   PM User | #8
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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?
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 04:41 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 06-13-2012, 05:05 PM   PM User | #10
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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
    
}

TestingPHP is offline   Reply With Quote
Old 06-13-2012, 05:17 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Old 06-13-2012, 05:26 PM   PM User | #12
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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 
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 05:47 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Old 06-13-2012, 05:49 PM   PM User | #14
TestingPHP
New Coder

 
Join Date: Jun 2012
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
TestingPHP is an unknown quantity at this point
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?
TestingPHP is offline   Reply With Quote
Old 06-13-2012, 06:12 PM   PM User | #15
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 PM.


Advertisement
Log in to turn off these ads.