HTML and CSS Reference
In-Depth Information
// Build the frame data - depending on the length, it can
// be passed one of three ways
dataOut[0]=0x81;
// Store the length in the 2nd byte
if (dataLength<256)
{
dataOut[1]=(byte)dataLength;
dataStart=2;
}
// Store the length in the 3rd and 4th bytes
else if (dataLength<UInt16.MaxValue)
{
dataOut[1]=0xFE;
dataOut[2]=(byte)(dataLength & 0x00FF);
dataOut[3]=(byte)(dataLength & 0xFF00);
dataStart=4;
}
// Store the length in bytes 3 - 9
else
{
dataOut[1]=0xFF;
for (int i=0; i<8; i++)
dataOut[i+2]=(byte)((dataLength >>(i * 8)) & 0x000000FF);
dataStart=10;
}
// Encode the data and store it in the output buffer
byte[] data=Encoding.UTF8.GetBytes(msg);
Array.Copy(data, 0, dataOut, dataStart, dataLength);
// Send the message
try
{
_mySocket.Send(dataOut,
(int)(dataLength+dataStart),
SocketFlags.None);
}
catch
{
// If we get an error, assume the socket has been disconnected
if (Disconnected !=null)
Disconnected(this, EventArgs.Empty);
}
}
}
The SendMessage() method constructs the frame header and then appends the actual text being sent. It then
uses the Send() method of the Socket object to send this frame to the client.
 
Search WWH ::




Custom Search