如何通过 UDP 套接字连接发送类?
我有一个 UDP 服务器,我一直在尝试使用 send() 方法发送结构。到目前为止还没有运气...
这就是我正在使用的:
H,G 是结构...
发送方:
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, H);
Byte[] buffer = stream.ToArray();
stream.Close();
和接收方:
IFormatter formatter = new BinaryFormatter();
Stream s = new MemoryStream(buffer.Data);
ClientAnswerStruct G = (ClientAnswerStruct)formatter.Deserialize(s);
s.Close();
MessageBox.Show(G.name);
但我收到此错误:
Unable to find assembly 'UdpClientSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
“UdpClientSample”恰好是将数据发送到服务器的客户端程序的标题...所以我想知道是否需要比序列化更多的功能才能通过 UDP 发送结构联系?
是否有突破可以解释 Iamamac 所说的内容?
I have a UDP server that I have being trying to send structures using send() method.. No luck so far...
This is what I am using:
H,G are structures...
sender side:
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, H);
Byte[] buffer = stream.ToArray();
stream.Close();
and on the receiver side:
IFormatter formatter = new BinaryFormatter();
Stream s = new MemoryStream(buffer.Data);
ClientAnswerStruct G = (ClientAnswerStruct)formatter.Deserialize(s);
s.Close();
MessageBox.Show(G.name);
But I get this error:
Unable to find assembly 'UdpClientSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
'UdpClientSample' happens to be the title of the client program that is sending the data to the server... So I'm wondering if it takes more than serialization to be able so send a structure through UDP connection?
Is there a breakthrough out there that explains what Iamamac says?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有看到完整的代码,但我猜服务器和客户端是两个不同的可执行文件,并且
ClientAnswerStruct
类在双方都定义了两次。当接收方反序列化数据时,它尝试重建一个ClientAnswerStruct
对象,但找不到它的定义(注意它是在发送方定义的。虽然在接收方有一个名为ClientAnswerStruct
,但它们并不相同)。正确的方法是在独立类库中定义
ClientAnswerStruct
类,并让服务器和客户端代码包含它(C# 术语中的“添加引用”)。I didn't see the whole code, but I guess the server and client is two different executable files, and the
ClientAnswerStruct
class is defined twice in both sides. When the receiver deserialize the data, it tries to reconstruct aClientAnswerStruct
object but can not find its definition (Notice that it is defined on the sender's side. Although on the receiver's side there is a class namedClientAnswerStruct
, but they are not the same).The right way to do this is define the
ClientAnswerStruct
class in a standalone class library, and let the server and client code include it ('add reference' in C# terminology).我建议稍微澄清一下你的问题。 。 。我假设您正在尝试在 C 或 C++ 中使用以下函数?
让我们知道您遇到的具体问题,例如:错误消息、不起作用的示例代码等。
如果您谈论的是 C++,那么结构就是一个类,除了结构中的所有成员默认情况下都是公共的。从技术上讲(但重要的是),您发送的不是类或结构,而是对象或原始数据类型的实例。
-bn
I would recommend clarifying your question a bit . . . I'm assuming you're trying to use the following function in C or C++?
Let us know what specific issue you are having, such as: error messages, sample code that is not working, etc.
If you are talking about C++, than a struct is a class except in a struct all members are public by default. Technically (but importantly) you are not sending a class or a struct, but an instance of an object or primitive data type.
-bn
正如 lamamac 已经告诉的,这似乎不是您的代码或您使用的函数的问题。只是您添加了对名为 UdpClientSample 的应用程序的引用,但当您启动应用程序时,它找不到所需的应用程序。
解决此问题的第一个最简单的方法是选择项目中的引用,右键单击它,选择属性并将属性Copy Local设置为true。
如果应用程序现在开始运行,您应该考虑一下加载顺序 或使用 AssemblyResolve 或使用 ILMerge
As lamamac already told, this seems not to be a problem of your code or functions you use. It's just that you added a reference to your application called UdpClientSample but when you start your application it can't find the needed application.
The first simplest method to solve this problem is to select the reference in your project, right click it, select properties and set the property Copy Local to true.
If the application now starts to run you should make a thought about the load order or using AssemblyResolve or using ILMerge