Packet issues
-
I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.
public class Data { //Default constructor public Command cmdCommand; //Command type (login, logout, send message, etcetera) public string strMessage; //Message text public string strName; //Name by which the client logs into the room public Data() { cmdCommand = Command.Null; strMessage = null; strName = null; } //Converts the bytes into an object of type Data public Data(byte\[\] data) { //The first four bytes are for the Command cmdCommand = (Command)BitConverter.ToInt32(data, 0); //The next four store the length of the name int nameLen = BitConverter.ToInt32(data, 4); //The next four store the length of the message int msgLen = BitConverter.ToInt32(data, 8); //This check makes sure that strName has been passed in the array of bytes if (nameLen > 0) strName = Encoding.UTF8.GetString(data, 12, nameLen); else strName = null; //This checks for a null message field if (msgLen > 0) strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen); else strMessage = null; } //Converts the Data structure into an array of bytes public byte\[\] ToByte() { List result = new List(); //First four are for the Command result.AddRange(BitConverter.GetBytes((int)cmdCommand)); //Add the length of the name if (strName != null) result.AddRange(BitConverter.GetBytes(strName.Length)); else result.AddRange(BitConverter.GetBytes(0)); //Length of the message if (strMessage != null) result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage))); else result.AddRange(BitConverter.GetBytes(0)); //Add the name if (strName !=
-
I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.
public class Data { //Default constructor public Command cmdCommand; //Command type (login, logout, send message, etcetera) public string strMessage; //Message text public string strName; //Name by which the client logs into the room public Data() { cmdCommand = Command.Null; strMessage = null; strName = null; } //Converts the bytes into an object of type Data public Data(byte\[\] data) { //The first four bytes are for the Command cmdCommand = (Command)BitConverter.ToInt32(data, 0); //The next four store the length of the name int nameLen = BitConverter.ToInt32(data, 4); //The next four store the length of the message int msgLen = BitConverter.ToInt32(data, 8); //This check makes sure that strName has been passed in the array of bytes if (nameLen > 0) strName = Encoding.UTF8.GetString(data, 12, nameLen); else strName = null; //This checks for a null message field if (msgLen > 0) strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen); else strMessage = null; } //Converts the Data structure into an array of bytes public byte\[\] ToByte() { List result = new List(); //First four are for the Command result.AddRange(BitConverter.GetBytes((int)cmdCommand)); //Add the length of the name if (strName != null) result.AddRange(BitConverter.GetBytes(strName.Length)); else result.AddRange(BitConverter.GetBytes(0)); //Length of the message if (strMessage != null) result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage))); else result.AddRange(BitConverter.GetBytes(0)); //Add the name if (strName !=
-
I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.
public class Data { //Default constructor public Command cmdCommand; //Command type (login, logout, send message, etcetera) public string strMessage; //Message text public string strName; //Name by which the client logs into the room public Data() { cmdCommand = Command.Null; strMessage = null; strName = null; } //Converts the bytes into an object of type Data public Data(byte\[\] data) { //The first four bytes are for the Command cmdCommand = (Command)BitConverter.ToInt32(data, 0); //The next four store the length of the name int nameLen = BitConverter.ToInt32(data, 4); //The next four store the length of the message int msgLen = BitConverter.ToInt32(data, 8); //This check makes sure that strName has been passed in the array of bytes if (nameLen > 0) strName = Encoding.UTF8.GetString(data, 12, nameLen); else strName = null; //This checks for a null message field if (msgLen > 0) strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen); else strMessage = null; } //Converts the Data structure into an array of bytes public byte\[\] ToByte() { List result = new List(); //First four are for the Command result.AddRange(BitConverter.GetBytes((int)cmdCommand)); //Add the length of the name if (strName != null) result.AddRange(BitConverter.GetBytes(strName.Length)); else result.AddRange(BitConverter.GetBytes(0)); //Length of the message if (strMessage != null) result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage))); else result.AddRange(BitConverter.GetBytes(0)); //Add the name if (strName !=
At which point do you get this error? I suspect, the byte[] you pass to the constructor does not contain the correct data. Add a check in the beginning of the constructor to ensure the byte[] is long enough at least.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
At which point do you get this error? I suspect, the byte[] you pass to the constructor does not contain the correct data. Add a check in the beginning of the constructor to ensure the byte[] is long enough at least.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)As far as I can tell, I get the error after executing the following commands: In the OnAccept method..
byte\[\] bufData = new byte\[1024\]; clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive), new object\[\] {clientSocket, bufData});
In the OnReceive method..
Socket clientSocket = null;
Data msgReceived = null;
byte[] bufData = new byte[1024];
bool bypass = false;
ClientInfo cInfo = null;try { bool blInvalidClient = false; try { object\[\] oData = (object\[\]) ar.AsyncState; clientSocket = (Socket) oData\[0\]; bufData = (byte\[\]) oData\[1\]; clientSocket.EndReceive(ar); //Console.WriteLine(clientList.Count); } catch (SocketException) { blInvalidClient = true; } if (!blInvalidClient) { //Transform the array of bytes received from the user into an //intelligent form of object Data msgReceived = Packet.PacketReceived(bufData);
...
The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.
-
As far as I can tell, I get the error after executing the following commands: In the OnAccept method..
byte\[\] bufData = new byte\[1024\]; clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive), new object\[\] {clientSocket, bufData});
In the OnReceive method..
Socket clientSocket = null;
Data msgReceived = null;
byte[] bufData = new byte[1024];
bool bypass = false;
ClientInfo cInfo = null;try { bool blInvalidClient = false; try { object\[\] oData = (object\[\]) ar.AsyncState; clientSocket = (Socket) oData\[0\]; bufData = (byte\[\]) oData\[1\]; clientSocket.EndReceive(ar); //Console.WriteLine(clientList.Count); } catch (SocketException) { blInvalidClient = true; } if (!blInvalidClient) { //Transform the array of bytes received from the user into an //intelligent form of object Data msgReceived = Packet.PacketReceived(bufData);
...
The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.
-
Problem seems to be here. Not sure I understand why...
if (nameLen > 0) strName = Encoding.UTF8.GetString(data, 12, nameLen); else strName = null;
-
As far as I can tell, I get the error after executing the following commands: In the OnAccept method..
byte\[\] bufData = new byte\[1024\]; clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive), new object\[\] {clientSocket, bufData});
In the OnReceive method..
Socket clientSocket = null;
Data msgReceived = null;
byte[] bufData = new byte[1024];
bool bypass = false;
ClientInfo cInfo = null;try { bool blInvalidClient = false; try { object\[\] oData = (object\[\]) ar.AsyncState; clientSocket = (Socket) oData\[0\]; bufData = (byte\[\]) oData\[1\]; clientSocket.EndReceive(ar); //Console.WriteLine(clientList.Count); } catch (SocketException) { blInvalidClient = true; } if (!blInvalidClient) { //Transform the array of bytes received from the user into an //intelligent form of object Data msgReceived = Packet.PacketReceived(bufData);
...
The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.