View Single Post
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