Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. java.net.SocketException: Software caused connection abort: socket write error

java.net.SocketException: Software caused connection abort: socket write error

Scheduled Pinned Locked Moved Java
csharpjavahelpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a while(true) loop. My ClientThread sends one image only. After that, it starts producing this exception. Here is what I have tried so far

    Overview

    1- MainThread starts ServerThread
    2- ServerThread start ClientThread
    3- ClientThread has a loop. In this loop, I read next image and pass it to MovieWriter (which writes it to DataOutputStream of clientSocket) with a delay of 50ms

    class ClientThread extends Thread
    {
    @Overrie
    public void run()
    {
    OutputStream outStream = clientSocket.getOutputStream(); // clientSocket = class variable
    DataOutputStream dataOutStream = new DataOutputStream(outStream);

    MovieWriter mv = new MovieWriter(dataOutStream);
    mv.WriterHttpHeader();
    while(true)
    {
      Thread.sleep(50);
      ByteArrayOutputStream image = ReadImage.GetNext(); // 'ReadImage' is static class which returns the next image
      mv.WriteNextImage(image.toByteArray());
    }
    

    }
    }

    class MovieWriter
    {
    public MovieWriter(DataOutputStream)
    {
    // set class variable
    }

    public void WriterHttpHeader()
    {
    String header = "HTTP/1.1 200 OK \r\n Content-Type: multipart/x-mixed-replace; boundary=--boundary";
    dataOutputStream.write(GetBytes(header)); // dataOutputStream = class variable; GetBytes() return bytes of header
    }

    public void WriteNextImage(byte[] data)
    {
    try
    {
    String response = "Content-Type: image/jpg \r\n" + "Content-Length: " + String.ValueOf(data.length);
    dataOutputStream.write(GetBytes(response)); // first write response header
    dataOutputStream.write(data); // then write image data
    } catch(Exception ex) {
    ex.printStackTrace();
    }
    }

    }

    Again, when the while loop in ClientThread runs, MovieWriter sends one image only. After that, it raises the said exception. What am I doing wrong? Thanks for anything you share :) NOTE: I cut this code short for better understanding. If you need more code, I can provide

    L 2 Replies Last reply
    0
    • D Django_Untaken

      Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a while(true) loop. My ClientThread sends one image only. After that, it starts producing this exception. Here is what I have tried so far

      Overview

      1- MainThread starts ServerThread
      2- ServerThread start ClientThread
      3- ClientThread has a loop. In this loop, I read next image and pass it to MovieWriter (which writes it to DataOutputStream of clientSocket) with a delay of 50ms

      class ClientThread extends Thread
      {
      @Overrie
      public void run()
      {
      OutputStream outStream = clientSocket.getOutputStream(); // clientSocket = class variable
      DataOutputStream dataOutStream = new DataOutputStream(outStream);

      MovieWriter mv = new MovieWriter(dataOutStream);
      mv.WriterHttpHeader();
      while(true)
      {
        Thread.sleep(50);
        ByteArrayOutputStream image = ReadImage.GetNext(); // 'ReadImage' is static class which returns the next image
        mv.WriteNextImage(image.toByteArray());
      }
      

      }
      }

      class MovieWriter
      {
      public MovieWriter(DataOutputStream)
      {
      // set class variable
      }

      public void WriterHttpHeader()
      {
      String header = "HTTP/1.1 200 OK \r\n Content-Type: multipart/x-mixed-replace; boundary=--boundary";
      dataOutputStream.write(GetBytes(header)); // dataOutputStream = class variable; GetBytes() return bytes of header
      }

      public void WriteNextImage(byte[] data)
      {
      try
      {
      String response = "Content-Type: image/jpg \r\n" + "Content-Length: " + String.ValueOf(data.length);
      dataOutputStream.write(GetBytes(response)); // first write response header
      dataOutputStream.write(data); // then write image data
      } catch(Exception ex) {
      ex.printStackTrace();
      }
      }

      }

      Again, when the while loop in ClientThread runs, MovieWriter sends one image only. After that, it raises the said exception. What am I doing wrong? Thanks for anything you share :) NOTE: I cut this code short for better understanding. If you need more code, I can provide

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It seems to me that your logic is the wrong way round. A web browser is a client application that requests information form a web server. So your 'client' should be a server, which sends images based on requests from the browser, rather than trying to push them to a browser that is not expecting them.

      D 1 Reply Last reply
      0
      • L Lost User

        It seems to me that your logic is the wrong way round. A web browser is a client application that requests information form a web server. So your 'client' should be a server, which sends images based on requests from the browser, rather than trying to push them to a browser that is not expecting them.

        D Offline
        D Offline
        Django_Untaken
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        So your 'client' should be a server .....

        - I have a 'ServerThread' (one and only one server thread) - The piece of code that serves images, upon request, is known as 'ClientThread' - This 'ServerThread' can start N number of 'ClientThread', against N number of requests from web browser. And YES. Good naming conventions help understand better. 'ClientThread' could better be named as 'RequestThread'. But the problem still remains. My browser display ONE picture, sent from my server. After that ONE picture, it starts throwing said exception.

        L 1 Reply Last reply
        0
        • D Django_Untaken

          Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a while(true) loop. My ClientThread sends one image only. After that, it starts producing this exception. Here is what I have tried so far

          Overview

          1- MainThread starts ServerThread
          2- ServerThread start ClientThread
          3- ClientThread has a loop. In this loop, I read next image and pass it to MovieWriter (which writes it to DataOutputStream of clientSocket) with a delay of 50ms

          class ClientThread extends Thread
          {
          @Overrie
          public void run()
          {
          OutputStream outStream = clientSocket.getOutputStream(); // clientSocket = class variable
          DataOutputStream dataOutStream = new DataOutputStream(outStream);

          MovieWriter mv = new MovieWriter(dataOutStream);
          mv.WriterHttpHeader();
          while(true)
          {
            Thread.sleep(50);
            ByteArrayOutputStream image = ReadImage.GetNext(); // 'ReadImage' is static class which returns the next image
            mv.WriteNextImage(image.toByteArray());
          }
          

          }
          }

          class MovieWriter
          {
          public MovieWriter(DataOutputStream)
          {
          // set class variable
          }

          public void WriterHttpHeader()
          {
          String header = "HTTP/1.1 200 OK \r\n Content-Type: multipart/x-mixed-replace; boundary=--boundary";
          dataOutputStream.write(GetBytes(header)); // dataOutputStream = class variable; GetBytes() return bytes of header
          }

          public void WriteNextImage(byte[] data)
          {
          try
          {
          String response = "Content-Type: image/jpg \r\n" + "Content-Length: " + String.ValueOf(data.length);
          dataOutputStream.write(GetBytes(response)); // first write response header
          dataOutputStream.write(data); // then write image data
          } catch(Exception ex) {
          ex.printStackTrace();
          }
          }

          }

          Again, when the while loop in ClientThread runs, MovieWriter sends one image only. After that, it raises the said exception. What am I doing wrong? Thanks for anything you share :) NOTE: I cut this code short for better understanding. If you need more code, I can provide

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Second attempt at adding this comment ... It seems to me that your logic is the wrong way round. Sending an image (or any HTML response) to a web browser, is usually the result of a request from the browser to the server. The way you have it, you are trying to push data to the browser when it may not be expecting it, so there will be no connection between the two.

          1 Reply Last reply
          0
          • D Django_Untaken

            Richard MacCutchan wrote:

            So your 'client' should be a server .....

            - I have a 'ServerThread' (one and only one server thread) - The piece of code that serves images, upon request, is known as 'ClientThread' - This 'ServerThread' can start N number of 'ClientThread', against N number of requests from web browser. And YES. Good naming conventions help understand better. 'ClientThread' could better be named as 'RequestThread'. But the problem still remains. My browser display ONE picture, sent from my server. After that ONE picture, it starts throwing said exception.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well, as I suggested above, that is because the browser is not expecting it. Your server thread should only be sending an image when the browser sends it a request.

            D 1 Reply Last reply
            0
            • L Lost User

              Well, as I suggested above, that is because the browser is not expecting it. Your server thread should only be sending an image when the browser sends it a request.

              D Offline
              D Offline
              Django_Untaken
              wrote on last edited by
              #6

              Richard MacCutchan wrote:

              Well, as I suggested above, that is because the browser is not expecting it.

              Perhaps I am having tough time understanding you. Please bear with me. To the best of my knowledge, I am sending only the images and nothing else. I also tried removing the header just before I transmit the image, even that does not help. :(

              L 1 Reply Last reply
              0
              • D Django_Untaken

                Richard MacCutchan wrote:

                Well, as I suggested above, that is because the browser is not expecting it.

                Perhaps I am having tough time understanding you. Please bear with me. To the best of my knowledge, I am sending only the images and nothing else. I also tried removing the header just before I transmit the image, even that does not help. :(

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                At the risk of repeatedly repeating myself ... You cannot send unsolicited messages to a web browser, they do not work that way.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups