为什么这个Java TCP客户端/服务器不起作用?

时间:2015-10-15 00:00:46

标签: java tcp tcpclient

客户端:

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();
    }
}

服务器:

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");
        }

    }

}

我可以执行服务器代码并启动连接,但之后,我收到此错误:

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)

这可能是什么错误?它是客户端还是服务器代码?谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

Socket client = new Socket("host",53300); 

您将连接到端口53300。

public static void main(String [] args)抛出IOException {         新的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)

所有这一切的净效果是在12345端口收听。

端口号需要同意。

相关问题