Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:46:52+00:00 2026-06-01T23:46:52+00:00

I have a case where the client after establishing a connection with the server,

  • 0

I have a case where the client after establishing a connection with the server, receives one file and when the same connection was used ( persistent ) I end up getting this error mentioned above.
Below is the implemented code:

Scanner in = new Scanner(file);

this.clientSocket = new Socket(this.host,this.port);

this.os = new DataOutputStream(this.clientSocket.getOutputStream());

this.is = this.clientSocket.getInputStream();

while(in.hasNextLine()){
  newFile = in.nextLine();
  System.out.println(newFile);

  this.os.writeBytes(newFile + '\n');
  this.os.flush();
  scanFileList();
  writeFile();

 }

and the server side implementation is:

 final class HttpRequest implements Runnable {
   final static String CRLF = "\r\n";
   Socket socket;
   static String dir;
   BufferedOutputStream outToClient = null;
   // Constructor
   public HttpRequest(Socket socket) throws Exception {
      this.socket = socket;
      dir = "C:\\Users\\";
 }


      // Implement the run() method of the Runnable interface.
 public void run() {
     try {

       // Get a reference to the socket's input and output streams.
       InputStream is = socket.getInputStream(); 
       outToClient = new BufferedOutputStream(socket.getOutputStream());
       processRequest(is,outToClient);
         } catch (Exception e) {
               System.out.println(e);
         }
 }

private void processRequest(InputStream is,BufferedOutputStream os) throws Exception {

    // Set up input stream filters.
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    // Get the request line of the HTTP request message.
    String fileName = br.readLine();



    // Prepend a "." so that file request is within the current directory.
    System.out.println(fileName);
    // Open the requested file.
    File myFile = null ;

    boolean fileExists = true ;

    myFile = new File(dir + fileName);

    FileInputStream fis = null ;
    try {
      fis = new FileInputStream(dir + fileName);
      System.out.println(fis);
    } catch (FileNotFoundException e) {
        fileExists = false ;
    }

    // Debug info for private use
    System.out.println("Incoming!!!");


   // Send the entity body.
   if (fileExists) {
    sendBytes(myFile, os);
    //fis.close();
   } 
   // Close streams and socket.
   is.close();
   os.close();
   br.close();
   socket.close();
   }

   private static void sendBytes(File myFile, 
    BufferedOutputStream os) throws Exception {
         // Construct a 1K buffer to hold bytes on their way to the socket.
         byte[] mybytearray = new byte[(int) myFile.length()];


         FileInputStream fis = null;
         // Copy requested file into the socket's output stream.
         try {
           fis = new FileInputStream(myFile);
         } catch (FileNotFoundException ex) {
            // Do exception handling
         }
         BufferedInputStream bis = new BufferedInputStream(fis);

         try {
           bis.read(mybytearray, 0, mybytearray.length);
           os.write(mybytearray, 0, mybytearray.length);
           os.flush();

           // File sent, exit the main method
           return;
         } catch (IOException ex) {
         // Do exception handling
           }
        }

   }

The error happens when the program on the client side tries to write to the server through: this.os.writebytes(newFile + /n);

  Testfile01.bmp

  writing

  saved

  Testfile02.bmp

Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at TCPClientPersistentNp.openSocket(TCPClientPersistentNp.java:53)
at TCPClient.main(TCPClient.java:66)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:46:53+00:00Added an answer on June 1, 2026 at 11:46 pm

    I’m not going to try to read your code in detail (see comment above …), but there is clearly something very strange / wrong about it.

    Put simply, if you are going to talk to an HTTP-based server, you can’t just open a socket and write stuff. Your client has to create well-formed HTTP requests, and process the HTTP responses that come back.

    The exception on the client-side is happening because the server side … in fact YOUR CODE … has closed the connection at the other end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quite specific client-server design case and I want to ask for
Question: How do you reconnect a client to the server after you have issued
So I have a case of client-itus here. The client is whining and my
I have a case where i'm getting formatted text with <a href ...> ,
I have a case sensitive SERVER (SQL_Latin1_General_CP1_CS_AS) but the Database is Case insensitive (SQL_Latin1_General_CP1_CI_AS).
How is a socket used by an HTTP client properly closed after transmitting the
I have something similar to an online chat client. When one user sends a
I have to add e-mail notifications to a client server application. Notifications happen as
I'm writing a program that will have both a server side and a client
the client writes 5 bytes to the socket after every 1 second. the server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.