![]() |
Java how to listen on a port?
How can I listen on a port with Java?
|
Help please? I need to know how I can listen on a port.
|
|
Quote:
PHP Code:
|
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? |
Quote:
Basically I only want the listening part nothing else no client connecting just the listening part. Thanks. |
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. |
Quote:
And that's it? |
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:
|
Quote:
Why isn't this working? PHP Code:
|
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. |
Quote:
Code:
import java.io.*;Code:
[root@V-4836 ~]# java -Xmx1G -Xms1G PortListenPHP Code:
|
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?
|
Quote:
Could you compile it for me then? |
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.