通过NetworkStream发送的文件收到损坏的C#(File sent over NetworkStream is received corrupted C#)
我的目标是使用NetworkStream通过TCP连接发送文件。 我首先发送我要发送的数据的长度,然后我使用文件流和二进制写入器逐字节发送数据。
在调试过程的过程中,我发现在接收端文件的开始处放置了一些“0”字节。 例如,基本文件的内容
azertyuiop
作为azerty
接收(4个空格替换uiop
),导致图像等文件被破坏。到目前为止我得到的代码:(其中br是BinaryReader,bw是BinaryWriter)
发件人:
using (var readStream = new FileStream(fileLocation, FileMode.Open)) { // Send the data length first bw.Write(new FileInfo(fileLocation).Length); bw.Flush(); var buffer = new byte[1]; while (readStream.Read(buffer, 0, 1) > 0) { bw.Write(buffer[0]); bw.Flush(); } }
接收器:
// Get data length var dataLength = br.ReadInt32(); using (var fs = new FileStream(newFileLocation, FileMode.Create)) { var buffer = new byte[1]; for(int i = 0; i < dataLength; i++) { br.Read(buffer, 0, 1); fs.Write(buffer, 0, 1); } }
我错过了什么或做错了什么?
My goal is to send a file over a TCP connection using NetworkStream. I first send the length of the data I'm going to send, and then I use a filestream and a binary writter to send the data byte by byte.
While debugging the process, I found out that some '0' bytes are being put at the beggining of the file on the receiving end. For example, the base file's content
azertyuiop
is received asazerty
(4 spaces replacinguiop
), causing files like images to be corrupted.The code I've got so far : (Where br is a BinaryReader and bw is a BinaryWriter)
Sender:
using (var readStream = new FileStream(fileLocation, FileMode.Open)) { // Send the data length first bw.Write(new FileInfo(fileLocation).Length); bw.Flush(); var buffer = new byte[1]; while (readStream.Read(buffer, 0, 1) > 0) { bw.Write(buffer[0]); bw.Flush(); } }
Receiver:
// Get data length var dataLength = br.ReadInt32(); using (var fs = new FileStream(newFileLocation, FileMode.Create)) { var buffer = new byte[1]; for(int i = 0; i < dataLength; i++) { br.Read(buffer, 0, 1); fs.Write(buffer, 0, 1); } }
What am I missing or doing wrong ?
原文:https://stackoverflow.com/questions/47541004
最满意答案
CRM 2016在WCF服务上运行,我习惯了CRM 4和2011中的WS.我不确定wsdl.exe是否可以在这种情况下工作,但svcutil.exe的工作原理。
CRM 2016 operates on WCF services and I was used to WS that was in CRM 4 and 2011. I'm not sure if wsdl.exe should work in this case or not but svcutil.exe works.
相关问答
更多-
JAXB2如何直接使用WSDL url而不是.WSDL文件(how can JAXB2 directly use WSDL url instead of .WSDL file)[2022-02-05]
对于maven-jaxb2-plugin请参阅: https://github.com/highsource/maven-jaxb2-plugin/wiki/Specifying-What-To-Compile 对于WSDL,它会是这样的:WSDL https://www.acme.com/my.WSDL< ... -
如何使用WSDL(How to use a WSDL)[2024-01-13]
我将启动Visual Studio 2008,创建一个Web项目(或控制台应用程序 - 无关紧要),然后右键单击项目,并从上下文菜单中选择“添加服务参考”。 在框中输入文件路径和名称并导入WSDL - 这将生成一个简单,非常基本的WCF客户端供您使用。 您应该在生成的代码中找到一个“YourservicenameClient”类,该类应该在WSDL合同上为每个定义的方法设置方法。 实例化客户端并调用要呼叫的方法 - 这就是所有的! YourServiceClient client = new YourSer ... -
请参阅http://msdn.microsoft.com/en-us/library/ms181854%28VS.90%29.aspx “从WSDL文件创建.NET Web服务提供程序端点。” See http://msdn.microsoft.com/en-us/library/ms181854%28VS.90%29.aspx "Create a .NET Web Service provider endpoint from a WSDL file."
-
右键单击并选择“Show Interface Viewer”。 选择“WSDL内容”选项卡。 右键单击右侧的WSDL,然后选择“全选”,然后选择“复制”。 粘贴到记事本中并保存。 (2016/04/13更新纳入下面我自己的评论)或者: 尝试合并具有免费工具描述的WSDL和XSD文件来执行此操作。 它有一个指向源代码和二进制文件的Google Code页面的链接。 在Google Code页面上,如果您不想从源代码构建它,请单击左侧的“ 下载”链接以仅下载Windows二进制文件。 Right-click a ...
-
来自WSDL的CS文件(CS file from WSDL)[2022-02-17]
CRM 2016在WCF服务上运行,我习惯了CRM 4和2011中的WS.我不确定wsdl.exe是否可以在这种情况下工作,但svcutil.exe的工作原理。 CRM 2016 operates on WCF services and I was used to WS that was in CRM 4 and 2011. I'm not sure if wsdl.exe should work in this case or not but svcutil.exe works. -
强烈建议使用SoapUI ,因为它会自动列出wsdl文件中的所有方法,并为您提供执行wsdl文件以实时查看结果的选项。 它也可以免费使用。 安装SoapUI - 您的第一个SoapUI项目后,这应该会给您一个良好的开端 Using SoapUI is highly recommended, as it will list all the methods in the wsdl file automatically, and will give you an option to execute the wsd ...
-
在WSDL中使用XSD文件(Using XSD file in WSDL)[2023-03-20]
XSD是您将发送给您的Web服务的对象的架构。 使用下面的命令行从您的XSD文件创建序列化对象。 例如,假设您的语言是C#,您的Web服务是SendData() xsd.exe -c -l:c#-n:XmlSerializationHowTo yourobject.xsd 这将创建一个名为yourobject.cs的类文件。在您的代码中,引用您的Web服务,并调用SendData(yourobject); 注意,您也可以从xsd创建样本xml,反之亦然。 XSD is the schema of your ... -
带有wsdl文件的Java SOAP(Java SOAP with wsdl file)[2023-05-29]
在我的公司,我们正在处理很多对SAP的SOAP请求。 我们为Maven使用cxf-codegen-plugin 。 它从wsdl文件生成SOAP结构作为java类(请求/响应/数据类型),然后可以将其用作生成请求/响应的方式。 它在pom.xml中的示例设置可能如下所示:org.apache.cxf cxf-codege ... -
ONVIF wsdl文件(ONVIF wsdl file)[2022-01-18]
该链接是wsdl文件。 右键单击并查看源。 That link is the wsdl file. Right click and view the source. -
通常,我们创建一个扩展名为.xjb的绑定文件,以解决WSDL或模式中的任何冲突。 例如,如果两个元素具有相同的名称,并且您想要区分它们,则可以通过将其指定为绑定文件来重命名。 这是一个例子:
相关文章
更多- 基于C#的UDP协议的同步实现
- 用C#捕捉键盘和鼠标
- [转] C#开源项目大全
- 微信开发者-主动请求-准备工作-(1)发送get、post请求,上传文件(C#)
- 微信C#响应代码
- 微信公众平台的C#实现
- android遥控器:控制电脑上的暴风影音播放(C#作为服务端)
- android遥控器:控制电脑上的暴风影音播放(C#作为服务端)
- CLR Via C# 3rd 阅读摘要 -- Chapter 21 – Automatic Memory Management (Garbage Collection)
- C#开发微信公众化平台
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)