I am having a problem sending a .PNG image over a network. I have multiple clients connected to a single server. A client chooses the user to whom she would like to send the image to. Here is the code snippet on client side.
BufferedImage img = ImageIO.read(new File(source));
ImageIO.write(img, "png", spGUI.sdtGUI.output);
where spGUI.sdtGUI.output is an output stream wrapped around the output stream of the socket.
I am getting an exception that it cannot write the PNG file.
PS:I don't know how to include the stack trace in the post.
Related
I am trying to get a Java ICAP server to interface with a blue coat device which is acting as the ICAP client. The ICAP server I am working with is here: icap. Basically I have been getting things working and now I am stuck on why on the server side I am not receiving the file. Below is a few lines of code that shows kind of where I am at most recently. Obviously most of the code has been omitted.
IcapRequest request = (IcapRequest)e.getMessage();
ChannelBuffer buffer = null;
buffer = request.getHttpRequest().getContent();
if(buffer != null) {
System.out.println("Buffer = " + buffer.toString(Charset.defaultCharset()));
}
try {
FileOutputStream fout= new FileOutputStream(testfile);
while (request.getHttpResponse().getContent().readable()) {
byte[] bb = new byte[request.getHttpResponse().getContent().readableBytes()];
request.getHttpResponse().getContent().readBytes(bb);
fout.write(bb);
}
Basically, I see using wireshark and on my server print statements I am getting the file name, the html request, etc. But I am not getting all the content when it is a large file. If it is a small .txt file I can get the content and save the txt file and all content to my server side disk. If it is any kind of .docx file that is maybe about 10K in size or larger there appears to be only one ICAP client packet with content using a PSH method but no other content so if I try to save the file to my server on disk I am not getting all the content so the file is basically corrupt. So at this point I am not sure why I cannot get my ICAP server to save the .docx file sent from the blue coat device as I am more leaning toward the problem is on the server side. Any advice would be greatly appreciated.
I have an application that show CCTV feed from mobile. I successfully develop iOS application and now my client wants me to port to Android. The porting was fine until I'm stuck at this part of code.
This part of code, I have to connect to TCP socket server. When connected, I don't have to send server any thing, instead, server will send me a JPEG image. So, after connected, I'll have to keep reading until I received JPEG end marker (0xFF, 0xD9) and then close the connection.
What I plan to do is
Socket s = new Socket("Server IP Addreess", SERVER_PORT);
DataInputStream dis = new DataInputStream(socket.getInputStream());
ArrayList<Byte> bytes = new ArrayList<Byte>();
boolean err = false;
while (true) {
byte b = dis.readByte();
if (b == -1) {
err = true;
break;
}
bytes.add(b);
if ((bytes.get(bytes.size() - 1) == (byte) 0xFF) &&
(bytes.get(bytes.size() - 2) == (byte) 0xD9)) {
break;
}
}
socket.close();
if (!err) {
// create bitmap from byte array and show in ImageView
}
But I'm not sure that this is correct or not. The other solution I'm thinking about is
Socket s = new Socket("Server IP Addreess", SERVER_PORT);
BitmapFactory.decodeStream(s.getInputSteam());
But, also, I don't know how to close socket when server send 0xFF, 0xD9. Or will the BitmapFactory will detect that marker and close socket connection for me?
Any suggestion are welcome. Thank you!
P.S. I don't have test environment as they took it back when I delivered iOS app. So, I have to develop and then deliver to them to test (by playing with the app). Then if it's not working, they will tell me to correct it but I won't be able to access LogCat or other useful information. This is just like trying to sew a button in dark room, and I can't turn on the light.
: |
EDIT
More information about this server
Server won't send length of file, so, number of bytes for each file is unknown.
I have to detect 0xFF, 0xD9 that indicate end of file.
Problem is, I have to terminate socket connection from my side, server won't do it.
I can't change the way server works, it's hardware that my client purchase.
Also, I'm getting one image for each TCP connection, I'm not getting multiple images for single TCP connection.
This is a bad idea to just look for some magic bytes to determine the end of the data. While these magic bytes should be at the end of the file they can also happen inside the data.
Better would be to either prefix the data with the length or use a separate TCP connection for each jpeg file and just read until the end of connection (that is until you don't get any more data). If this is not possible you have to do more advanced parsing, see Detect Eof for JPG images.
I have simple java-server via sockets.
Server is read from client url of file which need to download.
FileOutputStream outStream= new FileOutputStream(SERVER_PATH + file.getName());
BufferedOutputStream out = new BufferedOutputStream(outStream);
byte buf[] = new byte[BATCH];
int read = 0;
while ((read = in.read(buf,0,BATCH))>=0){
out.write(buf,0,read);
}
how to continue to download file?
Your Question is a little ambiguous .!
After looking at the code, it looks like you are reading from a File in Client machine and Writing the same to the Server URL.
Assuming this situation,
The points that can help you resolve this are,
1. There will an IOException if the connection is lost. That means you have to handle the exception and reconnect to the Socket. May be after waiting for some time (!!)
2. Then you need to open the server File in Append mode and continue with out.write. As the out is not reset or lost with the Disconnection.
Thanks, Sunil
I used the Android's ToyVpnClient to set up a tunnel and intercept incoming & outgoing packets. I want to write these packets from this tunnel, which is stored in a ByteBuffer, to a pcap file. I've looked at jpcap and jnetpcap, but they seem to only support creating the pcap file from the packets that were captured by using these libraries to listen to the device's network interfaces in the first place.
Is there any api available that would help me create the pcap file from the tunnel's packet data stored in the ByteBuffer? If I were to create my own pcap writer, how would i go about parsing this packet from the tunnel and put it into the pcap format? (I've looked but haven't found any examples)
Here's the relevant code sample from the ToyVpnClient:
...
DatagramChannel tunnel = DatagramChannel.open();
// Connect to the server.
tunnel.connect(server);
// Packets to be sent are queued in this input stream.
FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
// Packets received need to be written to this output stream.
FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());
// Allocate the buffer for a single packet.
ByteBuffer packet = ByteBuffer.allocate(32767);
// keep forwarding packets till something goes wrong.
while (true) {
// Read the outgoing packet from the input stream.
int length = in.read(packet.array());
if (length > 0) {
// Write the outgoing packet to the tunnel.
packet.limit(length);
tunnel.write(packet);
//How to write to pcap file here?
packet.clear();
}
...
}
Thank you
For anyone looking for similar solution, I got it working using the netutils library's PCapFileWriter and modifying it to include a fake ethernet header for each packet (http://code.google.com/p/netutils/)
I am trying to make a server-client model in which the server can see what the client is doing on their systems. I want to capture and send an image via a socket (skt). How can I display the image received by the server.
Client Thread:
screenShot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "PNG", skt.getOutputStream());
Server Thread:
BufferedImage image = ImageIO.read(connarray.get(0).getInputStream());
Graphics g = image.getGraphics();
g.drawImage(image, 500, 500, null);
You don't ask an actual question here, but I could make a couple of observations.
Both your client and server code is catching and squashing exceptions. If any exceptions were thrown by the client or server-side, your code is throwing away all of the evidence. Change
} catch(Exception ew) { }
to
} catch(Exception ew) { ew.printStackTrace(); }
If there are exceptions being thrown, this will tell you what they are. (In production code, you should probably log exceptions instead of calling printStackTrace(...) ... but that is a lesson for later.)
Taking a screenshot every 1/10th of a second is going to generate a lot of load on the client and server side.
There are existing (non-Java) tools for doing this kind of thing.