C#[Tcp服务器/客户端]如何轻松发送数据包? [英] C# [Tcp Server/Client] How do I send packets easily ?

查看:76
本文介绍了C#[Tcp服务器/客户端]如何轻松发送数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的读者,



在您认为这是关于如何通过互联网实际发送字节数组之前,我已经知道如何做到这一点。我需要知道的是如何实际发送数据包。我可能实际上使用了错误的术语,它实际上可能不会被称为数据包。但无论如何,我会试着解释我的问题;



我即将创建一个聊天应用程序,在这个聊天应用程序中你可以聊天,我已经知道了要做到这一点。但我想进一步采取大约一百万步,我将添加一个使用麦克风的能力,我也将添加用户名,状态等。我在创建时遇到的唯一问题是制作一种允许发送不同类型信息的方法。



这是TCP服务器的工作方式:

等待byte [],接收byte [],执行以下操作,重复。



我通常将byte []中的第一个字节设为某个数字,例如,字节50表示常规聊天消息,字节60表示语音消息。这样,客户端和服务器确切地知道那些byte []的用途。但这真的很烦人,只有一半时间有效。有没有人知道更好的方法呢?

Dear Readers,

Before you think this is about how to actually send a byte array via the Internet, I already know how to do that. What I need to know is how to actually send a packet like thing. I might actually use the wrong term, it might actually not be called a Packet. But anyways, I''ll try to explain my problem;

I am about to create a Chat Application, in this Chat Application you can chat, I already know how to do that. But I want to take about a million steps further, I am going to add an abillity to use the Microphone, also I am going to add Usernames, Statusses and more. The only problem I''ve faced as I was creating this is making a method that allows to send different types of information.

This is how a TCP Server works:
Wait for byte[], Receive byte[], do following action, repeat.

I usually made the first byte in the byte[] to a certain number, for instance, byte 50 would mean a regular chat message, byte 60 would be a voice message. So that way, a client and the server know exactly what those byte[] are for. But this is really annoying and only works half of the time. Does anyone know a better way of doing this ?

推荐答案

使用记录类型。使用标识记录类型及其长度的标题以及您可能需要的任何其他信息,为每条消息中的实际数据添加前缀。然后每一端都会准确知道有效载荷在他们处理的任何消息上是什么。
Use record types. Prefix the actual data in each message with a header that identifies the record type and its length, and perhaps any other information you may want. Then each end will know exactly what the payload is on any message they process.


你对这种特殊方法不满意是件好事。您需要使用更多或更少的高级数据结构,并且您的传输层应该与它们完全无关。因此,您需要学习序列化:

http://en.wikipedia.org /wiki/Serialization#.NET_Framework [ ^ ],

http://msdn.microsoft.com/en -us / library / vstudio / ms233843.aspx [ ^ ]。



出于性能原因,最有可能需要二进制序列化。请参阅:

http://msdn.microsoft.com/en-us /library/72hyey7b.aspx [ ^ ]。



您不必使用套接字或 TcpWriter / TcpReader 。或者,您可以使用WCF的经典.NET远程处理。请参阅我过去答案中对不同网络级别的简短概述:

我如何将byte []发送到其他电脑 [ ^ ],

通信b / w局域网上的两个Windows应用程序。 [ ^ ]。



-SA
It''s good that you are not satisfied with this ad-hoc approach. You need to use more or less advanced data structures, and your transport layer should be totally agnostic to them. For this reason, you need to learn serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[^].

Most likely, for performance reasons, you need binary serialization. Please see:
http://msdn.microsoft.com/en-us/library/72hyey7b.aspx[^].

You don''t have to work with with network and TCP using sockets or TcpWriter/TcpReader. Alternatively, you can use either "classic" .NET remoting of WCF. Please see my short overview of different levels of networking in my past answers:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

—SA


这篇关于C#[Tcp服务器/客户端]如何轻松发送数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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