繁体   English   中英

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

[英]Why this Java TCP client/server doesn't work?

客户:

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)

这可能是什么错误? 客户端或服务器代码上有内容吗? 感谢您的回答。

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

在这里,您将连接到端口53300。

公共静态void main(String [] args)引发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)

所有这些的最终结果是侦听端口12345。

端口号需要同意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM