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?
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
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.
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?
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[] argv) throws IOException, InterruptedException { ServerSocket welcomeSocket = new ServerSocket(43594); while (true) { Thread.sleep(2000); System.out.println("Nothing to do; take'n a nap."); } }
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[] argv) throws IOException, InterruptedException
{
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
}
}
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.
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 ~]#
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?
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).
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...