Client:
import java.io.*;
import java.net.*;
class Cliente {
private boolean loop = true;
private int delay = 3000;
private int id;
public Cliente(int id){
this.id = id;
}
public void execute() throws IOException, InterruptedException{
int nMsg = 0;
String msg = "Enviou a seguinte mensagem: ";
while (loop){
Socket client = new Socket("host",53300);
DataOutputStream fromClientOutPut = new DataOutputStream(client.getOutputStream());
fromClientOutPut.writeBytes("Cliente " + this.id + ": " + msg + " " + nMsg + " " );
client.close();
Thread.sleep(this.delay);
}
}
public static void main(String argv[]) throws IOException, InterruptedException{
BufferedReader readClientId = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Digite o ID do cliente: ");
int id = Integer.parseInt(readClientId.readLine());
Cliente cliente = new Cliente(id);
cliente.execute();
}
}
Server:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Servidor {
public static void main(String[] args) throws IOException {
new Servidor(12345).executa();
}
private int porta;
private List<PrintStream> clientes;
public Servidor(int porta) {
this.porta = porta;
this.clientes = new ArrayList<PrintStream>();
}
public void executa() throws IOException {
double num1, num2, total = 0.0;
char opr = '\n';
ServerSocket servidor = new ServerSocket(this.porta);
System.out.println("Porta " + porta + " aberta!");
while (true) {
Socket cliente = servidor.accept();
System.out.println("Nova conexao com o cliente " + cliente.getInetAddress().getHostAddress());
PrintStream ps = new PrintStream(cliente.getOutputStream());
this.clientes.add(ps);
ObjectOutputStream resultado = new ObjectOutputStream(cliente.getOutputStream());
ObjectInputStream dados = new ObjectInputStream(cliente.getInputStream());
num1 = dados.readDouble();
num2 = dados.readDouble();
opr = '+';
total = (num1 + num2);
resultado.writeDouble(total);
resultado.writeChar(opr);
resultado.flush();
resultado.close();
dados.close();
System.out.println("operacao realizada");
}
}
}
I can execute the server code and start the connection, but after that, i get this error:
Exception in thread "main" java.net.ConnectException: Conexão recusada
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Cliente.execute(Cliente.java:16)
at Cliente.main(Cliente.java:28)
What error this could possible be? Is it something on the Client or Server Code? Thanks for the answers.
Socket client = new Socket("host",53300);
Here you are connecting to port 53300.
public static void main(String[] args) throws IOException { new Servidor(12345).executa(); }
private int porta;
private List<PrintStream> clientes;
public Servidor(int porta) {
this.porta = porta;
this.clientes = new ArrayList<PrintStream>();
}
public void executa() throws IOException {
double num1, num2, total = 0.0;
char opr = '\n';
ServerSocket servidor = new ServerSocket(this.porta)
The net effect of all this is to listen at port 12345.
The port numbers need to agree.