简体   繁体   中英

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 ?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM