java.net.SocketException: Software caused connection abort: socket write error
-
Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a
while(true)
loop. MyClientThread
sends one image only. After that, it starts producing this exception. Here is what I have tried so farOverview
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 50msclass 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 -
Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a
while(true)
loop. MyClientThread
sends one image only. After that, it starts producing this exception. Here is what I have tried so farOverview
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 50msclass 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 provideIt 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.
-
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.
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', againstN
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. -
Hello there. I am trying to send images (byte[] data) to web browser using java. I am doing this in a
while(true)
loop. MyClientThread
sends one image only. After that, it starts producing this exception. Here is what I have tried so farOverview
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 50msclass 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 provideSecond 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.
-
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', againstN
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. -
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.
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. :(
-
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. :(