简体   繁体   中英

Socket instance wont connect to local server

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:

  • You are trying to create echo server, which means that you need to listen on 8080 instead of connecting to 8080?
ServerSocket serverSocket = new ServerSocket(8080);
Socket clientSocket = serverSocket.accept();
  • Assuming that your code is actually the IRC client. Do you have something that listens on port 8080 on 127.0.0.1? Try using netstat to verify that it is listening on 127.0.0.1:8080
  • Check that arguments[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.

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