Advertisement

best way to send an receive multivarible data by socket

Started by December 13, 2014 08:49 AM
10 comments, last by Nypyren 9 years, 11 months ago

hi.

im working on a game project that uses sockets for sending and receiving data. client games are unity but still server is c sharp asp.

as we all know on sockets you just can send and receive bytes. so what is best for me to send and receive multi varibles like speed direction and...

i think best way is to concat all varibles to an string and convert that string to a byte and after that send and un concat the string other side. but maybe its notthe best way and there is other ways specially in c sharp. here is my psudocode that i think can work well:


int position,rotation;
 string data=concat(data,position,rotation);
 byte[] byteBuffer = Encoding.ASCII.GetBytes(data);
 socket.send(bytebuffer);

i think this way can not be efficient enough. can i find some other way? thank you

Is there a reason you want to use text for transmission?

Advertisement

Remember, every variable in every programming language is just a series of bytes in memory somewhere.

In some languages, accessing the raw data is trivial.

In newer managed languages they try to hide this boiler plate detail and they don't really want programmers mucking around with raw bytes as much.

However, the process of taking complex data and converting that to a bytestream is called serialization.

When programs send data over a socket, they typically send raw binary data.

Through serialization, all pointers and references to other objects have to be replaced by data or by some sort of foreign key that can be resolved by the receiving end.

Sending data over the network is very similar to writing and reading files.

Basically, whatever you have to do in your language of choice to write data to a file is the same exact thing you do to write data to a socket.

I think you are using java, which comes with some sort of interface you implement. http://www.tutorialspoint.com/java/java_serialization.htm


Is there a reason you want to use text for transmission?

no dave weinstein. i just thought its just possible to concat strings. most of my data varibales are int numbers.


I think you are using java, which comes with some sort of interface you implement. http://www.tutorialspoint.com/java/java_serialization.htm

Opps....


but still server is c sharp asp.

This: http://msdn.microsoft.com/en-us/library/ms233843.aspx


Remember, every variable in every programming language is just a series of bytes in memory somewhere.
In some languages, accessing the raw data is trivial.
In newer managed languages they try to hide this boiler plate detail and they don't really want programmers mucking around with raw bytes as much.
However, the process of taking complex data and converting that to a bytestream is called serialization.
When programs send data over a socket, they typically send raw binary data.
Through serialization, all pointers and references to other objects have to be replaced by data or by some sort of foreign key that can be resolved by the receiving end.
Sending data over the network is very similar to writing and reading files.
Basically, whatever you have to do in your language of choice to write data to a file is the same exact thing you do to write data to a socket.

I think you are using java, which comes with some sort of interface you implement. http://www.tutorialspoint.com/java/java_serialization.htm

thank you but im working on c sharp as i said on my topic. i almost know the basics of sockets but let me tell my problem better. in socket programing your server and clients can be comepeltely diffrent. on can be c shap other java o php or unityscript and... a socket just send and receives from a certain ip and socket and for now i just know to send bytes byt sockets. but its something unmanaged. how can i manage it? should i concat and unconcat the bytes after turning it to something like integer or string or something has made for it to assign my byte data to certain variables.

Advertisement

thank you but im working on c sharp as i said on my topic. i almost know the basics of sockets but let me tell my problem better. in socket programing your server and clients can be comepeltely diffrent. on can be c shap other java o php or unityscript and... a socket just send and receives from a certain ip and socket and for now i just know to send bytes byt sockets. but its something unmanaged. how can i manage it? should i concat and unconcat the bytes after turning it to something like integer or string or something has made for it to assign my byte data to certain variables.

C# is called a managed language because it manages memory allocation for you (thats the simple definition- there is more to it).

Converting to, concatenating, and sending strings is very wasteful.

Serialization for most purposes is binary in nature. Most sterilization libraries simply send the raw binary in little endian and then have a endianess flag.

I'm assuming that your Unity code is also C#, since its unlikely that different serialization libs work well together.

When you work with raw data it does not have a string or integer data type. Its simply bytes. This detail is hidden from you in managed languages but they do provide built in libraries for doing the heavy lifting.


Opps....

moeen k, on 13 Dec 2014 - 02:49 AM, said:

but still server is c sharp asp.

This: http://msdn.microsoft.com/en-us/library/ms233843.aspx
Check out the BinaryWriter and BinaryReader classes.
http://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx

Note, though, that the TCP stream will not tell you where one packet ends and another starts. Thus, you need to prefix each packet with a length in itself.
Thus, you typically generate a packet by writing data using a BinaryWriter to a MemoryStream, then get the length, and write length (as a two byte ushort, typically) and the actual data from the MemoryStream. When receiving, you do the reverse: wait until there's at least two bytes read the length, then read that many bytes, stuff it into a MemoryStream, and use a BinaryReader to read the data back out.
enum Bool { True, False, FileNotFound };

An update...

This has been answered by this article someone wrote:

http://www.gamedev.net/page/resources/_/technical/multiplayer-and-network-programming/multilevel-multiplayer-with-unity-r3580


Some Tricks

How to send custom objects through RPC calls.


Unity does not allow sending on RPC calls according to their documentation; only int/string/float/NetworkPlayer/NetworkViewID/Vector/Quaternation. To send a different type of object you must serialize the object and de-serialize. First I used strings for that but it was a problem with strings larger than 4096 chars, and then I found that that Unity accepts also arrays of bytes, so I used this feature.

For example to send an object UserMessage with different properties (Date, UserId, Message, Type,..etc.) through an RPC call, it's impossible because that object is not accepted, so I serialize the object like this:

public static byte[] Byte_ConvertObjectToBinary(object obj)
{
MemoryStream o = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(o, obj);
return o.GetBuffer();
}
and send through a rpc call:

networkView.RPC("Server_NewMessageAdded", networkPlayer, Utils.Byte_ConvertObjectToBinary(userMessage ));
and on the client de-serialize:

public static object Byte_ReadFromBinary(byte[] data)
{
if (data == null || data.Length == 0)
return null;
var ins = new MemoryStream(data);
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(ins);
}

How to attach a custom object to a game object



thank you my frinds. all of you. but i found something much more straight and easier and i thout its better to share that with you.

you just can set some varibales into an string and after that you can split that to read them seperatly. like code bellow:


string s = "first,second,x";
 string[] s2=s.Split(',');
 Console.WriteLine(s2[0]);
 Console.ReadLine();

This topic is closed to new replies.

Advertisement