简体   繁体   中英

Java Socket : I'm trying to generate random port numbers between a range, but connection is refused, but when I'm using a integer, it works

Client :

public class FileClient {  

static int Min = 1050;
static int Max = 15000;
static int PORT = Min + (int)(Math.random() * ((Max - Min) + 1));

    public static void main(String[] args) throws IOException {  
        Socket sock = new Socket("localhost", PORT);  
        if (PORT>=2000&&PORT<=2050) {
            System.out.println("The rare disconnection");
            sock.close();
        }
        System.out.println("Connection Opened");
        // sendfile  
        File myFile = new File("input.txt");  
        byte[] mybytearray = new byte[(int) myFile.length()];  
        FileInputStream fis = new FileInputStream(myFile);  
        BufferedInputStream bis = new BufferedInputStream(fis);  
        System.out.println("Input Stream Opened");
        bis.read(mybytearray, 0, mybytearray.length);  
        OutputStream os = sock.getOutputStream();  
        os.write(mybytearray, 0, mybytearray.length);  
        System.out.println("Data written to output file");
        os.flush();  
        bis.close();  
        System.out.println("Input Stream Closed");
        sock.close();  
        System.out.println("Connection Closed");
    }  
}  

Server:

public class FileServer {      

   public static void main (String[] args ) throws IOException {     
      int XPORT = FileClient.PORT;
      int bytesRead;  
      ServerSocket serverSocket = new ServerSocket(XPORT);
      System.out.println("Listening for a client");
      while(true) {  
         Socket clientSocket = null;  
         clientSocket = serverSocket.accept();  
         InputStream in = clientSocket.getInputStream();  
         // Writing the file to disk  
         // Instantiating a new output stream object  
         OutputStream output = new FileOutputStream("output.txt");  
         byte[] buffer = new byte[1024];  
         while ((bytesRead = in.read(buffer)) != -1) {  
            output.write(buffer, 0, bytesRead);  
         }  
         // Closing the FileOutputStream handle  
         output.close();  
      }
   }
} 

Java Socket : I'm trying to generate random port numbers between a range, but connection is refused, but when I'm using a integer, it works. This is working for one value. I want the server to deny the connection for a few port numbers (Ex: 2000-2100) And want the connection to be accepted and file transfer to be completed. When using integers, my file transfer works perfectly.

When you run the client you get one random number. When you run the server you get another. They're almost certainly different. There's no reason to expect this to work, and there's no reason to even try. Use a fixed port number. There are plenty available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM