I found a quite strange thing when I try to send a picture from client side to server side through socket using Java. Here is my code in client side of Class ImgSender which extends Thread class:
public class ImgSender extends Thread
{
Socket img_s;
BoolFlag imageready,dataready;
ImgBoundingBox image;
ByteArrayOutputStream byteArrayOutputStream;;
public ImgSender(Socket s,BoolFlag dataready,BoolFlag imageready,ImgBoundingBox image)
{
this.img_s=s;
this.imageready=imageready;
this.image=image;
this.dataready=dataready;
}
public void run()
{
boolean running=true;
while(running)
{
// System.out.println("Img sender running");
if(imageready.getFlag())
{ System.out.println("trying to send img");
try
{
OutputStream outputStream = img_s.getOutputStream();
ImageIO.write(image.getImg(), "jpg", outputStream);
System.out.println("Send image"+System.currentTimeMillis());
outputStream.flush();
outputStream.close();
imageready.setFlag(false);
}
catch(Exception e)
{
System.out.println("image client send failed");
imageready.setFlag(false);
}
}
}
}
What is strange is that as I comment out the first statement:
System.out.println("Img sender running");
The image won't be sent to the server and "trying to send img" won't be printed out. If I don't comment it out, or , I do some other stuff like sleep() before the if statement, the picture could be sent to the server and "trying to send img" is printed. Moreover, if I debug the programme by putting a break point at:
if(imageready.getFlag())
Then execute it step by step, it would pass the if statement and send it image successfully.
In terms of the server side, here is the code for the Class ImgManager:
private class ImgManager extends Thread
{
Socket img_s;
boolean working=true;
public ImgManager(Socket s)
{
this.img_s=s;
}
public void run()
{
while(working)
{
try
{
InputStream inputStream = img_s.getInputStream();
System.out.println("Reading: " + System.currentTimeMillis());
BufferedImage image = ImageIO.read(ImageIO.createImageInputStream(inputStream));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("img.jpg"));
}
catch(Exception e )
{
working=false;
System.out.println("stopped working socket");
}
}
}
public void end()
{
try
{
working=false;
img_s.close();
}
catch(Exception e)
{
System.out.println("connection close failed");
}
}
}
}
}
When it receive the image, it would print out the size of the picture so that we know if it can get the image.
I tried these two programmes on different machines(mac and pc windows7) and different software(blueJ,eclipse or even launch it with command line). The problem shows no matter in which environment.
Here is the link to my code of the whole project storing in google drive: Server:https://drive.google.com/file/d/0B6w_5_wGgS14UnZCbXJKUGdvSFE/view?usp=sharing Client:https://drive.google.com/file/d/0B6w_5_wGgS14aUlmcG4yQWVnemM/view?usp=sharing
I think it's better to download the source code and look inside it, it's hard to describe everything in here. Sincerely looking for your help!