1

这是我正在尝试工作的代码,但我收到了关于 localhost 不可用的 IOException。

public class MultiThreadChatClient implements Runnable{

    // Declaration section
    // clientClient: the client socket
    // os: the output stream
    // is: the input stream

    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;

    public static void main(String[] args) {

    // The default port 

    int port_number=5050;
        String host="localhost";


    // Initialization section:
    // Try to open a socket on a given host and port
    // Try to open input and output streams
    try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }

    // If everything has been initialized then we want to write some data
    // to the socket we have opened a connection to on port port_number 

        if (clientSocket != null && os != null && is != null) {
            try {

        // Create a thread to read from the server

                new Thread(new MultiThreadChatClient()).start();

        while (!closed) {
                    os.println(inputLine.readLine()); 
                }

        // Clean up:
        // close the output stream
        // close the input stream
        // close the socket

        os.close();
        is.close();
        clientSocket.close();   
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           

    public void run() {     
    String responseLine;

    // Keep on reading from the socket till we receive the "Bye" from the server,
    // once we received that then we want to break.
    try{ 
        while ((responseLine = is.readLine()) != null) {
        System.out.println(responseLine);
        if (responseLine.indexOf("*** Bye") != -1) break;
        }
            closed=true;
        } catch (IOException e) {
        System.err.println("IOException:  " + e);
    }
    }
}

我得到的例外是:

无法获取与主机 localhost 的连接的 I/O

4

3 回答 3

0

错误可能是由于没有服务器监听连接请求。先启动服务端程序,再启动客户端

否则也试试这个:套接字构造函数需要第一个参数,InetAddress而不是String
你必须使用 getByName(String) 方法将主机初始化为套接字对象。修改您的套接字构造函数,如下所示:

clientSocket = new Socket(InetAddress.getByName(host), port_number);
于 2012-08-26T14:33:49.613 回答
0

检查是否在端口 5050 上启用了 TCP。

于 2012-08-27T06:14:17.693 回答
0

而不是localhost,提供您的本地 IP 地址。要知道本地 IP 地址,请转到命令提示符并键入ipconfig.

例子

public static void main(String[] args) {

    // The default port.
    int portNumber = 6679;
    // The default host.
    String host = "192.150.46.2";
于 2017-10-30T12:49:43.343 回答