带套接字的文件传输 [英] file transfer with sockets

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

问题描述

我已经google搜索无法获得我正在寻找的具体内容。我发现很多完整的应用程序实现了某种类型的文件

转移,但我特别想要的是一个例子来关注

使用tcp套接字在客户端/服务器,服务器/客户端之间传输文件。

服务器和客户端都是我的程序所以我不是在寻找如何

实现一个FTP客户端,或者如何通过

http等从Web服务器下载文件...我的客户端/服务器之间的协议是一个简单的命令

结构,我打算用它来做这样的英文:


这个例子是客户端到服务器场景但我会在两端实现..

所以客户端/服务器可以是服务器/客户端等..

1)客户端从服务器请求文件

2)服务器验证文件是否存在并发送回'' ok''文件大小

(或消息说没有文件存在)

3)客户端收到文件的大小并发送'确定''开始转移

4)服务器发送文件/客户端接收文件

5)客户端收到后发送''ok''


我已经在这里给出了这些步骤,以明确和具体的部分我正在尝试

来学习如何做。除了第4步,我可以在这里做好所有步骤,实际发送/接收文件。对此的帮助将不胜感激。


我甚至不确定这些具体步骤是否会保持这样,我只使用

它们缩小在我的问题领域。此外,我正在使用socket.send和socket.receive(或者他们的

异步方法)直接使用套接字来做这个

,因为我'已经读过最有效的方法,因为

反对使用网络流对象。除了我正在努力学习的具体

任务的帮助,任何关于哪种技术是一个不错的选择的评论

也是受欢迎的(socket.send等..网络流对象..老实说,我只是在完成起草之前在VS帮助中看到一个''socket.sendfile''方法

这篇帖子...我是无论如何,因为我从这个论坛中学到很多东西,并且

即使其中一个更高级别的课程是更好的选择,我想首先学习插座方式的
。 )


解决方案

大卫,


你没有要使用NetworkStream对象,你可以使用Socket

对象就好了。一旦你在服务器端有了这个文件,你就可以只需要从块中读取文件中的字节,然后将它们传递给Send

方法,发送字节在溪流上。当然,你的客户必须要读取它们,并且它必须只读取你要发送的字节数

发送(它已经知道因为你发送了已经很久了。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl ...


>我已经google搜索无法获得我正在寻找的具体内容。我已经找到了很多完整的应用程序来实现某种类型的文件转移,但我特别想要的是一个示例,使用tcp套接字在两者之间传输文件客户端/服务器,服务器/客户端。
服务器和客户端都是我的程序,所以我不是在寻找如何实现FTP客户端,或者如何通过<从Web服务器下载文件br /> http等...我的客户端/服务器之间的协议是一个简单的基于命令的结构,我打算在它上面用英文这样的东西:


这个例子是客户端到服务器的场景,但我会实现两个

结束..所以客户端/服务器可以是服务器/客户端等..

1)客户端请求文件来自服务器

2)服务器验证文件是否存在并发回ok,文件大小为

(或者说没有文件存在的消息)

3)客户收到文件的大小并发送''ok''开始转账

4)服务器发送文件/客户端接收文件

5)客户端收到完成后发送''ok''


我在这里给出了这些步骤要明确和具体的部分,我想要学习怎么做。
我可以在这里做好所有步骤,除了第4步,

实际发送/接收文件。对此有所帮助将非常感谢

赞赏。


我甚至不确定这些具体步骤是否会保持这样,我只使用了

他们缩小我的问题范围。此外,我正在寻找使用socket.send和socket.receive(或者他们的异步方法是

他们的async方法)直接使用套接字来做这个
,因为我'已经读过最有效的
方式的elseware,而不是使用网络流对象。除了我正在努力学习的

特定任务的帮助之外,还欢迎任何有关哪种技术的评论是好的选择(socket.send等等。网络流对象..

我老实说刚刚在VS帮助中看到''socket.sendfile''方法

完成这篇文章的起草...我是无论如何,因为我从这个论坛学到很多

,即使其中一个更高级别的课程是更好的选择,我想先学习插座方式。 )






" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl ...


这个例子是客户端到服务器场景,但我会在两个
结束时实现..所以客户端/服务器可以是服务器/客户等等。

1)客户端从服务器请求文件

2)服务器验证文件是否存在并发回ok,文件大小

(或者说没有文件存在的消息)

3)客户端收到文件的大小并发送''ok''开始转移

4)服务器发送文件/客户端接收文件

5)客户端在收到完成后发送''ok''



听起来像是可行的协议给我:)


我甚至不确定这些具体步骤是否会保持这样,我只使用

他们来缩小我的问题范围。此外,我正在寻找使用socket.send和socket.receive,(或

他们的异步方法)使用套接字直接做这个



首先使用sync.ed方法,IMO async只应在

交互式应用程序中使用。以同样的方式,您可以拥有一个同步服务器和一个

异步客户端。


>,因为我已经读过elseware是最有效的方式,而不是使用网络流对象。除了我正在努力学习的具体任务的帮助,也欢迎任何关于哪种技术是一个很好的选择的评论
(socket.send等... networkstream对象..



只需使用套接字发送/接收,它应该是微不足道的实现。只需要一个简单的循环,你有字节可读(你已经知道文件了)尺寸)。


嗨尼古拉斯,

一旦你在服务器端有文件,你就可以了

以块的形式从文件中读取字节,然后将它们传递给Send

方法,通过流发送字节。当然,你的客户必须

阅读它们,它必须只读取你要发送的字节数

发送(它已经知道,因为你已经发送了长度)。


我不知道发送和接收时实际上会是什么样子

结束了?但是我得到了这个概念。

我很尴尬要说但是我真的没用过.net'的流媒体IO模型

但是......很久以前我用过vb4,我现在的编程活动大多是

脚本用于与网络管理相关的自动化。我只是现在

真的想进入c#/ .net / network编程。我得到了所有.net

IO使用流,并且我会使用流(或fileStream?)从服务器上的磁盘读取

文件,假设读取它进入一个字节缓冲区,然后使用

套接字发送该字节缓冲区。但是,你怎么说,例如,控制和

跟踪''块',你提到了吗?在累积所有文件块或循环导致

多次调用socket.send之后,它会是一次调用

socket.send吗?我假设一个循环更有可能因此它会对小型和大型文件进行处理...不幸的是我仍处于开始状态

学习这个阶段而且缺乏一些关键基本面。但是我可以快速选择

,因为过去的编程经验和一些管理员

脚本。


" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:%2 **************** @ TK2MSFTNGP04.phx.gbl .. 。


David,


你不必使用NetworkStream对象,你可以使用Socket

对象就好了。一旦你在服务器端有了这个文件,你就可以只需要从块中读取文件中的字节,然后将它们传递给Send

方法,发送字节在溪流上。当然,你的客户必须读取它们,并且它必须只读取你要发送的字节数

发送(它已经知道因为你发送了已经很久了。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl ...


>>我谷歌搜索无法获得具体的我正在寻找的东西。我已经找到了很多完整的应用程序来实现某种类型的文件转移,但我特别想要的是一个示例,使用tcp套接字在两者之间传输文件客户端/服务器,服务器/客户端。
服务器和客户端都是我的程序,所以我不是在寻找如何实现FTP客户端,或者如何通过<从Web服务器下载文件br /> http等...我的客户端/服务器之间的协议是一个简单的基于命令的结构,我打算在它上面用英文这样的东西:

这个例子是客户端到服务器方案,但我会在两个端点实现..所以客户端/服务器可以是服务器/客户端等。
1)客户端从服务器请求文件
2)服务器验证文件存在然后用文件大小发回''ok''(或者说没有文件存在的消息)
3)客户端接收文件的大小并发送''ok''开始传输
4)服务器发送文件/客户端接收fil e)5)客户收到邮件后发送''ok''

我已经在这里给出了这些步骤以明确和具体的部分我是
试着学习怎么做。我可以在这里完成所有步骤,除了步骤4,
文件的实际发送/接收。对此的帮助将非常感激。

我甚至不确定这些具体步骤是否会保持这样,我只是用它们来缩小范围。我的问题。此外,我正在寻找使用socket.send和socket.receive(或他们的异步方法)直接使用套接字执行此操作,因为我读过最多的elseware
有效的方式,而不是使用网络流对象。除了我正在努力学习的具体任务的帮助之外,还欢迎任何关于哪种
技术是一个好选择的评论(socket.send等......
网络流对象。老实说,在完成这篇文章的起草之前,我在VS帮助中看到了一个''socket.sendfile''方法......我还是张贴了
因为我从这个论坛中学到了很多东西,即使其中一个更高级别的课程是更好的选择,我想先学习插座方式。)



I have googled to no avail on getting specifically what I''m looking for. I
have found plenty of full blown apps that implement some type of file
transfer but what I''m specifcally looking for is an example to follow for
using a tcp socket to transfer files between client/server, server/client.
Both server and client are my program so I''m not looking for how to
implement an FTP client, or how to download a file from a web server via
http etc... Protocol between my client/server is a simple command based
structure and I intended on it going something like this in english:

this example is client to server scenario but I''ll implement on both ends..
so client/server can be server/client etc..
1) client requests a file from server
2) server verifies file exists and sends back an ''ok'' with the files size
(or message saying no file exists)
3) client receives file''s size and sends the ''ok'' to start the transfer
4) server sends file / client receives file
5) client sends ''ok'' after receive is done

I''ve given these steps here to be clear and specific on the part I''m trying
to learn how to do. I can do all steps here fine except step 4, the actual
send/receive of the file. Help on that would be greatly appreciated.

I''m not even sure if those specific steps will stay like that, I only used
them to narrow down the area of my question. Further, I''m looking to do this
with the sockets directly, using socket.send and socket.receive, (or their
async methods), as I''ve read elseware that is the most effecient way, as
opposed to using a networkstream object. Aside from help on the specific
task I''m trying to learn, any comments on which technique is a good choice
are also welcome (socket.send etc... networkstream object.. and I honestly
just saw a ''socket.sendfile'' method in VS help before finishing drafting
this post... I''m posting anyway since I learn a lot from this forum, and
even if one of the higher level classes is a better choice, I want to learn
the socket way first.)


解决方案

David,

You don''t have to use the NetworkStream object, you can use a Socket
object just fine. Once you have the file on the server side, you can just
read the bytes from the file in chunks, and then pass them to the Send
method, sending the bytes over the stream. Of course, your client has to
read them, and it has to read only the number of bytes that you are going to
send (which it already knows because you sent the length already).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>I have googled to no avail on getting specifically what I''m looking for. I
have found plenty of full blown apps that implement some type of file
transfer but what I''m specifcally looking for is an example to follow for
using a tcp socket to transfer files between client/server, server/client.
Both server and client are my program so I''m not looking for how to
implement an FTP client, or how to download a file from a web server via
http etc... Protocol between my client/server is a simple command based
structure and I intended on it going something like this in english:

this example is client to server scenario but I''ll implement on both
ends.. so client/server can be server/client etc..
1) client requests a file from server
2) server verifies file exists and sends back an ''ok'' with the files size
(or message saying no file exists)
3) client receives file''s size and sends the ''ok'' to start the transfer
4) server sends file / client receives file
5) client sends ''ok'' after receive is done

I''ve given these steps here to be clear and specific on the part I''m
trying to learn how to do. I can do all steps here fine except step 4, the
actual send/receive of the file. Help on that would be greatly
appreciated.

I''m not even sure if those specific steps will stay like that, I only used
them to narrow down the area of my question. Further, I''m looking to do
this with the sockets directly, using socket.send and socket.receive, (or
their async methods), as I''ve read elseware that is the most effecient
way, as opposed to using a networkstream object. Aside from help on the
specific task I''m trying to learn, any comments on which technique is a
good choice are also welcome (socket.send etc... networkstream object..
and I honestly just saw a ''socket.sendfile'' method in VS help before
finishing drafting this post... I''m posting anyway since I learn a lot
from this forum, and even if one of the higher level classes is a better
choice, I want to learn the socket way first.)



Hi,

"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

this example is client to server scenario but I''ll implement on both
ends.. so client/server can be server/client etc..
1) client requests a file from server
2) server verifies file exists and sends back an ''ok'' with the files size
(or message saying no file exists)
3) client receives file''s size and sends the ''ok'' to start the transfer
4) server sends file / client receives file
5) client sends ''ok'' after receive is done

Sounds like a workable protocol to me :)

I''m not even sure if those specific steps will stay like that, I only used
them to narrow down the area of my question. Further, I''m looking to do
this with the sockets directly, using socket.send and socket.receive, (or
their async methods)

Start with the sync.ed method first, IMO async should only be used in
interactive applications. In the same way you can have a sync server and an
async client.

>, as I''ve read elseware that is the most effecient way, as opposed to using
a networkstream object. Aside from help on the specific task I''m trying to
learn, any comments on which technique is a good choice are also welcome
(socket.send etc... networkstream object..

Just use Socket Send/Receive, it should be trivial to implement. just a
simple loop whiel you have bytes to read (you already knowthe file size).


Hi Nicholas,
"Once you have the file on the server side, you can just
read the bytes from the file in chunks, and then pass them to the Send
method, sending the bytes over the stream. Of course, your client has to
read them, and it has to read only the number of bytes that you are going to
send (which it already knows because you sent the length already)."

I don''t know what that would actually look like on sending and receiving
end? However I get the concept.
I''m embarrassed to say but I really haven''t used .net''s streaming IO model
yet... I used vb4 a long time ago, my programming activities now are mostly
scripting for automation related to network administration. I''m just now
really trying to get into c#/.net/network programming. I get that all .net
IO uses streams, and that I would use a stream (or fileStream?) to read the
file from disk on server, assuming reading it into a byte buffer, then using
the socket to send that byte buffer. But how do I, for example, control and
keep track of the ''chunks'', you mentioned? would it be one call to
socket.send after accumulating all the file chunks or a loop resulting in
multiple calls to socket.send? I assume a loop is more likely so it would
work on small and large files... unfortunately I am still at the beginning
learning stages of this and lack some crucial fundamentals. But I can pick
it up real quick because of past programming experience and some admin
scripting.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...

David,

You don''t have to use the NetworkStream object, you can use a Socket
object just fine. Once you have the file on the server side, you can just
read the bytes from the file in chunks, and then pass them to the Send
method, sending the bytes over the stream. Of course, your client has to
read them, and it has to read only the number of bytes that you are going
to send (which it already knows because you sent the length already).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>>I have googled to no avail on getting specifically what I''m looking for. I
have found plenty of full blown apps that implement some type of file
transfer but what I''m specifcally looking for is an example to follow for
using a tcp socket to transfer files between client/server, server/client.
Both server and client are my program so I''m not looking for how to
implement an FTP client, or how to download a file from a web server via
http etc... Protocol between my client/server is a simple command based
structure and I intended on it going something like this in english:

this example is client to server scenario but I''ll implement on both
ends.. so client/server can be server/client etc..
1) client requests a file from server
2) server verifies file exists and sends back an ''ok'' with the files size
(or message saying no file exists)
3) client receives file''s size and sends the ''ok'' to start the transfer
4) server sends file / client receives file
5) client sends ''ok'' after receive is done

I''ve given these steps here to be clear and specific on the part I''m
trying to learn how to do. I can do all steps here fine except step 4,
the actual send/receive of the file. Help on that would be greatly
appreciated.

I''m not even sure if those specific steps will stay like that, I only
used them to narrow down the area of my question. Further, I''m looking to
do this with the sockets directly, using socket.send and socket.receive,
(or their async methods), as I''ve read elseware that is the most
effecient way, as opposed to using a networkstream object. Aside from
help on the specific task I''m trying to learn, any comments on which
technique is a good choice are also welcome (socket.send etc...
networkstream object.. and I honestly just saw a ''socket.sendfile'' method
in VS help before finishing drafting this post... I''m posting anyway
since I learn a lot from this forum, and even if one of the higher level
classes is a better choice, I want to learn the socket way first.)




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

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