Search code examples
c#python.netunity-containertwisted

python twisted server with C# client


i have a python twisted tcp server which i transfer data with this code :

self.transport.write("true")

it transfers the string "true".

and i plan to get it in my C# client with this code :

byte[] rba = new byte[tcpclnt.ReceiveBufferSize];
stm.Read (rba, 0, (int)tcpclnt.ReceiveBufferSize);

i both tried UTF8 and Default(ASCII) encode to convert it to string :

string returndata = Encoding.UTF8.GetString (rba);

and

string returndata = Encoding.Default.GetString (rba);

and tried to do this :

if ( returndata == "true" ) 
    do something

but it doesn't equal returndata to "true" and i have no idea why ?

can anyone help me to understand it ?


Solution

  • byte[] rba = new byte[tcpclnt.ReceiveBufferSize];
    var len=stm.Read (rba, 0, (int)tcpclnt.ReceiveBufferSize);
    string returndata = Encoding.UTF8.GetString (rba,len);
    

    You are decoding the entire buffer rather than just the 4 bytes you got. stm.Read should return 4, which you can then use in the GetString to tell it to stop after 4.

    Alternatively, you can probably do this too:

    var returndata = stm.ReadToEnd();