c#中的文件传输问题 [英] File Transfer Problem in c#

查看:83
本文介绍了c#中的文件传输问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Pocket pc(.net compact

c#)的套接字将文件传输到桌面(不使用.net只是mfc和套接字2 API)时出现问题。插座

通信不是问题,我可以跨越数据传输。在

服务器上我使用Socket 2 API(recv函数读取字节)而不是使用

..NET。我使用FileStream打开Pocket PC上的文件,然后将一个

BinaryReader对象与流关联,并调用ReadBytes将所有字节

读入一个字节数组。我有一个连接的套接字,我通过网络异步发送这个字节数组

。我在我的计算机上获取该文件但是图像

数据已损坏。我试过调试,发现我在字节数组中的值与b
在文件中的值不同。而是正确传输JPG的

标头信息。由于该文件最初是在PC上的,因此通过将其添加到

项目下载到模拟器中。我假设在对应的位置

的字节值应该与在Pocket PC上加载文件数据

后字节数组包含的字节值相匹配。它不是!例如,当我在字节数组中看到相应的索引时,字节#213在PC上具有十进制

值62

读取65.这是编码问题? Unicode和ASCII?是否有与字节排序有关的
?为什么会这样?是什么方式

提供从手持到PC的文件的简单字节到字节传输

以及从PC到手持网络?请回复,因为在这个

点,我已经尝试了所有选项,但我没有办法解决。我能够
发送来回客户端和服务器的文本消息。


以下是客户端和服务器的代码片段。

服务器


FILE * pFile = NULL; //文件指针

BYTE recvbuf [1024];

pFile = fopen(strFileName," w"); //如果文件确实打开一个文件写入

存在它会将其清空

int bytesRecv = SOCKET_ERROR; / /初始化为错误值。

//这个while循环一直保持读取字节,直到终止符号为

在流中找不到

while(bytesRecv == SOCKET_ERROR ||!boTermFound)

{//继续读取和写入字节,直到你读到消息的结尾

bytesRecv = recv(AcceptSocket, (char *)recvbuf,1024,0);

//必须检查天气最后n个字节是否包含终止符串

if(bytesRecv == 0 || bytesRecv == WSAECONNRESET)

{

AfxMessageBox(连接已关闭);

返回false;

break;

}

boTermFound = CheckForTerminator(strTermString,recvbuf,bytesRecv);

fwrite(recvbuf,sizeof(BYTE),bytesRecv,pFile);

}

fclose(pFile)


客户端(.net compact c#)

-------------------------


FileStream myFileStream;

myFileStream = new

FileStream(m_strFileName,FileMode.Open,FileAccess。 Read,FileShare.Read); //一个

的FileStream实例

byte [] b = new byte [myFileStream.Length]; //创建一个存储的字节数组

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);

//发送终结符

_asyncEvent.Reset();

_socket.BeginSend(Network.TerminatorBytes,0,

网络。 TerminatorBytes.Length,SocketFlags.None,_sendCallback,true);

//这只是表示数据终止的代码。


这变得很严重问题。我的整个项目依赖于

成功转移jpg图像。请看看这个。


问候,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[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

推荐答案

Abhishek< ga ***** @ rpi.edu>写道:
Abhishek <ga*****@rpi.edu> wrote:
我在使用Pocket pc(.net compact
c#)中的套接字传输文件到桌面(不使用.net只是mfc和套接字2 API)时遇到问题。套接字通信不是问题,我可以跨越数据传输。在
服务器上我使用Socket 2 API(recv函数读取字节)而不使用
.NET。我使用FileStream打开Pocket PC上的文件,然后将一个
BinaryReader对象与流关联,并调用ReadBytes将所有字节读取到一个字节数组中。


这不是你发布的代码所做的。您发布的代码依次读取每个字节的
,这在性能方面非常可怕。

我实际上并不使用任何技术 - 我会创建一个内存流,并且
从文件流中读取块,直到你不能再读取为止,在你去的时候把块写到内存流然后调用ToArray之后

内存流。这样你就不会遇到问题,如果

文件大小也会在你阅读的时候发生变化。

我有一个连接套接字我发送这个字节数组
异步通过网络。
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.
That''s not what the code you posted does. The code you posted reads
each byte in turn, which is pretty horrendous in terms of performance.
I''d actually use neither technique - I''d create a memory stream, and
read chunks from the file stream until you can''t read any more, writing
the chunks to the memory stream as you go and then calling ToArray on
the memory stream afterwards. That way you won''t get into problems if
the file size changes while you''re reading, too.
I have a connected socket and I send this byte array
asynchronously over the network.




正确 - 首先要做的是找出错误所在的一面。我b $ b建议你在每一方编写简单的代码,根本不处理

文件,它只是将一个固定的二进制块写入

套接字,或者将字节写入屏幕进行比较(在阅读器上

方面)。


我可以看到一个问题客户端是你已经有效地获得了多个线程发送数据 - 你发送终结器

与jpeg本身有关不能是一个好的

想法。我不认为终结者首先是一个好主意,说实话 - 确保什么是阻止它出现在主文件中?在数据之前发送

长度。


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Right - the first thing to do is find out what side the error''s on. I
suggest you write simple code on each side which doesn''t deal with
files at all, it just either writes a fixed block of binary out to the
socket, or writes the bytes to the screen for comparison (on the reader
side).

One problem I can see on the client side is that you''ve effectively got
more than one thread sending data - you''re sending the terminator
asynchonrously with respect to the jpeg itself, which can''t be a good
idea. I don''t think the terminator is a good idea in the first place,
to be honest - what''s to stop it appearing in the main file? Send a
length before the data instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





我的smtp网关遇到了同样的问题...


我通过检查终止字符并解压缩确切数据来解决问题

然后发送到要写的filestram。


在我的情况下

从smtp服务器回复...读取[1024]字节说实际数据是否只有24

bytes .Net将填入\0\0\ 0 ...剩下的。


不确定..关于你的问题。


Jeyapandian。


" ;阿布舍克巴克" < GA ***** @ rpi.edu>写在消息

新闻:uJ ************** @ TK2MSFTNGP12.phx.gbl ...

我有转移问题使用Pocket pc(.net compact

c#)的套接字到桌面的文件(不使用.net只是mfc和套接字2 API)。插座

通信不是问题,我可以跨越数据传输。在

服务器上我使用Socket 2 API(recv函数读取字节)而不是使用

..NET。我使用FileStream打开Pocket PC上的文件,然后将一个

BinaryReader对象与流关联,并调用ReadBytes将所有字节

读入一个字节数组。我有一个连接的套接字,我通过网络异步发送这个字节数组

。我在我的计算机上获取该文件但是图像

数据已损坏。我试过调试,发现我在字节数组中的值与b
在文件中的值不同。而是正确传输JPG的

标头信息。由于该文件最初是在PC上的,因此通过将其添加到

项目下载到模拟器中。我假设在对应的位置

的字节值应该与在Pocket PC上加载文件数据

后字节数组包含的字节值相匹配。它不是!例如,当我在字节数组中看到相应的索引时,字节#213在PC上具有十进制

值62

读取65.这是编码问题? Unicode和ASCII?是否有与字节排序有关的
?为什么会这样?是什么方式

提供从手持到PC的文件的简单字节到字节传输

以及从PC到手持网络?请回复,因为在这个

点,我已经尝试了所有选项,但我没有办法解决。我能够
发送来回客户端和服务器的文本消息。


以下是客户端和服务器的代码片段。

服务器


FILE * pFile = NULL; //文件指针

BYTE recvbuf [1024];

pFile = fopen(strFileName," w"); //如果文件确实打开一个文件写入

存在它会将其清空

int bytesRecv = SOCKET_ERROR; / /初始化为错误值。

//这个while循环一直保持读取字节,直到终止符号为

在流中找不到

while(bytesRecv == SOCKET_ERROR ||!boTermFound)

{//继续读取和写入字节,直到你读到消息的结尾

bytesRecv = recv(AcceptSocket, (char *)recvbuf,1024,0);

//必须检查天气最后n个字节是否包含终止符串

if(bytesRecv == 0 || bytesRecv == WSAECONNRESET)

{

AfxMessageBox(连接已关闭);

返回false;

break;

}

boTermFound = CheckForTerminator(strTermString,recvbuf,bytesRecv);

fwrite(recvbuf,sizeof(BYTE),bytesRecv,pFile);

}

fclose(pFile)


客户端(.net compact c#)

-------------------------


FileStream myFileStream;

myFileStream = new

FileStream(m_strFileName,FileMode.Open,FileAccess。 Read,FileShare.Read); //一个

的FileStream实例

byte [] b = new byte [myFileStream.Length]; //创建一个存储的字节数组

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);

//发送终结符

_asyncEvent.Reset();

_socket.BeginSend(Network.TerminatorBytes,0,

网络。 TerminatorBytes.Length,SocketFlags.None,_sendCallback,true);

//这只是表示数据终止的代码。


这变得很严重问题。我的整个项目依赖于

成功转移jpg图像。请看看这个。


问候,Abhishek



I had a same kind of problem with my smtp gateway...

I solved th problem by checking termination char and extract the exact data
then send to the filestram to write.

In my case
Reply from smtp server...read [1024] bytes say if the actual data is only 24
bytes .Net will fill with "\0\0\0..." the rest.

Not sure..about your problem.

Jeyapandian.

"Abhishek" <ga*****@rpi.edu> wrote in message
news:uJ**************@TK2MSFTNGP12.phx.gbl...
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


我放在那里的代码略有不同,因为我一直在尝试所有不同类型的东西,这是我最近的变化

正在进行中。我也使用了BinaryReader方法,但没有用。我知道

逐字节读取是可怕的,但现在我想转移我的数据

没有任何损坏,这是我的主要关注点。

很可能使用终结符字符串可能不是一个好主意,但是

终结符字符串不会重复两次。在我的情况下,我只在字节传输结束时获得终止符

字符串。所以终结符字符串不是
在数据中间某处混合。


关于客户端或服务器端存在的问题似乎大多数

可能是客户端。因为我有一个JPG文件,我将桌面上的
转移到掌上电脑。这是我试图用套接字发回

的文件。现在我检查这个的方式是我在两端的字节数组中加载文件

。掌上电脑中的前213个字节和

桌面是相同的,但是在214字节之后事情发生了变化,它在文件的桌面版本上的deciamal

值是62并且它在口袋里的价值

个人电脑版本是65.对应于62的字符是''>'',这就是我在打开文件时显示的

桌面上的二进制文件。现在从这个字节

起,事情不再相同了。如果不是,那么事情就会变得很不好了。所以我的感觉是,掌上电脑使用不同的编码以不同的方式解释字节数据

。但我不知道。我尝试使用一个文件流在Pocket PC上加载相同的文件并使用另一个文件流写入

并在口袋中制作了一个图像文件的副本

pc所以从pokcet pc环境中加载字节数据似乎没问题但

与桌面上的字节数据没有相同的对应关系。我希望我已经回答了你所有的问题。请给我提示做什么

下一个?


Abhishek


" Jon Skeet [C#MVP] " < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
The code I put down there is slightly different there because I have been
trying all different kind of things and this was the latest changes I was
underway. I have also used the BinaryReader method and did not work. I know
reading byte to byte is horrendous but right now I want to transfer my data
without any corruption and that is my major concern.

Its is likely that using a terminator string might not be a good idea but a
terminator string wont repeate twice. In my case I do get the terminator
string at the end of the byte transfer only. So the terminator string is not
getting mixed somewhere in the middle of the data.

About the problem being there on the client end or server end it seems most
likely it is the client end. Because I have a one JPG file which I transfer
from the desktop to the pocket pc. This is the very file I am trying to send
back using sockets. Now the way I am checking this is I am loading the files
in byte array at both ends. The first 213 bytes in the pocket pc and the
desktop are identical but things change after the 214 byte, its deciamal
value on the desktop version of the file is 62 and its value in the pocket
pc version is 65. The character corresponding to 62 is ''>'' and that is what
it shows when i open the file in binary on the desktop. Now from this byte
onwards things are no longer identical. Things get different if not
drastically. So what my feeling is that the pocket pc interprets byte data
in a different manner using a different encoding. But I dont know. I tried
loading the same file on the pocket pc using one file stream and writing
using another filestream and it made a copy of the image file on the pocket
pc so loading of the byte data seems ok from the pokcet pc environment but
is not having the same correspondance with the byte data on the desktop. I
hope I have answered all your questions. Please give me hints what to do
next?

Abhishek

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Abhishek< ga ***** @ rpi.edu>写道:
Abhishek <ga*****@rpi.edu> wrote:
我在使用Pocket pc(.net
compact c#)中的套接字传输文件到桌面(不使用.net只是mfc和套接字2 API)时遇到了问题。套接字通信不是问题,我可以跨越数据传输。在
服务器上我使用Socket 2 API(recv函数读取字节)而不使用
.NET。我使用FileStream打开Pocket PC上的文件,然后
将BinaryReader对象与流关联,并调用ReadBytes将所有
字节读入字节数组。
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.


<这不是你发布的代码所做的。您发布的代码依次读取每个字节,这在性能方面非常可怕。
我实际上不使用任何技术 - 我会创建一个内存流,并且
从文件流中读取块,直到你不能再读取为止,随后将块写入内存流,然后在内存流上调用ToArray。这样你在阅读时如果文件大小发生变化就不会遇到问题。



That''s not what the code you posted does. The code you posted reads
each byte in turn, which is pretty horrendous in terms of performance.
I''d actually use neither technique - I''d create a memory stream, and
read chunks from the file stream until you can''t read any more, writing
the chunks to the memory stream as you go and then calling ToArray on
the memory stream afterwards. That way you won''t get into problems if
the file size changes while you''re reading, too.

我有一个连接套接字我发送这个字节数组
异步通过网络。
I have a connected socket and I send this byte array
asynchronously over the network.



正确 - 首先要做的是找出错误所在的一面。我建议你在每一方编写简单的代码,根本不处理文件,它只是将一个固定的二进制块写入
套接字,或写入字节到屏幕上进行比较(在读者方面)。

我在客户端看到的一个问题是你有效地获得了多个线程发送数据 - 你正在发送关于jpeg本身的终结者
,这不是一个好主意。我不认为终结者首先是一个好主意,说实话 - 什么阻止它出现在主文件中?在数据之前发送
长度。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件



Right - the first thing to do is find out what side the error''s on. I
suggest you write simple code on each side which doesn''t deal with
files at all, it just either writes a fixed block of binary out to the
socket, or writes the bytes to the screen for comparison (on the reader
side).

One problem I can see on the client side is that you''ve effectively got
more than one thread sending data - you''re sending the terminator
asynchonrously with respect to the jpeg itself, which can''t be a good
idea. I don''t think the terminator is a good idea in the first place,
to be honest - what''s to stop it appearing in the main file? Send a
length before the data instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于c#中的文件传输问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆