Search code examples
javaswingserversocket

not able to get inputstream in server socket file transfer


I have developed a screen in swings to download a file from the server. The whole concept works fine when i click the download button once. But when i click the download button second time, i find that the code pauses in getting the inputstream.(this i have followed it using the sysouts shown.)

Below shown are the two separate code snippets in two different files. TCPClient has the serversocket codings whereas the clientUI has the ui components which calls the TCPSever method to accept a socket and for requesting purpose.

In the tcp client side:

  public TCPClient() throws Exception{
    System.out.println("Inside TCPClient constructor---");
    clientSocket = new Socket("localhost", 3500);
    System.out.println("After creating socket instance---");
    oos = new ObjectOutputStream(clientSocket.getOutputStream());
    System.out.println("after getting the ouput stream---");
    ois = new ObjectInputStream(clientSocket.getInputStream());
    System.out.println("after getting the input stream.");
    }

In the Client UI:

private void downloadButton_actionPerformed(ActionEvent e) throws Exception 
{ 
    Object selectedItem = contentsList.getSelectedValue();
        System.out.println("selectedItem---"+selectedItem);
        new TCPClient().downloadContents(nodeName,selectedItem.toString());
    }
} 

Kindly provide me a solution for this...

Below is the server code:

    public void listening() throws Exception{
        ServerSocket ss = new ServerSocket(3500);
            System.out.println( "DataServer Is Listening..." );

            while( true )
            {
                Socket soc = ss.accept();

            ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());                                                            
                ObjectOutputStream oos = new ObjectOutputStream( soc.getOutputStream() );
String input = ( String ) ois.readObject( );

if(input.startsWith("downloadContents")){
            String nodeName = ois.readObject().toString();
            String contentName = ois.readObject().toString();
            List contentsForNode = DBServer.getContentsForNode(nodeName);
            for(Object obj : contentsForNode){
                if(obj.toString().contains(contentName)){
                    new FileServer().send(obj.toString());
                    break;
                }
            }
        }

    }
    }

    public static void main( String[] args ) 
        {
            TCPServer obDataServer  = new TCPServer();

            try
            {
                obDataServer.listening();
            }
            catch ( Exception ioe )
            {
                ioe.printStackTrace();
            }

        }

Solution

  • At a guess, your server is single-threaded and is still reading its input stream, because you haven't closed the client socket. But it's anybody's guess until you post the relevant server code.