Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / February 2004

Tip: Looking for answers? Try searching our database.

byte to byte transfer of jpg file corrupting image data bytes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abhishek - 18 Jan 2004 15:09 GMT
Hello,

My objective is to transfer over the network, a JPG file on the pocket
pc(.net compact framework) to a desktop server using sockets. The socket
communication is not a issue and I am able to transfer data across.On the
serve I am using Socket 2 API (recv function to read bytes)and not using
.NET.  I use FileStream to open the file on the pocket pc, then associate a
BinaryReader object with the stream and call ReadBytes to read all the bytes
into a byte array. I have a connected socket and I send this byte array
asynchronously over the network. I get the file on my computer but image
data is corrupted. I tried debugging, and found that the values that I have
in the byte array are not the same as they are in the file. Rather the
header information of the JPG gets transferred correctly. Since the file was
originally on the PC and was downloaded to the emulator by adding it to the
project. I assume that the byte values at their corresponding locations
should match with what the byte array contains after loading the file data
on the pocket pc. It does not! For instance the byte #213 is having decimal
value 62 on the PC when I see the corresponding index in the byte array it
reads 65. Is this a problem of encoding? Unicode and ASCII? Does it have
anything to do with byte ordering? why is this happening? what is the way
out to provide a simple byte to byte transfer of files from hand-held to PC
and from PC to hand-held over the network? Please reply because at this
point I have tried all options and I don't have a way out. Thanks in
anticipation.

Best wishes,

Abhishek Gattani
Feroze [MSFT] - 19 Jan 2004 03:14 GMT
Can you try the following ?

1) Are you sure you are reading in the JPG file correctly on the pocketpc ?
for eg, can you write out your byte buffer to another jpg file on the Pocket
PC and make sure you get the same image back ?

2) Can you send across some other format file (eg : text file) to the
desktop without errors ?

Please attach a code snippet of both client and server, if the above doesnt
help.

feroze.

Signature

Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> Hello,
>
[quoted text clipped - 24 lines]
>
> Abhishek Gattani
Abhishek - 19 Jan 2004 08:09 GMT
I tried what you suggested. I opened two streams and a copy of the jpg file
was created. this is what i tried.

myFileStream = new
FileStream(m_strFileName,FileMode.Open,FileAccess.Read,FileShare.Read);//an
instance of FileStream
myOutStream = new
FileStream(m_strFileName.Replace("DSC","ABH"),FileMode.OpenOrCreate,FileAcce
ss.Write,FileShare.None);
myOutStream.WriteByte(); myOutStream.Close(); myFileStream.Close();

2) Yes I can send other kind of data successfully. I am able to send text
messages to and fro client and server.
Following is the code snippet of client and server.

Server

FILE *pFile = NULL;//file pointer
BYTE recvbuf[1024];
pFile = fopen(strFileName,"w");//opens a file for writing if the file does
exist it will empty it
int bytesRecv = SOCKET_ERROR;//initializes to an error value.
//this a while loop that keep reading bytes till the terminating symbol is
not found in the stream
while( bytesRecv == SOCKET_ERROR || !boTermFound)
{//keep reading and writing bytes till you read the end of the message
 bytesRecv = recv(AcceptSocket, (char*)recvbuf, 1024, 0 );
 //have to check weather the last n bytes contain the terminator string
 if (bytesRecv == 0 || bytesRecv == WSAECONNRESET )
 {
  AfxMessageBox( "Connection Closed");
  return false;
  break;
 }
 boTermFound = CheckForTerminator(strTermString,recvbuf,bytesRecv);
 fwrite(recvbuf, sizeof(BYTE),bytesRecv, pFile);
}
fclose(pFile)

Client (.net compact c#)
-------------------------

FileStream myFileStream;
myFileStream = new
FileStream(m_strFileName,FileMode.Open,FileAccess.Read,FileShare.Read);//an
instance of FileStream
byte[] b = new byte[myFileStream.Length];//create a byte array to store
for(long i=0;i<myFileStream.Length;i++)
b[i] = (byte)myFileStream.ReadByte();
myFileStream.Close();
_socket.BeginSend(b, 0, b.Length,
SocketFlags.None, null, null);
// send the terminator
_asyncEvent.Reset();
_socket.BeginSend(Network.TerminatorBytes, 0,
Network.TerminatorBytes.Length, SocketFlags.None, _sendCallback, true);
//this is just code that indicates termination of data.

This is becoming a serious problem. My whole project depends on
succcessfully able to transfer jpg images. Please look into this.

Regards, Abhishek

> Can you try the following ?
>
[quoted text clipped - 45 lines]
> >
> > Abhishek Gattani
Feroze [MSFT] - 22 Jan 2004 02:28 GMT
I see some problems with your code:

1) In the client

a) You are calling BeginSend() with null for Callback/State, but I dont see
you calling EndSend() anywhere.
b) You are reading the file in byte by byte. It is faster to read in a
buffer at the same time. Or you can read the file in fixed chunks, and use
MemoryStream() to buffer it. Then you can read fixed size chunks from the
memorystream and write it to the socket.

2) The server:

a) You are making wrong assumptions about the recv() call. See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock
/recv_2.asp
.
This function returns SOCKET_ERROR if it fails, and you have to call
WSAGetLastError() to find out what went wrong. So, your server should be:

  bytesRecv = recv(AcceptSocket, (char*)recvbuf, 1024, 0 );
  //have to check weather the last n bytes contain the terminator string
  if (bytesRecv == SOCKET_ERROR )
  {
   DWORD error = WSAGetLastError();
   if(error == WSAECONNRESET) {
   AfxMessageBox( "Connection Closed");
   return false;
   break;
   }
  }

b) You are using "fopen" to create your file. The default fopen() mode is
"TEXT" mode, where it performs some translations. You should instead use the
"b" mode. Or better still, use CreateFile/WriteFile.

Signature

Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> I tried what you suggested. I opened two streams and a copy of the jpg file
> was created. this is what i tried.
>
> myFileStream = new

FileStream(m_strFileName,FileMode.Open,FileAccess.Read,FileShare.Read);//an
> instance of FileStream
> myOutStream = new

FileStream(m_strFileName.Replace("DSC","ABH"),FileMode.OpenOrCreate,FileAcce
> ss.Write,FileShare.None);
> myOutStream.WriteByte(); myOutStream.Close(); myFileStream.Close();
[quoted text clipped - 32 lines]
> FileStream myFileStream;
> myFileStream = new

FileStream(m_strFileName,FileMode.Open,FileAccess.Read,FileShare.Read);//an
> instance of FileStream
> byte[] b = new byte[myFileStream.Length];//create a byte array to store
[quoted text clipped - 71 lines]
> > >
> > > Abhishek Gattani
avdijk - 06 Feb 2004 06:38 GMT
Don't you think it is possible that there occurs a format conversio
between the PC and Pocket PC.

I have a similar problem where I store a JPG in SQL, and when I try t
access the Image, I get an Exception.
I write the Stream to file on the Pocket PC, and when I browse the fil
on the Pocket PC it doesn't open, but when I open it with my PC it i
100%.

Any comments

avdij
Feroze [MSFT] - 23 Feb 2004 04:24 GMT
It is definitely possile. Especially if the PocketPc uses BigEndian, whereas
the PC uses LittleEndian. You should be mindful of this difference. Note
that I am not saying that the PocketPc uses BigEndian, you should confirm
that it does.

Signature

Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> Don't you think it is possible that there occurs a format conversion
> between the PC and Pocket PC.
[quoted text clipped - 12 lines]
> ------------------------------------------------------------------------
> View this thread: http://www.mcse.ms/message296161.html

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.