Problems with Chat Sockets with NO-IP Host

3

Well, first of all, I looked at several sites, including here, how to do this. I tested codes and modified them but still, I kept getting errors.

The problem is the sockets to make the connection. I have no idea how to use them, and I need to create a program that works as server / client. He will send something like 5 Strings, for another program that will do some reorganizations and send strings to another server / client and return more strings to the first, basically it will be a chat with a server, one Chat.jar and Servidor.jar .

I would like to make Servidor.jar work on a computer with no-ip installed .

Here is the code I have at the moment:

Socket clientSocket = null;
BufferedReader inputLine;
PrintStream os = null;
BufferedReader is = null;
InetSocketAddress addr = new InetSocketAddress("servidor.ddns.net", 15980);
try {
    ServerSocket serverSocket = new ServerSocket(15980);
    clientSocket = serverSocket.accept();
    clientSocket = new Socket("servidor.ddns.net", 15980);
    System.out.println("Connected");
    inputLine = new BufferedReader(new InputStreamReader(System.in));
    os = new PrintStream(clientSocket.getOutputStream());
    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    serverSocket.close();
} catch (UnknownHostException e) {
    System.err.println("Don't know about host");
} catch (IOException e) {
    System.err.println("Couldn't get I/O for the connection to the host \nServer must be down! NOOO!!");
}

In a more illustrative way, it would be something like this:

  

Server / Client¹ ---- > Server / Primary Client ---- > Server / Client²

Then ...

  

Server / Client² ---- > Server / Primary Client ---- > Server / Client¹

The Primary Server / Client would be Servidor.Jar and the others, Chat.jar .

Using a similar code with the code above, only received the message " IOException e " and other forms, received something like " Connect: refused connect ."

Well ... In short, I need to send several Strings, while I can still receive Strings. I do not know how to explain it right and how to ask, I'm totally lost. I hope you have understood.

NOTE: The code for the rest of the program is done. That's just missing.

This part of the code is new and is already working normal, now only remained a problem, I wanted to switch 127.0.0.1 , for a connection to the host in the IP, for example: meuservidor.ddns.net

Can you do this? If so, how?

Socket cliente = new Socket("127.0.0.1", 12342);
System.out.println("O cliente conectou ao servidor");
ObjectOutputStream dados = new ObjectOutputStream(cliente.getOutputStream());
// O código dessa forma está funcionando
dados.writeUTF("Projeto");
dados.writeUTF("Outra Mensagem");
dados.writeUTF("Projeto");

dados.flush();
cliente.close();

This is what appears when I change localhost to " meuservidor.ddns.net " using this port: 12342

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at calc.cliente.main(cliente.java:10)

The solution must be something that does not involve freeing ports on the firewall / router.

    
asked by anonymous 25.02.2015 / 18:17

1 answer

4

Your server code is well-routed. But it seems to me that your confusion lies in how it works. I'll try to clarify.

You need to first understand the distributed communication model via Socket: Think of a socket as a channel that connects two processes (in this case, two JVMs) whether they are on two separate machines or not. For the network structure that we use from where I am aware, a machine is identified by its IP and a process inside a machine is identified by a PORT. So if you want to create in JAVA software that "listens" on port 7777 (a server) and wait for external communication, you will use the ServerSocket object:

ServerSocket servidor = new ServerSocket( 7777 );
Socket cliente = servidor.accept();
// Parei pra esperar...

In this case, when the code is executed, servidor.accept() will stop and wait for a connection. Now you want another software (ie another class to be run separately) to connect to send messages. Basically you need it:

Socket cliente = new Socket( "127.0.0.1", 7777 );

This code when executed will open the channel with the server (provided it is running on the same machine (IP = 127.0.0.1) .If you want another machine, just change the IP.You now have a channel, and you need to pass things in. In the case of Socket in Java, you have a two-way path, that is, one going from the client to the server and another from the server going to the client. objects of the Stream class (which are nothing more than a sequence of bytes).

As in your problem you want your server to receive Strings, just after the channel is opened, you need to get the Stream of input (input) and do a reading:

InputStream ins = cliente.getInputStream();
ObjectInputStream objIn = new ObjectInputStream(ins);
Object msg = objIn.readObject();

I've used ObjectInputStream here to make it easier to read any channel object. You can continue using your BufferedReader . Already on the client side, to send a message you need something like this:

OutputStream outs = cliente.getOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream( outs );
objOut.writeObject( "HELLOOOOO" );

Fundamentally this is it. The client uses the output channel (OutputStream) on its side of the Socket to send an object, which is the String "HELLOOOOO". Note that your msg msg object can do what you like and then send it back to the client (using your OutputStream).

In order for the server to communicate with other software, it will need to create another Socket. And then the pattern is the same: either he expects a new connection, or he tries to open the connection to someone who is already waiting.

Other very common cases: (1) so your server can receive multiple messages from the same client, a loop in readObject can help; (2) if your server needs to handle multiple clients, the best thing to do is create a Thread for each client, but there are 500 more.

Finally, I hope you understand this concept because it is fundamentally the same for any network connection we use today: web, torrent, game, database ... everything. Good luck!

If I can clarify something else, ask in the comments.

Regarding the error " java.net.ConnectException: Connection refused "; the same is usually related to the lack of a ServerSocket available in the IP: PORT specified. If it were the case that your HOST was not being resolved to a valid IP, the error would possibly be: UnknownHostException .

So, I suggest you do two checks: (1) is your server really running? (may seem silly, but as you are testing more than one client, it may be that when closing the first connection your server has closed); (2) is your HOST "myserver.ddns.net" actually being resolved to the IP of the machine where the server is running?

For example: when I ping the name of my machine, the expected response is about PING minha_maquina (127.0.1.1) . When I do the same for Google: PING google.com (173.194.42.163) . In case, you have to see if your PING meuservidor.ddns.net resolves to the IP of the machine where your server software is running.

    
25.02.2015 / 19:28