• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get jpeg file from socket stream

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic