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

时间:2014-11-04 12:58:55

标签: java tcp

我是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);
    }
}

这些是例外.. !!

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 个答案:

答案 0 :(得分:4)

local host替换为localhost