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 / Remoting / January 2004

Tip: Looking for answers? Try searching our database.

File Transfer over network using Sockets problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abhishek - 19 Jan 2004 08:14 GMT
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[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
news.microsoft.com - 31 Jan 2004 01:42 GMT
Hi,
What about Stream class? Why you dont use it? It's deriving from
MarshallByRef. But you need like this interfaces.

interface IFileTransferContext
{
Stream Source
{
get;
}
}

interface IServerApplication
{
   IFileTransferContext GetFile(ID);
   IFileTransferContext PutFile(ID);
   CloseFileTransferContext(IFileTransferContext);
}

> 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
[quoted text clipped - 50 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 - 13 lines]
>
> Regards, Abhishek

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.