• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get jpeg file from socket stream

 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a server, wrote in delphi, which will stream down a jpeg image to whoever connected to it's socket.
so, i make a connection to this server and then i'm using FileOutStream, in order to read the jpeg file that stream down from the server and save it to a file name xxxxx.jpg.
the problem is, i can't view the image as it suppose, using paint, IE, etc...
do anyone have any idea, what was happen?....
my code:
Socket sock = new Socket("battohsai", 20028);
BufferedInputStream bis = new BufferedInputStream(sock.getInputStream());
FileOutputStream fis = new FileOutputStream("abc.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fis);
int c, i = 0;
c = bis.read();
while(c!=-1)
{
bos.write(c); c = bis.read();
}
bos.flush(); bos.close(); fis.close(); bis.close(); sock.close();
thanks.....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the size of the output file compare with the size of the file on the server? Assuming a Windows environment, try comparing the files with "fc /b serverfile receivedfile" -- what are the differences on a byte level and when do they start to occur? How do you know it's not the server which is messing things up?
- Peter
PS. When you close() a stream wrapper, it also close()s the wrapped stream. So bos.close() and bis.close() should be enough.
 
Anson Ding
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ariffin Ahmad:
Your question seen to be hard, anyway you should try out this! It should work....
--------------------------------------------------
Socket sock = new Socket(hostName, portNo);
DataInputStream bis = new DataInputStream sock.getInputStream());
FileOutputStream fis = new FileOutputStream("rr.jpg");
DataOutputStream bos = new DataOutputStream(fis);
long remoteTime = (
((long)(bis.readUnsignedByte() & 0xff)<<24)|
((long)(bis.readUnsignedByte() & 0xff)<<16)|
((long)(bis.readUnsignedByte() & 0xff)<<8)|
((long)(bis.readUnsignedByte() & 0xff)<<0));
int c, i=0;
c = bis.read();
while( c != -1){
bos.write(c);
c = bis.read();
}//end while
--------------------------------------------------
Hope this help!
:roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic