为什么这个 Java TCP client/server 不起作用?
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。
public static void main(String[] args) 抛出 IOException {
新服务器(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 上监听。
端口号需要一致。
客户:
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。
public static void main(String[] args) 抛出 IOException { 新服务器(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 上监听。
端口号需要一致。