通过套接字以字符串形式发送文件 [英] sending a file through socket as a string

查看:120
本文介绍了通过套接字以字符串形式发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,您能解释一下如何通过套接字将字符串文件从服务器发送到客户端,以及如何将字符串转换回客户端的原始文件以供进一步使用.

Hello guys, can you explain me how to send a file as a string through socket from server to client side and convert the string back into the original file on the client side for further use.

推荐答案

1.为什么将其作为字符串发送?有什么理由不能仅仅将其作为二进制文件发送?可能需要在前缀之前加上几个字节来指定文件大小,以便收件人知道二进制文件何时结束.

2.如果需要发送字符串,则可以使用Base64编码将二进制数据转换为文本,或者在另一端将其解码.
1. Why send it as a string? Is there any reason why you can''t just send it as binary? Maybe need to prefix with a few bytes to give file size so that recipient know when binary file ends.

2. If you need to send a string, maybe convert binary data to text using Base64 encoding / decode it back at the other end.


使用System.Net.Sockets.TcpListenerSystem.Net.Sockets.TcpClient;这是为此目的使用套接字的最方便的方法.为什么要使用原始套接字?使用经典远程处理或WCF(自托管)的更高层次的方法也将起作用.服务合同方法的参数将是文件内容(如果需要,则为字符串,但出于性能考虑,更喜欢使用UTF-8将其编码为字节数组).

—SA
Use System.Net.Sockets.TcpListener and System.Net.Sockets.TcpClient; this is most convenient way of using sockets for such purpose. Why raw sockets? A higher-level way with using classical remoting or WCF (self-hosted) would also work. A parameter of service contract method will be a file content (as string, if you want, but prefer encoding it the an array of bytes using UTF-8, for performance sake).

—SA


这篇关于通过套接字以字符串形式发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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