我一般是套接字编程/网络的新手。我正在尝试创建一个简单的IRC,它是一个回显服务器。我正在为套接字对象使用127.0.0.1和端口8080,但我不断收到ConnectionException。不用在计算机上进行任何设置以允许连接吗?
客户端类(我需要创建一个服务器类,也许可以处理允许连接的内容?)
package client.build;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.util.Scanner;
public class Client {
private static Socket socket;
private static DataInputStream inputStream;
private static DataOutputStream outputStream;
// This might not be needed
private static Scanner input = new Scanner(System.in);
private final String[] arguments;
public Client(String[] arguments) throws IOException {
this.arguments = arguments;
}
public void connect() throws IOException, InterruptedException {
try {
socket = new Socket(arguments[0], Integer.parseInt(arguments[1]));
inputStream = new DataInputStream(System.in);
outputStream = new DataOutputStream(socket.getOutputStream());
} catch(ConnectException e) {
System.out.println("failed to connect to echo server");
Thread.sleep(600);
System.exit(0);
}
}
}
答案 0 :(得分:0)
几件事要检查:
ServerSocket serverSocket = new ServerSocket(8080);
Socket clientSocket = serverSocket.accept();
netstat
来验证它是否正在监听127.0.0.1:8080 arguments[0]
确实是127.0.0.1。您可以尝试使用localhost的null