Question by
RealSoftGames · Feb 28, 2016 at 11:59 PM ·
c#networkingsockets
c# .net sockets sending method over network
Hi im trying to sync 2 player objects over the network, im trying to write my own UDP client/server.
i am able to get positional data of a player synced to the server as a string, but i want to send it as a command/Method much like how unity uses the Unet [Command] functionality, but i havent found any resources on doing something like this with sockets yet, their are only examples of sending basic data types.
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("1902.168.0.1"), 8080);
byte[] buffer = new byte[1024];
float timer, nextTime = 0.05f;
Vector3 prevPos;
void Start ()
{
print("Connecting to server...");
try
{
sock.Connect(ep);
print("Connected to server!");
}
catch
{
print("Could not connect to server!");
}
}
// Update is called once per frame
void Update ()
{
prevPos = transform.position;
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * 5f, 0, Input.GetAxis("Vertical") * Time.deltaTime * 5f);
if (transform.position == prevPos || Time.time < timer)
return;
print("Type Message to send to server: ");
buffer = ASCIIEncoding.ASCII.GetBytes(transform.position.ToString());
print("Bufer length: " + buffer.Length);
sock.Send(buffer);
timer = Time.time + nextTime;
//Console.WriteLine("Sent Data to server!\n");
}
}
any info on how i could integrate unity specific code outside of the game engine would be great. it may even solve some of the issues.
i had tried looking through the references in the server program but their doesnt seem to be anything that can link back to mono or unityEngine.
Comment