c# - Send a value by socket and read the value by reader.ReadInt32() changes value -
i trying send value socket .so have 2 parts in project client
, server
.
the client
sends value server using code :
system.io.binarywriter binarywriter = new system.io.binarywriter(networkstream); binarywriter.write(1); binarywriter.write(2); binarywriter.flush();
so in other part need read 2 values mean 1
, 2
;
so in server part have code :
static void listeners() { socket socketforclient = tcplistener.acceptsocket(); if (socketforclient.connected) { networkstream networkstream = new networkstream(socketforclient); while (true) { list<int> variables = new list<int>(); using (var reader = new binaryreader(networkstream)) { (int = 0; < 2; i++) { int t = reader.readint32(); variables.add(t); } } } } }
as can see hold values in variables
list .but doesn't work .i mean in server part can't values 1
, 2
, values :841757955
best regards.
as far can tell code, sending data string in binary format, yield bytes characters 1,2
.
when read data try int32 values.
there 2 options here:
read , write data string.
client code: binarywriter.write("1,2"); server code: string text = binaryreader.readstring(); // "1,2"
or read , write data integers.
client code: binarywriter.write(10); binarywriter.write(20); server code: int value1 = binaryreader.readint32(); //10 int value2 = binaryreader.readint32(); //20
Comments
Post a Comment