C#网络 [英] c# networking

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

问题描述

大家好,

我正在开发一个程序,该程序需要将一些信息传输到远程服务器进行处理.我已经将两个对象打包到一个字节流中,但是似乎很难弄清楚如何发送它们两个.

有很多很棒的例子可以使用套接字和TcpClients发送数据,但是我还没有看到任何演示如何发送不同类型的数据并在另一端解密的信息.

例如,我有需要发送的对象A和对象B.我可以成功发送对象A或对象B,但不能同时发送.我尝试过一个接一个地发送它们,但似乎在第一个发送后关闭套接字时遇到了问题,我也尝试将它们作为一个大字节数组发送,但卡在了另一端试图弄清楚如何找到对象A和对象B之间的分离.

有没有人对持续通信"有任何好的最佳实践?(我想这个名字……我不确定是什么意思.将网络流与套接字一起使用会更好吗?或tcpclient?任何帮助或示例都将不胜感激.

Hi Everyone,

I''m working on a program that needs to transmit some information to a remote server for processing. I have packaged the two objects into a byte stream, but seem to be having trouble figuring out how to send both of them.

There are a lot of great examples out there for sending data using sockets and TcpClients, howerver I haven''t seen any that demonstrate sending different types of data, and decyphering them on the other end.

For example, I have Object A, and Object B that I need to send. I can successfully send Object A, or Object B, but not both. I''ve tried sending them one after the other but seem to run into issues with the socket closing on me after the first one, and I''ve also tried sending both as a big byte array, but got stuck on the other end trying to figure out how to find the seperation between object A and Object B.

Does anyone have any good best practices for ''continued communications?'' (I''m guessing at the name...I''m not entirely sure what this would be considered. Is it better to use a networkstream with a socket, or tcpclient? Any help or examples would be greatly appreciated.

Thanks!

推荐答案

使用WCF进行流式传输可能适合您:

[ http://msdn.microsoft.com/en-us/library/ms731913. aspx ]
Streaming with WCF might be for you:

[http://msdn.microsoft.com/en-us/library/ms731913.aspx]


吉姆,

谢谢您的帮助.阅读完这些内容之后,使用缓冲而不是流式传输现在更有意义,因为在反序列化之前我需要那里的所有信息,这也帮助我偶然发现了另一个示例文章
Hi Jim,

Thank you for helping me with this. After reading through that, It also makes more sense now to use buffering instead of streaming as I need all the information there before I can deserialize, and it also helped me stumble across another example article found here.


这篇关于C#网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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