Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 独立机器之间的套接字通信_Java_Sockets - Fatal编程技术网

Java 独立机器之间的套接字通信

Java 独立机器之间的套接字通信,java,sockets,Java,Sockets,采取以下基本方案: import java.io.*; import java.net.*; public class TestServer { public static void main(String[] args) throws IOException { ServerSocket listener = new ServerSocket(12345); try { while (true) {

采取以下基本方案:

import java.io.*;
import java.net.*;

public class TestServer {
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(12345);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
                    out.println(new java.util.Date().toString());
                    out.close();
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}


public class TestClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("0.0.0.0",12345); // Stack trace points to this line as the one with the error
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println(in.readLine());
            in.close();
            socket.close();
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
        }
    }
}
TestServer
程序等待客户机连接到它,以便它可以向客户机发送信息,在本例中是当前日期。在IDE(顺便说一句,JCreator)中的家用计算机上,我可以在同一台计算机上运行
TestServer
程序和
TestClient
程序,并获得所需的结果。问题是,当我在另一台计算机上运行
TestClient
程序并尝试连接到
TestServer
程序时,我会不断收到消息
IOException:Connection seeded


有什么方法可以让它工作吗?

您试图用错误的IP地址连接到服务器(0.0.0

您需要知道运行服务器程序的计算机的IP地址,并使用该地址在客户端程序中实例化套接字


请注意,应该可以从客户端计算机访问服务器。

从您的问题中不清楚您是否已将ip地址从“0.0.0.0”更改为远程计算机的ip地址,因为每台计算机都有自己的ip地址?那么为什么
getInetAddress()
服务器套接字的方法类返回
0.0.0.0/0.0.0