套接字客户端和服务器在不同的pc上

时间:2014-05-29 09:19:44

标签: java sockets client

我正在我的电脑上运行服务器套接字并试图通过在连接到同一网络的另一台电脑上启动的客户端连接到该服务器套接字。

- >服务器代码:

public class Server {

public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException
{

   ServerSocket server = new ServerSocket(4444);

   System.out.println("Waiting for clients to connect...");

   while(true)
   {
      Socket s = server.accept();
      InetAddress clientAddress = s.getInetAddress();
      System.out.println("Incoming connection from: " + clientAddress.getHostName() + "[" + clientAddress.getHostAddress() + "]");
      s.close();
   }
}

- >客户代码:

public class Client {


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

   Socket s = new Socket("192.168.1.2", 4444); //192.168.1.2 is ip address of pc where runnin server; both pc are connected to same network

   s.close();
}

当我运行客户端时,我收到此错误

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at social_network.Client.main(Client.java:29)

Java结果:1

似乎无法从客户端访问服务器......怎么回事?

0 个答案:

没有答案