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.