I'm new to socket programming/networking in general. I'm trying to create a simple IRC that's an echo server. I'm using 127.0.0.1 and port 8080 for my socket object but I keep getting a ConnectionException. Does it have to do with me not setting anything up on my computer to allow connections?
Client class (I need to make a Server class maybe that handles allowing connections?)
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);
}
}
}
Few things to check:
ServerSocket serverSocket = new ServerSocket(8080);
Socket clientSocket = serverSocket.accept();
netstat
to verify that it is listening on 127.0.0.1:8080arguments[0]
is really 127.0.0.1. You can try with the null
for localhost
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.