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.
Related
I'm a newbie in android and python, and I'm trying to establish a TCP socket connection between my android client and a python server. Every time I swipe in my app, I send the swipe movement's direction to the server.
//java client
OutputStream output = client.getOutputStream();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));
byte[] msg;
if(strings[1].equals("swipe"))
{
out.write(strings[1].getBytes());
out.flush();
//strings[0] contains the movement information in format of:
//xmovement_ymovement
//for example: 2.5365_8.5478
msg = strings[0].getBytes();
out.write(msg);
out.flush();
}
#python server
#the 1024 is a temporary number I just put in there
#because I don't know how to find the size of the data in the java client side
request_byte = client_socket.recv(1024)
print("reqeust_byte size: " + str(sys.getsizeof(request_byte)))
request = request_byte.decode()
print("Received " + request)
if request == "swipe":
movement_byte = client_socket.recv(1024)
print("movement_byte size: " + str(sys.getsizeof(movement_byte)))
movement_str = movement_byte.decode()
x_movement, y_movement = movement_str.split("_")
print("x: " + x_movement)
print("y: " + y_movement)
swipe(float(x_movement), float(y_movement))
When a simple button press is done and a single data is sent, the code works fine, but when I swipe and a lot of data is being sent in a short amount of time to the server, the data gets mixed up and turns unpredictable.
Now, as this post Receiving end of socket splits data when printed says, I know that I need to build a protocol that delineates messages, but on the Java client side, I can't get the size of the data I'm trying to send, so I'm stuck here.
This post In Java, what is the best way to determine the size of an object? says I can find size of objects by using java.lang.instrument package, but I can't import java.lang.instrument.Instrumentation in android studio. Is there another way to find the size of an object in android studio?
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.
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
this is my first question on stack overflow, I hope you can help me. I've done a bit of searching online but I keep finding tutorials or answers that talk about reading either text files using a BufferedReader or reading bytes from files on the internet. Ideally, I'd like to have a file on my server called "http://ascistudent.com/scores.data" that stores all of the Score objects made by players of a game I have made.
The game is a simple "block-dropping" game where you try to get 3 of the same blocks touching do increase the score. When time runs out, the scores are loaded from a file, their score is added in the right position of a List of Score objects. After that the scores are saved again to the same file.
At the moment I get an exception, java.io.EOFException on the highlighted line:
URL url = new URL("http://ascistudent.com/scores.data");
InputStream is = url.openStream();
Score s;
ObjectInputStream load;
//if(is.available()==0)return;
load = new ObjectInputStream(is); //----------java.io.EOFException
while ((s = (Score)load.readObject()) != null){
scores.add(s);
}
load.close();
I suspect that this is due to the file being empty. But then when I catch this exception and tell it to write to the file anyway (after changing the Score List) with the following code, nothing appears to be written (the exception continues to happen.)
URL url = new URL("http://ascistudent.com/scores.data");
URLConnection ucon = url.openConnection();
ucon.setDoInput(true);
ucon.setDoOutput(true);
os = ucon.getOutputStream();
ObjectOutputStream save = new ObjectOutputStream(os);
for(Score s:scores){
save.writeObject(s);
}
save.close();
What am I doing wrong? Can anyone point me in the right direction?
Thanks very much,
Luke
Natively you can't write to an URLConnection unless that connection is writable.
What I mean is that you cannot direcly write to an URL unless the otherside accept what you are going to send. This in HTTP is done throug a POST request that attaches data from your client to the request itself.
On the server side you'll have to accept this post request, take the data and add it tothe scores.data. You can't directly write to the file, you need to process the request in the webserver, eg:
http://host/scores.data
provides the data, while
http://host/uploadscores
should be a different URL that accepts a POST request, process it and remotely modifies score.data