tags:

views:

114

answers:

3

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?

A: 

I would recommend clarifying your question a bit . . . I'm assuming you're trying to use the following function in C or C++?

ssize_t send( int socket, const void *buffer, size_t length, int flags );

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

bn
I get this error:Unable to find assembly 'UdpClientSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Y_Y
+2  A: 

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 a ClientAnswerStruct 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 named ClientAnswerStruct, 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).

Iamamac
You guessed right!
Y_Y
What if after I serialize the structure, save it into a File, and send the file to the client? would the client be able to read and de-serialize?
Y_Y
@Y_Y Just serialize it and send the byte array. The key here is to make sure the client is able to find the definition of the structure. What you need to do is to put the definition in a standalone dll project, and make the server and client projects refer to it.
Iamamac
A: 

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

Oliver