C
C#6mo ago
Eyal

Sockets and WPF

I have a multiplayer trivia server written in C++, in some part of the code I use a condition variable after a client has asked a specific request in order to give an answer when all of the users have finished answering. My client is written in C# and whenever a client ask the request I mentioned earlier their app crushes. Here is the client code to send a request and get a response in the following protocol (1byte = request code, 4bytes = buffer length, Xbytes = buffer with data):
c#
public JObject sendAndRecive(JObject packet, NetworkStream stream, byte code)
{
string jsonString = packet.ToString();

byte[] data_len_bytes = BitConverter.GetBytes(jsonString.Length);
//reverse to reverse the endian orientation
data_len_bytes = data_len_bytes.Reverse().ToArray();

var messageBytes = Encoding.UTF8.GetBytes(jsonString);

byte[] message_and_len = data_len_bytes.Concat(messageBytes).ToArray();

var full_msg_byte = Enumerable.Prepend(message_and_len, code).ToArray();

stream.Write(full_msg_byte, 0, full_msg_byte.Length);

// Receive response from the server
byte[] len_buffer = new byte[5];
int len_bytesRead = stream.Read(len_buffer, 0, len_buffer.Length);

int data_len = (len_buffer[1] << 24) | (len_buffer[2] << 16) | (len_buffer[3] << 8) | len_buffer[4];

byte[] buffer = new byte[data_len];

//read from stream
int bytesRead = stream.Read(buffer, 0, buffer.Length);
//get the string
string utf8String = Encoding.UTF8.GetString(buffer, 0, bytesRead);
//turn string into json
JObject jsonObject = JObject.Parse(utf8String);

return jsonObject;
}
c#
public JObject sendAndRecive(JObject packet, NetworkStream stream, byte code)
{
string jsonString = packet.ToString();

byte[] data_len_bytes = BitConverter.GetBytes(jsonString.Length);
//reverse to reverse the endian orientation
data_len_bytes = data_len_bytes.Reverse().ToArray();

var messageBytes = Encoding.UTF8.GetBytes(jsonString);

byte[] message_and_len = data_len_bytes.Concat(messageBytes).ToArray();

var full_msg_byte = Enumerable.Prepend(message_and_len, code).ToArray();

stream.Write(full_msg_byte, 0, full_msg_byte.Length);

// Receive response from the server
byte[] len_buffer = new byte[5];
int len_bytesRead = stream.Read(len_buffer, 0, len_buffer.Length);

int data_len = (len_buffer[1] << 24) | (len_buffer[2] << 16) | (len_buffer[3] << 8) | len_buffer[4];

byte[] buffer = new byte[data_len];

//read from stream
int bytesRead = stream.Read(buffer, 0, buffer.Length);
//get the string
string utf8String = Encoding.UTF8.GetString(buffer, 0, bytesRead);
//turn string into json
JObject jsonObject = JObject.Parse(utf8String);

return jsonObject;
}
3 Replies
paperClip
paperClip6mo ago
do you have any other info? like stack traces?
Eyal
EyalOP6mo ago
no, since it only occurs when i use multiple users so it's very hard to get to that information
Omnissiah
Omnissiah6mo ago
a bunch of try-catches would help, also some logging like $"i got a message {this long} from {this guy}" and such
Want results from more Discord servers?
Add your server