Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1234731

fp.bugs 3189: Win32 only: socket.close() does not close tcp connection

XMLWordPrintable

    • 1.0.2
    • x86
    • windows_nt
    • Verified

      Michael Kyle, ###@###.###

      Win32 only: Socket.close() does not close the TCP connection if the socket was opened
      from a SocketServer.accept() call.

      Steps to reproduce
      Compile and run the attached code
      Telnet to the Server on port 10000
      Type any text, press <Enter> to end the line.
      // Note: the connection should be closed by the server

      // Source code
      import java.net.*;
      import java.io.*;


      public class SocketServerTest
      {
         boolean done = false;
         Socket clientSocket = null;
         DataInputStream is = null;
         int socketNo = 10000;

         public void run()
         {
            // local variables
            String sTemp;

            try
            {
               sock = new ServerSocket(socketNo);
               System.out.println("Opening socket on port: " + socketNo);
            }
            catch ( Exception e )
            {
               System.out.println("cannot create the server socket");
            }

            while ( ! done )
            {
               try
               {
                  System.out.println("Waiting for connection");
                  clientSocket = sock.accept();

                  // read from the socket
                  is = new DataInputStream(clientSocket.getInputStream());
                  sTemp = is.readLine();
                  System.out.println(sTemp);
                  if ( sTemp.equals("done") )
                     done = true;
              
                  // close the socket
                  clientSocket.close();
               }
               catch ( Exception e )
               {
                  System.out.println("cannot accept client connection");
               }
            } // while

            System.out.println("Exiting .... ");
         } // run()


         public static void main(String args[])
         {
          // passing false indicates that the server is not running as
          // an applet rather than an application
            Junk ser = new Junk();
            ser.run();
         }

      }

      _______1/30/96 : Jim Hagen ( hagen@eng) added customer-suggested fix ____
      _______below is also code example w/ client for a less manual test____
      This information is from
      Alan Pulliam
      Synchrologic, Inc.
      430 10th St. NW
      Suite N005
      Atlanta, GA 30318
      (404)876-3209
      ###@###.###
      via fp.bugs 3137

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



      class Client
        {
        // sockets
        private Socket control = null;

        // control streams
        private PrintStream cout = null;
        private DataInputStream cin = null;

        public Client(String host, int port_number) throws IOException
        {
           control = new Socket(host, port_number);

         // Control Streams
      cout = new PrintStream(new
      BufferedOutputStream(control.getOutputStream()),true);
      cin = new DataInputStream(new
      BufferedInputStream(control.getInputStream()));
        }


         private Socket openDataConnection() throws IOException
         {
            ServerSocket portSocket;
            String portCmd;
            InetAddress myAddress = null;
            int shift;
            IOException e;
            try {
               myAddress = InetAddress.getByName(null); }
            catch (UnknownHostException x) // can't happen
               {
               }
               
            portSocket = new ServerSocket(0);
      String addr = myAddress.toString();
            int index = addr.lastIndexOf('/');
      String ipaddr = addr.substring(index + 1);
      portCmd = ipaddr + " " + portSocket.getLocalPort();

      System.out.println("sending port info: " + portCmd);
      cout.println(portCmd);

      System.out.println("waiting for data connection");
            Socket dataSocket = portSocket.accept();
            portSocket.close();
      System.out.println("accepted data connection on socket " + dataSocket.toString());
            return dataSocket;
         }
         public void sendData() throws IOException
         {
            int i;
            Socket d = openDataConnection();
            OutputStream dout = d.getOutputStream();
             
      byte buf[] = new byte[2048];

      for (i = 0; i < 100; ++i)
                dout.write(buf,0,buf.length);
      System.out.println(i + " " + buf.length + "K buffers sent");

             d.close();
         }

         public void getReply() throws IOException
         {
            String reply = cin.readLine();
            System.out.println("reply received: " + reply);
         }
      }

      // Program simulating FTP Client to demonstrate Socket.Close() bug
      // 1) opens control connection to server
      // 2) creates and listens on data port
      // 3) sends data port to server
      // 4) accepts connection on data port
      // 5) closes listener socket
      // 6) sends data on data port
      // 7) closes data port
      // 8) waits for server acknowledgement on control port
      public class jstest
      {
        public static void main(String args[])
          {
          System.out.println("jstest:");

          try
            {
      // Substitute your server address and port
      // I used my local machine for client and server
      Client c = new Client("morton",5000);


            System.out.println("sending data.");
      c.sendData();

      System.out.println("waiting for reply");
      c.getReply();

            }
          catch(Exception e) // catch any exception
            {
            System.out.println("Exception caught: " + e.getMessage()
      );
            e.printStackTrace();
            }
          }
        }


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

      // Program simulating FTP server to demonstrate Socket.Close() bug

      public class jsserv
        {
        public static void main(String args[])
          {
          int i;
          System.out.println("jsserv:");

          try
            {
            int port_number = 5000;
            int count = 10;
            
            ServerSocket socket = null;
            try
              {
              socket = new ServerSocket(port_number, count);
              }
            catch(IOException e)
              {
              System.out.println("Can not open socket");
              System.exit(0);
              }

            System.out.println("waiting for client connection.");
            Socket connection = socket.accept();

      System.out.println("accepted socket connection");
      DataInputStream cin = new DataInputStream(new
      BufferedInputStream(connection.getInputStream()));

      PrintStream cout = new PrintStream(new
      BufferedOutputStream(connection.getOutputStream()),true);

      System.out.println("waiting for data port info");
      String line = cin.readLine();
      System.out.println(line);
            StringTokenizer st = new StringTokenizer(line);

            String ipaddr = st.nextToken();
      String port = st.nextToken();

      System.out.println("received data port info, connecting to " + ipaddr + " on port " + port);
      Socket s = new Socket(ipaddr,Integer.parseInt(port));

            //BufferedInputStream din = new
      //BufferedInputStream(s.getInputStream());
            InputStream din = s.getInputStream();
      byte buf[] = new byte[2048];

      i = 0;
      int bytes = 0;
      int n = 0;
      try
      {
                System.out.println("waiting for data");
            while (true)
      {
                  n = din.read(buf,0,buf.length);
      if (n == -1)
      throw new Exception("end of stream");
                  bytes +=n;
      ++i;
            }
          }
            catch (Exception e)
               {
               System.out.println("exception caught on buffer " + i + " " +e.getMessage() );
               System.out.println(bytes + " bytes read");
               e.printStackTrace();
               }

            s.close();

      cout.println("Done");
            }
          catch(Exception e) // catch any exception
            {
            System.out.println("Exception caught: " + e.getMessage() );
            e.printStackTrace();
            }
          }
        }


            dconnellsunw David Connelly (Inactive)
            kasmithsunw Kevin Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: