聊天代码只能在有线局域网上使用,而不能在无线局域网上使用

时间:2018-10-09 18:27:13

标签: java networking

我正在尝试转换这些程序以适应无线网络和有线网络。

此Java聊天代码在有线LAN上效果很好,但在无线局域网上效果不好。为什么? 客户端和服务器上的端口和IP地址相同。有线和无线IP地址与有线之间的连接性有什么区别? 需要进行哪些更改?

这是服务器:

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

public class servTest 
{
    public static final int PORT = 9000;
    public static int timer = 20000; //Arbitrary timer 

public static void main(String[] args) throws IOException
      {
          ServerSocket s= new ServerSocket(PORT);        
          InetAddress []addr = InetAddress.getLocalHost();
          String str = "";
          boolean inOpen = true,outOpen = true;
          Socket socket = new Socket();
          do
           {     
             String thisUser="";
             try
               {
                 for(InetAddress a:addr)
                  System.out.println("\n\n\n  Server address "
                        +a.toString());
                  BufferedReader in;  
                  PrintWriter out;    
              while(timer > 0)
                    {
                        timer--;
                        socket = s.accept();
                        in   = new BufferedReader (      
                                new InputStreamReader(   
                                socket.getInputStream()));

                        out = new PrintWriter(new BufferedWriter
                                (new OutputStreamWriter(        
                                socket.getOutputStream())),    
                                true);
      //   input from stream **********************             
                        try
                          {
                             in.ready();
                             str = in.readLine();     //get network 
                             System.out.println(str); //print packet  
                             inOpen = true;
                          }
                        catch(Exception e)
                          {
                             inOpen = false;  
                           }
                        try
                          {
                             out.println("SERVER timer" + timer);  
                             outOpen = true;
                          }
                        catch(Exception e)
                          {
                             outOpen = false;  
                             System.out.println("Exception "+e + 
                                   " output not open");
                           }
                    } // END network loop    


              }                  

         finally
           {

               //   close stream **********************             
                 if (inOpen || outOpen)
                   {     
                     inOpen = false;
                     outOpen = false;
                     socket.close();
                    } 

                 s.close();
                 String message = "Socket closed.\n\n";

           }
      }
     while (true);  // infinite loop for test   
  }
}

这是客户:

import java.util.*;
import java.io.*;
import javax.swing.*;

    import java.net.*;
    //import java.util.Random;

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

              InetAddress localAddr =  InetAddress.getLocalHost();
              String ServerAddress = "";
              ServerAddress = JOptionPane.showInputDialog(
                      "Enter server ip address.","127.0.0.1");
              int PORT = 9000;                //Must be the same as the server.

              InetAddress addr =
                  InetAddress.getByName(ServerAddress); 

              Socket socket;
              BufferedReader netIn;//Use BufferedReader for input from
                                          //network
              PrintWriter netOut;  //Use PrintWriter for output to the 
                                          //network.
              Scanner myIn = new Scanner(System.in);
              String sentence = "";                           
              boolean inOpen = true,outOpen = true;
              int x = 0;
              String str = "";
              while (true)   //infinite loop to test
                  {
                    pause(1);
                    socket = new Socket(addr,PORT);
                    outOpen = true;
                    netIn = new BufferedReader (  //attach netIn to input 
                        new InputStreamReader(    //from the network
                        socket.getInputStream()));//(inputStream)

                    netOut = new PrintWriter(new BufferedWriter//Attach netOut to
                        (new OutputStreamWriter(        //output to the
                        socket.getOutputStream())),     //network
                        true);
                        x++;
          //   output to stream **********************              


                        System.out.print("..............type.......................");

                        sentence = myIn.nextLine();      
                        try
                           {
                             netOut.println("CLIENT ****** " // output to network
                                   +x+" "+localAddr + ": " + sentence
                                   );
                           }
                        catch(Exception e)
                           {
                             outOpen = false;
                             System.out.println("Exception "+e + " output not open");
                             socket.close();
                           }
           //   input from stream **********************                
                         inOpen = true;


                         try
                          {
                             netIn.ready();
                             str = netIn.readLine(); //get network info
                             if(str.equals("END")) break;
                             System.out.println(       //print packet on screen 
                                   "Server echoing input "+str);
                          }
                         catch(Exception e)
                          {
                             System.out.println("Exception "+e + " input not open" + x);
                             inOpen = false;
                             socket.close();
                            } 
                      }        
     }               
    public static void pause (long r)
    {
        try
           {
               Thread.sleep(r);
           }
        catch (Exception e) 
           {
               out.println(" sleep error " + e);
            }          
    }
    }

0 个答案:

没有答案