为什么我无法通过WiFi连接两台不同的笔记本电脑?

时间:2014-03-26 08:48:47

标签: java networking

这是一个简单的客户端 - 服务器程序。它在单台计算机中执行时工作正常。但是,在使用WiFi连接的两台不同笔记本电脑中执行时,它不起作用。 这是代码:

服务器:

public class Server
{
   private static int port;
   private ServerSocket serverSocket;

   public void GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(20000);

      while(true)
      {
         try
         {
            System.out.println("Waiting for client"); 
            Socket server = serverSocket.accept();
            System.out.println("Connected");
            DataInputStream in =new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to ");
            server.close();
         }
         catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }
         catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      port=9001;
      try
      {
         Server s=new Server();
         s.GreetingServer(port);
      }
      catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

客户端:

public class Client
{
    private static String serverName;
    public static void main(String [] args)
    {
      String sName = "192.168.xxxx.x";
      int port = 9001;
      try
      {
         System.out.println("Connecting to " + sName
                             + " on port " + port);
         Socket client = new Socket(sName, port);
         System.out.println("Connected");
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);

         out.writeUTF("Hello from " + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in = new DataInputStream(inFromServer);
         System.out.println("Server says " + in.readUTF());
         client.close();
      }catch(IOException e)
      {
      }
   }
}

可能是什么问题?让我开心.. 提前谢谢..

0 个答案:

没有答案