Results 1 to 4 of 4
  1. #1
    forex is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default sending file from client to server - socket closed error

    Hi, the problem I am getting is "Error: Socket closed" from the client side. Now from my understanding what happens is that when i close an input or output stream it closes the connection as well. Am I right in saying this? What my program does is writes a file from client to server and I would like to send a simple message from the server back to the client after the file has finished writing, confirming the file length. The file won't finish writing unless i close the stream which closes the connection (so i cant send the confirmation msg back).
    Is there a way i can get around this? any help will be much appreciated

    Server Code
    Java Code:
    public static void main(String[] args) {
                BufferedInputStream bis;
                BufferedOutputStream bos;
                int num;
            
             try {
                int i = 1;
                ServerSocket s = new ServerSocket(8189);
                Socket incoming = s.accept();
                System.out.println("Spawning " + i);
              
                try {
                      try{
                        
                    InputStream inStream = incoming.getInputStream();
                    OutputStream outStream = incoming.getOutputStream();
    
                   BufferedReader inm = new BufferedReader(newInputStreamReader(inStream));
                    PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
    
                    String filename = inm.readLine();
                    System.out.println("Filename = " + filename);
                    out.println("ACK: Filename received = " + filename);
    
                    //RECIEVE and WRITE FILE
                    byte[] receivedData = new byte[8192];
                    bis = new BufferedInputStream(incoming.getInputStream());
                    bos = new BufferedOutputStream(new FileOutputStream(filename));
                    //length = bis.read(receivedData);
                    while ((num = bis.read(receivedData)) != -1){
                    bos.write(receivedData,0,num);
                }
                bos.close();
                bis.close();
    
                File receivedFile = new File(filename);
                long receivedLen = receivedFile.length();
                out.println("ACK: Length of received file = " + receivedLen);
                System.out.println("Length of received file = " + receivedLen);
    
                      } finally {
                    incoming.close();
                   }
                } catch (IOException e){
            e.printStackTrace();
            }
                } catch (IOException e1){
            e1.printStackTrace();
            }
        }
    Client
    Java Code:
      public static void main(String[] args) {
    
                BufferedInputStream bis;
                BufferedOutputStream bos;
                int num;
                byte[] byteArray;
    
            Socket s = null ;
            try{
                  s = new Socket("localhost",8189) ;
                  InputStream inStream = s.getInputStream() ;
                  OutputStream outStream = s.getOutputStream() ;
                  System.out.println("Connected to : " + s);
    
                  BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
                  PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
    
                  for (String x : args) {
                  File sentFile = new File(x);
                  out.println(sentFile);
                  long len = sentFile.length();
                  System.out.println(inm.readLine());
                  System.out.println("Sent File length = " + len);
    
                //SENDFILE
                bis = new BufferedInputStream(new FileInputStream(x));
                bos = new BufferedOutputStream(s.getOutputStream( ));
                byteArray = new byte[8192];
                while ((num = bis.read(byteArray)) != -1){
                    bos.write(byteArray,0,num);
                }
    
                 bos.close();
                 bis.close();
    
                System.out.println(inm.readLine());
                  }
            }catch (Exception e) {
                 System.out.println(e) ;
            }
        }
    I have tried flushing bos instead of closing but the client and server just get deadlocked and the file never writes. thanks.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I had a similar problem a few months back. The problem is on the receiving end.The server never gets to -1 as the client closes after end of file. i fixed it by having the receiving code i.e. server getting the file length from the client first and then using that length as the exit condition of the while loop on the server receive file code instead of -1. let me know if this worked for you.

  3. #3
    forex is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    thank you so much, I just tried this and it fixed the problem.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. Sending Text File --- Server-To-Client
    By nigamsir in forum Networking
    Replies: 1
    Last Post: 03-08-2010, 03:45 PM
  2. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  3. Replies: 1
    Last Post: 02-10-2009, 09:16 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •