1
I need to do a college job where it is necessary to do a TCP/IP communication something like a "chat chat" is what I have in mind to do.
But some questions arose, I don’t know exactly how to ask them so I made a design in two ways that I think should be the architecture of this type of communication. I’m using the language Java
for the program, but I decided not to specify the language in the Title so that if someone from another language has the same doubt can heal with the examples from here. But if you can answer in Java
I thank you so much.
First architecture I doubt
If this architecture is correct, then I will need to open a Thread for each Private connection that app1
go do?
For example: A app1
communicates with the app2
with the porta 99998
and if you are going to talk to app3
will need to make a Thread
and communicate with the app3
through the porta 99997
?
Second architecture I doubt
In this architecture all messages will go through Servidor Central
and he will be in charge of sending the message to a particular application.
If this is the right architecture, how can I make app1
talk to the app2
? I know you’re gonna go through Servidor Central
but how will he know that message from app1
goes to the app2
and not to another?
And how do I send the message that’s already on Servidor Central
to the app
chosen by app1
?
The doubts are those, I hope they are clear, if they are not please ask me to warn you so that I can correct and make as clear as possible.
Maybe the code I made can help in something, I do not know how but I will publish it, is in a very bad way because I do not know totally the methods and I have not formulated a good way to make a better communication.
Clienteum
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
//import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ClienteUm {
private static Socket socket;
public static void main(String[] args) {
try {
socket = new Socket("127.0.0.3", 12345);
DataOutputStream fluxoSaidaDados = new DataOutputStream(socket.getOutputStream());
BufferedReader leitorBuffered = new BufferedReader(new InputStreamReader(System.in));
escreverMensagemAoServidor(fluxoSaidaDados, leitorBuffered);
lerMensagemServidor();
} catch (IOException iec) {
System.out.println(iec.getMessage());
}
}
private static void escreverMensagemAoServidor(final DataOutputStream fluxoSaidaDados, final BufferedReader leitorBuffered)
throws IOException {
new Thread() {
public void run() {
String mensagemSaida;
try {
while (true) {
mensagemSaida = leitorBuffered.readLine();
fluxoSaidaDados.writeUTF("Mensagem do Cliente (1): " + mensagemSaida + "=127.0.0.2");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
private static void lerMensagemServidor() {
new Thread(){
public void run(){
try {
DataInputStream fluxoEntradaDados = new DataInputStream(socket.getInputStream());
while(true){
String mensagemOutroClienteQueVeioPeloServidor = fluxoEntradaDados.readUTF();
System.out.println(mensagemOutroClienteQueVeioPeloServidor);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
Central Server
He’s being called in the code of ServidorUm
because it was already a code I was testing before.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.SynchronousQueue;
public class ServidorUm {
private static ServerSocket servidorSocket;
private static List<Socket> socketsConectados;
public static void main(String[] args) {
try {
servidorSocket = new ServerSocket(12345);
socketsConectados = new ArrayList<Socket>();
do {
Socket socket = servidorSocket.accept();
DataInputStream fluxoEntradaDados = new DataInputStream(socket.getInputStream());
System.out.println("Cliente "+ socket.getLocalAddress().getHostAddress() +" se conectou! ");
socketsConectados.add(socket);
lerMensagemDoCliente(fluxoEntradaDados);
} while (true);
} catch (Exception e) {
System.err.println("Ops! " + e.getMessage());
}
}
private static void lerMensagemDoCliente(final DataInputStream fluxoEntradaDados) {
new Thread() {
public void run() {
String mensagemEntrada = "";
try {
while (true) {
mensagemEntrada = fluxoEntradaDados.readUTF();
String[] teste = mensagemEntrada.split("=");
String mensagem = teste[0];
String paraQuemEnviar = teste[1];
System.out.println("\n" + mensagem);
Socket socketQueReceberaMensagem = socketsConectados.stream()
.filter(x -> x.getLocalAddress().getHostAddress().equals(paraQuemEnviar))
.findFirst().get();
DataOutputStream fluxoSaidaDados = new DataOutputStream(socketQueReceberaMensagem.getOutputStream());
fluxoSaidaDados.writeUTF(mensagem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
Clientetwo ##
import java.io.BufferedReader;
import java.io.DataInputStream;
//import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ClienteDois {
private static Socket socket;
public static void main(String[] args) {
try {
socket = new Socket("127.0.0.2", 12345);
DataOutputStream fluxoSaidaDados = new DataOutputStream(socket.getOutputStream());
BufferedReader leitorBuffered = new BufferedReader(new InputStreamReader(System.in));
escreverMensagemAoServidor(fluxoSaidaDados, leitorBuffered);
lerMensagemServidor();
} catch (IOException iec) {
System.out.println(iec.getMessage());
}
}
private static void lerMensagemServidor() {
new Thread(){
public void run(){
try {
DataInputStream fluxoEntradaDados = new DataInputStream(socket.getInputStream());
while(true){
String mensagemOutroClienteQueVeioPeloServidor = fluxoEntradaDados.readUTF();
System.out.println(mensagemOutroClienteQueVeioPeloServidor);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
private static void escreverMensagemAoServidor(final DataOutputStream fluxoSaidaDados, final BufferedReader leitorBuffered)
throws IOException {
new Thread() {
public void run() {
String mensagemSaida;
try {
while (true) {
mensagemSaida = leitorBuffered.readLine();
fluxoSaidaDados.writeUTF("Mensagem do Cliente (2): " + mensagemSaida + "=127.0.0.3");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
Edit the question and add what you’ve tried to do.
– user28595
Good Articuno... I can’t start an implementation of something without first knowing how it works... So if my question is about how communication works, how can I have something implemented? I haven’t done anything unfortunately, the only thing I can do is edit the question and withdraw the "I’m doing" to "I wish to start a college job", so it helps you?
– Richard Willian
Your question can be considered too wide and be closed, so I gave the hint to add the code, after all there may be N ways to do this, which makes the doubt no longer be specific and be too broad.
– user28595
That’s why I sent two small images asking if any of the two ways are correct, if they are not correct, then it is simply up to who will answer (if you want) send another solution, I am right or wrong?
– Richard Willian
Okay, I just tried to help warn you about the closing (and you already have 2 votes to close this question as broad). It is your choice to apply my tips or not, but if you do not agree, it is your own right.
– user28595
Okay, but like I said above, how am I going to send something implemented, if I don’t have something implemented by not knowing how to start this implementation, agree?
– Richard Willian
Starting point: https://www.devmedia.com.br/java-sockets-creation-comunicacoes-em-java/9465 Ps.: I don’t think using ports is feasible to identify sender and recipient.
– Valdeir Psr
Thank you Valdeir Psr
– Richard Willian
I added the code, I hope it’s easier now, sorry for the lack of good practices and the "gambiarras" were the ways I found to get them to communicate having little experience with this type of application.
– Richard Willian