A
Abhishek
I have a problem transfering files using sockets from pocket pc(.net compact
c#) to desktop(not using .net just mfc and sockets 2 API). 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. I am able to
send textmessages 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 = (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
c#) to desktop(not using .net just mfc and sockets 2 API). 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. I am able to
send textmessages 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 = (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