简体   繁体   中英

how to run a server socket on amazon EC2

I'm trying to write an android app that interacts with a server. I'm trying now to write the server side with sockets. I created an instance of ec2 and ran it. I connected to it with putty and ran a simple "hello world" java program. Now I'm trying to run a server which use a socket, but I get this as the server socket: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=5667]. This is my code, very basic so far:

public class EchoServer {
public static void main(String[] args) throws IOException {

    int portNumber = 5667;

    System.out.println("server socket main");

    try (ServerSocket serverSocket = new ServerSocket(portNumber);
            ) {

        System.out.println(serverSocket.toString());
    } catch (IOException e) {
        System.out
                .println("Exception caught when trying to listen on port "
                        + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}
}

What should I do to run the server so I could connect to it via my andriod device?

Thanks!

You are on the right track. 0.0.0.0 simply means:

A way to specify "any IPv4-interface at all". It is used in this way when configuring servers (ie when binding listening sockets).

Next, your server will need to listen for incoming connections by using SocketServer.accept() .

And then of course you'll want to receive and send data on the socket. This tutorial should help.

Finally, if you plan on serving multiple clients simultaneously with your server, you will want to consider concurrency and scalability , and perhaps consider using a framework like Netty .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM