O
Ofer Achler
I have an annoying problem,
I'm connecting to a server at port 6543 (doesn't really matter) and
trying to exchange information.
The protocol works this way:
i send 8 bytes that contain the number of bytes following (i. e. to send
"hello" you send it this way:
5 hello
)
And the recieve response is in the same format, first 8 bytes is the #
of bytes in the message.
The problem i'm having is that when i send then recieve, the netstream
object skipps over the header and reads something in the middle of the
message.
The really strange part is if i put a break point at where its supposed
to read the bytes, it reads everything correctly. If i put a breakpoint
outside the method that reads, it fails. Makes it quite hard to debug.
Any suggestions?
Here's some sample code:
public string Recv ( )
{
if (Connected)
{
Byte[] RecvBufSize = new Byte[8];
string retData,buffer;
Encoding ASCII = Encoding.ASCII;
// Receive number of bytes for this message
Int32 bytes = socket.Read(RecvBufSize, 0, 8);
buffer = ASCII.GetString(RecvBufSize, 0, bytes);
// Theres a bug somewhere that makes us read too late
Byte[] RecvBytes = new Byte[Convert.ToInt32(buffer)];
// Recieve the full message
bytes = socket.Read(RecvBytes, 0, RecvBytes.Length);
retData = ASCII.GetString(RecvBytes, 0, bytes);
return retData;
}
return null;
}
I'm connecting to a server at port 6543 (doesn't really matter) and
trying to exchange information.
The protocol works this way:
i send 8 bytes that contain the number of bytes following (i. e. to send
"hello" you send it this way:
5 hello
)
And the recieve response is in the same format, first 8 bytes is the #
of bytes in the message.
The problem i'm having is that when i send then recieve, the netstream
object skipps over the header and reads something in the middle of the
message.
The really strange part is if i put a break point at where its supposed
to read the bytes, it reads everything correctly. If i put a breakpoint
outside the method that reads, it fails. Makes it quite hard to debug.
Any suggestions?
Here's some sample code:
public string Recv ( )
{
if (Connected)
{
Byte[] RecvBufSize = new Byte[8];
string retData,buffer;
Encoding ASCII = Encoding.ASCII;
// Receive number of bytes for this message
Int32 bytes = socket.Read(RecvBufSize, 0, 8);
buffer = ASCII.GetString(RecvBufSize, 0, bytes);
// Theres a bug somewhere that makes us read too late
Byte[] RecvBytes = new Byte[Convert.ToInt32(buffer)];
// Recieve the full message
bytes = socket.Read(RecvBytes, 0, RecvBytes.Length);
retData = ASCII.GetString(RecvBytes, 0, bytes);
return retData;
}
return null;
}