4
Well, first of all, I’ve searched a lot of sites, including here, how to do this. I tested codes and modified 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 a server/client. It will send something like 5 Strings, to 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 a Servidor.jar
.
I’d like to do the Servidor.jar
work on a computer with no-ip installed.
Here’s 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/Client Main ----> Server/Client²
Afterward...
Server/Client² ---> Server/Client Main ----> Server/Client¹
The main Server/Client would be the Servidor.Jar
and the others, Chat.jar
.
Using a code similar to this code from above, I only received the message from "IOException e
" and in other ways, I was getting something like "Connect: refused connect
".
Well... in short, I need to send several strings, while I can still receive strings. I don’t know how to explain it properly or how to ask, I’m totally lost. I hope you understand.
NOTE: The code of the rest of the program is done. Only this is missing.
This part of the code is new and already working normal, now only left a problem, I wanted to change 127.0.0.1
, for an on-ip host connection, for example: meuservidor.ddns.net
Can you do that? 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 has to be something that does not involve releasing ports on the firewall/router.
Eduardo. I added some information about your mistake at the end of my reply. Let’s go.
– Nigini