1
votes

Well, here's my code:

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class TcpServerEcho {

public static void main(String argv[]) {

    int port = Integer.parseInt(argv[0]);
    ServerSocket server_socket = null;


    try {

        InetAddress thisIp = InetAddress.getByName("kiravaio.homepc.it");
        System.out.println(thisIp.getHostAddress());

        //or
        //server_socket = new ServerSocket(port, 10, InetAddress.getByName("79.47.49.68"));
        server_socket = new ServerSocket(port, 10, thisIp);

    }
    catch (Exception e) {
        System.err.println("Impossible to create socket server!");
        System.out.flush();
        System.exit(1);
    }

    System.out.printf("Server active on port: %d and on address %s\n", port, server_socket.getInetAddress());


    Socket client_socket = null;
    boolean exec = true;

    while(exec) {


        try {

            client_socket = server_socket.accept();

            InputStream is = client_socket.getInputStream();

            OutputStream os = client_socket.getOutputStream();

            boolean stop = false;

            System.out.println("Received: ");

            while(!stop) {
                int b = is.read();
                System.out.print((char)b);
                if(b == -1) {stop = true;}
                else {os.write( (byte)b );}
            }


        }

        catch (IOException ioe) {
            System.err.println("Error I/O!");
        } finally {
            try {
                client_socket.close();
            }

            catch (IOException ioe) {}
            }

        System.out.println("");
    }


    try {
        /* Never executed */
        server_socket.close();
    } catch (IOException ioe) {}
}

}

There's no way for me to create a socket on my public ip. I have a domain kiravaio.homepc.it always updated everytime my ip public changes. So as first, I get my ip address with InetAddress thisIp = InetAddress.getByName("kiravaio.homepc.it"); then I create the socket: server_socket = new ServerSocket(port, 10, thisIp); But it always fails and I don't know why, Only creating a socket on 127.0.0.1 works! :(

2
When you say "public IP", do you mean the IP of your router? Because your computer can only open sockets on its own network devices, iirc. - G. Bach
I mean that if I go to whatsmyip.org I get my public ip and I would like to bind it in a socket...so that I can accept connection requests... - SagittariusA
See Brian Roach's answer; you should learn the basics of networking before writing programs that require networking. - G. Bach
I'll be honest...I do not understand! If I bind the program on the local ip then...how can I reach this program from the extern? I should use public Ip right? - SagittariusA
Don't get me wrong, I wasn't telling you off; it's just necessary to know networking when you're trying to write programs that use it. To answer your question, read Brian Roach's answer. - G. Bach

2 Answers

4
votes

Looking up homepc.it reveals this is a dyndns service domain name. That domain name is resolving to the IP of your router not your PC.

Your PC will have a private network IP address (e.g. 192.168.1.x) assigned to it by your router (that IP is being used by your PC to talk to your router which is performing NAT (Network Address Translation)) .

You have to bind to that address (or simply use the constructor for ServerSocket that only takes a port; this will bind to 0.0.0.0 (INADDR_ANY) and listen on all interfaces), and configure your router to forward the port you have chosen to that IP address.

(If you don't know how to do that, you should consult your router's manual or perhaps post a question to https://superuser.com/)

0
votes

I strongly believe that for ServerSocket, you don't need to specify which ip address is going to be used, just do new ServerSocket(port) and the client should be able to connect via your dynamic domain.