在java中进行套接字编程时的异常

时间:2022-07-20 10:15:27

i am new at socket programming in java. while running the client code i face these exceptions. i don't understand how to overcome it ! here is my client code

我是java中socket编程的新手。在运行客户端代码时,我面临这些异常。我不明白如何克服它!这是我的客户端代码

import java.io.*;
import java.net.*;
public class SOCK_1_CLIENT {

    public static void main(String[] args) throws Exception {

        SOCK_1_CLIENT client = new SOCK_1_CLIENT();
        client.run();


    }

    public void run() throws Exception
    {
        Socket sock = new Socket("local host",5050);
        PrintStream ps = new PrintStream(sock.getOutputStream());
        ps.println("Hello to server from client");
        InputStreamReader ir = new InputStreamReader(sock.getInputStream());
        BufferedReader br = new BufferedReader(ir);
        String msg = br.readLine();
        System.out.println(msg);
    }
}

these are the exceptions..!!

这些都是例外.. !!

Exception in thread "main" java.net.UnknownHostException: local host
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at SOCK_1_CLIENT.run(SOCK_1_CLIENT.java:18)
    at SOCK_1_CLIENT.main(SOCK_1_CLIENT.java:11)

1 个解决方案

#1


4  

Replace local host with localhost.

用localhost替换本地主机。

#1


4  

Replace local host with localhost.

用localhost替换本地主机。