- Home /
How can I read data sent using the Transport API with python server
Currently I'm just playing around trying to create my own custom server using python. Right now I haven't accomplished much but I'm having issues reading the data I send over.
I'm using just the sample code from https://docs.unity3d.com/Packages/com.unity.transport@0.4/manual/samples/clientbehaviour.cs.html with a slight modification to the NetworkEvent.Type.Connect if statement.
ORIGINAL
if (cmd == NetworkEvent.Type.Connect)
{
Debug.Log("We are now connected to the server");
uint value = 1;
var writer = m_Driver.BeginSend(m_Connection);
writer.WriteUInt(value);
m_Driver.EndSend(writer);
}
ALTERED
if (cmd == NetworkEvent.Type.Connect)
{
Debug.Log("We are now connected to the server");
uint value = 1;
var writer = m_Driver.BeginSend(m_Connection);
//
string message = "hello";
writer.WriteBytes(stringToNativeArray(message));
m_Driver.EndSend(writer);
}
When I send the hello message my python server receives this
b'\x00\x00\xee\xda'
I'm confused as to what this is. I have very limited knowledge of networking and the transport layer but any help would be appreciated. (from an actual answer to additional online resources to help accomplish this task)
The simple UDP Python server
import socket
import sys
import ctypes
import json
from ast import literal_eval
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 9000)
sock.bind(server_address)
while True:
print('\nwaiting to receive message')
data, address = sock.recvfrom(4096)
if data:
print(data)
sent = sock.sendto(bytes(ctypes.c_ulong(500)), address)
print('sent to ' + str(address))
Ignore the useless imports, I've been trying various things.
Answer by osama2o1ooo · Dec 02, 2021 at 03:14 PM
I'm trying to do the exact same thing, I don't know where did you stop, or were you lucky?
but I'm getting in Unity
Received an invalid message header
when I send something from the PyServer to UnityClient
and of course the weird hex bytes that keep changing with each connection .
early research showed me that I have to build the socketing from the zero (use C# sockets):
here are some helpful links: [https://www.youtube.com/watch?v=eI3YRluluR4&ab_channel=Conor] [https://github.com/ConorZAM/Python-Unity-Socket/blob/master/README.md] [https://stackoverflow.com/questions/38654628/minimalist-python-server-for-unity3d-5-x]