Modbus and C# -- problem in reading response - c#

I am a newbie to modbus and need some help. I am trying to connect using modbus and serial communication. so far i managed to send data but i am unable to get any. the following is my code.
Building packet
private byte[] BuildPacket(int meter_address,int function,int table_name,int table_offset,int high_byte, int low_byte)
{
try
{
byte[] packet = new byte[6];
packet[0] = Convert.ToByte(meter_address);
packet[1] = Convert.ToByte(function);
packet[2] = Convert.ToByte(table_name);
packet[3] = Convert.ToByte(table_offset);
packet[4] = Convert.ToByte(high_byte);
packet[5] = Convert.ToByte(low_byte);
byte[] checksum = DoCheckSum(packet);
byte[] sendPacket = new byte[8];
sendPacket[0] = packet[0];
sendPacket[1] = packet[1];
sendPacket[2] = packet[2];
sendPacket[3] = packet[3];
sendPacket[4] = packet[4];
sendPacket[5] = packet[5];
sendPacket[6] = checksum[0];
sendPacket[7] = checksum[1];
return sendPacket;
}
catch (Exception)
{
throw;
}
}
Checksum for modbus
try
{
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i < (packet.Length); i++)
{
CRCFull = (ushort)(CRCFull ^ packet[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
byte[] crcByte = new byte[2];
crcByte[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
crcByte[0] = CRCLow = (byte)(CRCFull & 0xFF);
return crcByte;
}
catch (Exception ex)
{
throw ex;
}
}
Connection through serial and modbus
public void ConnectSerialModBus(string COM, int baud)
{
SerialPort port = new SerialPort(COM, baud, Parity.None, 8, StopBits.One);
if (!(port.IsOpen))
{
byte[] sendPacket = BuildPacket(3, 4, 11, 0, 1, 200);
port.Open();
port.RtsEnable = false;
port.Handshake = Handshake.None;
//SEND PACKET TO DEVICE
port.Write(sendPacket, 0, sendPacket.Length);
#region RECEIVE DATA FROM SERIAL
//MAKE PROCESS STOP FOR 5sec
Thread.Sleep(3000);
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.RtsEnable = true;
int size = port.ReadBufferSize;
byte[] readingbyte = new byte[size];
port.Read(readingbyte, 0, readingbyte.Length);
string reading = Encoding.GetEncoding("Windows-1252").GetString(readingbyte);
port.Close();
port.Dispose();
#endregion
}
}
The problem is when it comes to reading the response, the program gets stuck. if possible please help me out figure what is wrong with it.

found a solution to the problem, the problem was with the thread.sleep. was giving it 3secs which is too much for the rtf to receive the packet. changed to 10ms and worked fine.

Related

Retrying Method

I am communicating with a device via UDP, I send a message and I receive the response.
public byte[] Rx_message = new byte[12];
public byte[] packedMessage2 = new byte[12];
public IPEndPoint sendEndPoint;
public void senderUdpClient(byte Type, byte CommandC, byte CodeCmd, Int32 X, Int32 Y)
{
string serverIP = "192.168.2.11";
int sendPort = 40960;
int receivePort = 40961;
var span = new Span<byte>(packedMessage2);
span[0] = Type;
span[1] = CommandC;
span[2] = CodeCmd;
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(3, 4), X);
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(7, 4), Y);
var sum = unchecked((byte)packedMessage2.Take(11).Sum(x => x));
span[11] = sum;
sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
try
{
UdpClient senderClient = new UdpClient();
senderClient.Connect(this.sendEndPoint);
senderClient.Send(packedMessage2, packedMessage2.Length);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
UdpClient receivingUdpClient = new UdpClient(receivePort);
receivingUdpClient.Client.ReceiveTimeout = 50;
// receive message
Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
senderClient.Close();
receivingUdpClient.Close();
}
catch (Exception ex)
{
}
}
I want to add a retry mechanism if the equipment does not respond in the event of a network problem,
(if I have an error: Rx_message [0] = -1 and if all goes well Rx_message [0] = 2)
Normally I should do 2 retry if I can connect I continue, otherwise I display an error message
public byte[] Rx_message = new byte[12];
public byte[] packedMessage2 = new byte[12];
public IPEndPoint sendEndPoint;
public int Connected = 0;
public void senderUdpClient(byte Type, byte CommandC, byte CodeCmd, Int32 X, Int32 Y)
{
string serverIP = "192.168.2.11";
int sendPort = 40960;
int receivePort = 40961;
var span = new Span<byte>(packedMessage2);
span[0] = Type;
span[1] = CommandC;
span[2] = CodeCmd;
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(3, 4), X);
BinaryPrimitives.WriteInt32LittleEndian(span.Slice(7, 4), Y);
var sum = unchecked((byte)packedMessage2.Take(11).Sum(x => x));
span[11] = sum;
sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
try
{
for(int i = 0; i< 2; i++)
{
UdpClient senderClient = new UdpClient();
senderClient.Connect(this.sendEndPoint);
senderClient.Send(packedMessage2, packedMessage2.Length);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
UdpClient receivingUdpClient = new UdpClient(receivePort);
receivingUdpClient.Client.ReceiveTimeout = 50;
// receive message
Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
//first case where it is connected and I receive my answer correctly
if (Rx_message[0] == 2)
{
Connected = 1;
break;
}
//if I can't connect
if (Connected == 0)
{
//first try
if (i < 1)
{
Thread.Sleep(20);
continue;
}
//second try
if (i == 1)
{
//these two lines I put it just so that the code goes to try and shows me the error code
receivingUdpClient.Client.ReceiveTimeout = 1500;
Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
}
}
senderClient.Close();
receivingUdpClient.Close();
}
}
catch (Exception ex)
{
if (Connected == 0)
{
MessageBox.Show("Error Connection");
}
sbyte type_message = Convert.ToSByte(Rx_message[0]);
if (type_message == -1)
{
if (Rx_message[3] == 1)
{
MessageBox.Show("Type invalide");
}
if (Rx_message[3] == 2)
{
MessageBox.Show("Commande invalide");
}
if (Rx_message[3] == 3)
{
MessageBox.Show("Argument invalide!");
}
if (Rx_message[3] == 4)
{
MessageBox.Show("Erreur CS!");
}
}
}
}
Here is the code I tried to do, once it gets to the reading line
Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
he goes directly to Catch I don't know how I can force him to continue the code, otherwise someone can help me improve this mechanism

Send following byte received via SerialPort (USB VCP) without delay

I am trying to test a flash memory chip on an electronic device having USB comms. Using SerialPort (USB VCP).
I am sitting in a for loop scanning through the memory addresses in sequence with a small delay following each read. However I want to get rid of the delay and have this run as follows:
C# App sends byte command to i.e. read address 0 and return 32 bytes (address 0 to 31)
When C# app receives the 32 bytes then immediately send request for address 31 etc
repeat...
So how do I do it in this fast manner and not having to have a delay giving the serial port time to receive the bytes but rather have the next request send immediately following receipt of previous request results ?
Update #2
async private void buttonMemoryTest_Click(object sender, EventArgs e)
{
byte[] bytes = new byte[6];
memoryAddress = 0x00000000;
bytes[0] = 0x90;
bytes[1] = 0x01;
try
{
sendStopWatch.Stop();
sendStopWatch.Reset();
sendStopWatch.Start();
//2097152
for (UInt32 counter = 0; counter < 100; counter++)
{
bytes[2] = (byte)(memoryAddress >> 24);
bytes[3] = (byte)(memoryAddress >> 16);
bytes[4] = (byte)(memoryAddress >> 8);
bytes[5] = (byte)(memoryAddress);
serialPortComms.Write(bytes, 0, 6);
await Task.Delay(1);
memoryAddress += 32;
time = sendStopWatch.Elapsed;
textBoxMemoryTestTime.Text = Math.Round(time.TotalSeconds, 2) + "s";
textBoxPageCounter.Text = Convert.ToString(counter);
}
}
catch (InvalidOperationException)
{
}
catch (TimeoutException)
{
}
}
void serialPortComms_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = serialPortComms.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = serialPortComms.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
string newStr = "";
//See if there was data send back
try
{
if (selfTestClicked == 1)
{
if (selfTestResponseASCII == 1)
{
newStr = Encoding.ASCII.GetString(data, 0, data.Length);
}
else
{
newStr = BitConverter.ToString(data).Replace("-", " ");
}
textBoxReceiveByte_AppendText(newStr);
}
else
{
//
if (checkBoxASCII.Checked)
{
//newStr = Encoding.UTF8.GetString(data, 0, data.Length);
newStr = Encoding.ASCII.GetString(data, 0, data.Length);
}
else
{
newStr = BitConverter.ToString(data).Replace("-", " ");
}
if (checkBoxCR_LF.Checked)
{
newStr += "\r\n";
}
textBoxReceiveByte_AppendText(newStr);
//textBoxReceiveByte_ChangeText(newStr);
//processRxData(data);
}
}
catch (IndexOutOfRangeException)
{
}
}

Modbus TCP Communication C#

I am looking for a little help.
I have a program to communicate with a controller through Modbus TCP.
The only problem is I cannot extend the Nop from 125 to 400 because I got Illegal Data Address error message.
Could you please help me with this?
try
{
byte slaveid = 1;
byte function = 4;
ushort id = function;
ushort startAddress = 0;
uint NoP = 125;
byte[] frame = ReadInputRegistersMsg(id, slaveid, startAddress, function, NoP);
this.Write(frame); //data send to controller
Thread.Sleep(100);
byte[] buffReceiver = this.Read(); //data recieving from controller
int SizeByte = buffReceiver[8]; // Data what I got from the controller
UInt16[] temp = null;
if (function != buffReceiver[7])
{
byte[] byteMsg = new byte[9];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg);
byte[] errorbytes = new byte[3];
Array.Copy(buffReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes); // check the answer -> error message
}
else
{
byte[] byteMsg = new byte[9 + SizeByte];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg); // Show received messages in windows form app
Array.Copy(buffReceiver, 9, data, 0, data.Length);
temp = Word.ConvertByteArrayToWordArray(data); // Convert Byte[]-> Word[]
}
// Result
if (temp == null) return;
string result = string.Empty;
//foreach (var item in temp) // show all the data
for(int i=0;i<100;i++) // show the first 100 data
{
//result += string.Format("{0} ", item);
result += temp[i];
}
textBox3.Text = result; // insert the result into the textbox (windows form app)
}
catch
{
}
The ReadInputRegister Message is the following:
private byte[] ReadInputRegistersMsg(ushort id, byte slaveAddress, ushort startAddress, byte function, uint NoP)
{
byte[] frame = new byte[12];
frame[0] = (byte)(id >> 8); // Transaction Identifier High
frame[1] = (byte)id; // Transaction Identifier Low
frame[2] = 0; // Protocol Identifier High
frame[3] = 0; // Protocol Identifier Low
frame[4] = 0; // Message Length High
frame[5] = 6; // Message Length Low(6 bytes to follow)
frame[6] = slaveAddress; // Slave address(Unit Identifier)
frame[7] = function; // Function
frame[8] = (byte)(startAddress >> 8); // Starting Address High
frame[9] = (byte)startAddress; // Starting Address Low
frame[10] = (byte)(NoP >> 8); // Quantity of Registers High
frame[11] = (byte)NoP; // Quantity of Registers Low
return frame;
}
The hard limit for modbus is 125 NoP

C# Modbus SlaveException reading registry

I'm working on a c# project Which have to comunicate with PLC by TCP Modbus.
I'm using Nmodbus Library and it works fine.
The problem is when I try to read/write Registry over 12000.
I get this exception:
Exception 'Modbus.SlaveException' Function Code: 131
This is the part of the code which generates the exception:
private TcpClient tcpClient;
private ModbusIpMaster master;
private void connect(){
// connect to Modbus TCP Server
string ipAddress = "192.168.77.7"; //Input WISE IP
int tcpPort = 502;
tcpClient = new TcpClient(ipAddress, tcpPort);
// create Modbus TCP Master by the tcp client
master = ModbusIpMaster.CreateIp(tcpClient);
// rewrite the value of AO ch0 (40020~40021) by float
byte slaveID = 1;
// ushort rewriteAddress = 20;
// ushort[] rewriteValue = new ushort[2] { 0, 0 };
// float[] floatData = new float[1] { 223.456F };
// Buffer.BlockCopy(floatData, 0, rewriteValue, 0, 4);
Random random = new Random();
// read the holding register 12001~12005
// write the holding register 301~305
ushort startAddress = 12000;
ushort numOfPoints = 5;
master.Transport.ReadTimeout = 1000;
while (!_shouldStop1)
{
try
{
ushort[] register = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);
for (int index = 0; index <register.Length; index++)
{
Console.WriteLine(string.Format("AO[{0}] = {1}", index,register[index]));
}
for (ushort index = 0; index < 4; index++)
{
master.WriteSingleRegister(slaveID, (ushort)(301 + index),(ushort) data[index]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Any suggestion will be appreciated.
Thanks,
Federico.

Decrypt WebSocket client communication with C#

I am trying to make a demonstration for web socket communication for one of our new projects at work, and I've been trying to revise an old web socket server program I wrote quite a while ago.
It does a proper handshake and can send data to the client properly (which is all it REALLY needs to do), but the client data that comes back is in the special web socket communication protocol, and I am not that good with working with binary or hex or encryption algorithms.
I know from research that the text I'm receiving back contains a frame around it, and that it is sha1 encrypted, but my problem is that I have no idea how to read or remove this frame or unencrypt it.
Here is the full code of the web server so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Threading;
using System.Security.Cryptography;
namespace WebSocks
{
public class WebSockServer
{
/// <summary>
/// Port number to listen on
/// </summary>
private const int PortNumber = 8181;
/// <summary>
/// Socket which awaits connections
/// </summary>
private Socket ListenerSocket;
/// <summary>
/// Thread in which we await for incomming connections.
/// </summary>
private System.Threading.Thread _serverThread;
public delegate void ClientConnectedHandler (Socket Sock);
public delegate void ReceivedDataHandler(Socket Sock, string Message);
public event ClientConnectedHandler ClientConnected;
public event ReceivedDataHandler ReceivedData;
static WebSockServer() { }
/// <summary>
/// Starts thread with listening socket.
/// </summary>
public void Start()
{
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(Listen);
_serverThread = new System.Threading.Thread(ts);
_serverThread.Start();
}
/// <summary>
/// Stops listening for connections.
/// </summary>
public void End()
{
_serverThread.Abort();
ListenerSocket.Dispose();
}
public void Listen()
{
//Start listening
ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint ep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), PortNumber);
ListenerSocket.Bind(ep);
ListenerSocket.Listen(5);
while (true)
{
//New client
Socket client = ListenerSocket.Accept();
//Receiving clientHandshake
string clientHandshake = String.Empty;
byte[] buffer = null;
int readBytes = 0;
do
{
buffer = new byte[client.Available];
readBytes = client.Receive(buffer);
clientHandshake += Encoding.UTF8.GetString(buffer);
}
while (client.Available > 0);
//Last eight bytes are body of requets (we should include it in response)
byte[] secKey3 = buffer.Skip(readBytes - 8).Take(8).ToArray();
//Variables we can extract from clientHandshake
string clientOrigin = String.Empty;
string secKey1 = String.Empty;
string secKey2 = String.Empty;
string WebSocketVersion = String.Empty;
int WSV = 0;
string WebSocketKey = String.Empty;
//Extracting values from headers (key:value)
string[] clientHandshakeLines = Regex.Split(clientHandshake, Environment.NewLine);
foreach (string hline in clientHandshakeLines)
{
int valueStartIndex = hline.IndexOf(':') + 2;
if (valueStartIndex > 0)
{
if (hline.StartsWith("Origin"))
{
clientOrigin = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
else if (hline.StartsWith("Sec-WebSocket-Key2"))
{
secKey2 = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
else if (hline.StartsWith("Sec-WebSocket-Key1"))
{
secKey1 = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
if (hline.StartsWith("Sec-WebSocket-Version"))
{
WebSocketVersion = hline.Replace("Sec-WebSocket-Version: ", "");
WSV = Convert.ToInt32(WebSocketVersion);
}
if (hline.StartsWith("Sec-WebSocket-Key"))
{
WebSocketKey = hline.Replace("Sec-WebSocket-Key: ", "");
}
}
}
if (!String.IsNullOrEmpty(WebSocketVersion)) //WebSocketVersion 8 and up handshake check
{
//New WebSocketVersion number, included after Version 8
StringBuilder mResponse = new StringBuilder();
mResponse.AppendLine("HTTP/1.1 101 Switching Protocols");
mResponse.AppendLine("Upgrade: WebSocket");
mResponse.AppendLine("Connection: Upgrade");
mResponse.AppendLine(String.Format("Sec-WebSocket-Accept: {0}", ComputeWebSocketHandshakeSecurityHash09(WebSocketKey)) + Environment.NewLine);
byte[] HSText = Encoding.UTF8.GetBytes(mResponse.ToString());
client.Send(HSText, 0, HSText.Length, 0);
}
else
{
//This part is common for all websockets editions (v. 75 & v.76)
client.Send(Encoding.UTF8.GetBytes("HTTP/1.1 101 Web Socket Protocol Handshake" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Upgrade: WebSocket" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Connection: Upgrade" + Environment.NewLine));
if (String.IsNullOrEmpty(secKey1) && String.IsNullOrEmpty(secKey2)) //75 or less handshake check
{
client.Send(Encoding.UTF8.GetBytes(String.Format("WebSocket-Origin: {0}", clientOrigin) + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("WebSocket-Location: ws://localhost:8181/websock" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes(Environment.NewLine));
}
else //76 handshake check
{
//Keys present, this means 76 version is used. Writing Sec-* headers
client.Send(Encoding.UTF8.GetBytes(String.Format("Sec-WebSocket-Origin: {0}", clientOrigin) + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Sec-WebSocket-Location: ws://localhost:8181/websock" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes(Environment.NewLine));
//Calculating response body
byte[] secret = CalculateSecurityBody(secKey1, secKey2, secKey3);
client.Send(secret);
}
}
if (ClientConnected != null)
{
ClientConnected(client);
}
Thread t = new Thread(new ParameterizedThreadStart(WaitForMessages));
t.Start(client);
}
}
private static void SendMessage(string Msg, Socket client, int WebSockVersion)
{
if (WebSockVersion >= 8)
{
bool IsFinal = true;
int OpCode = 1;
int? Mask = null;
byte[] payload = Encoding.UTF8.GetBytes(Msg);
int PayloadLength = payload.Length;
byte[] buffer = new byte[64]; // for working out the header
int offset = 0;
buffer[offset++] = (byte)((IsFinal ? 128 : 0) | ((int)OpCode & 15));
if (PayloadLength > ushort.MaxValue)
{ // write as a 64-bit length
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | 127);
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = (byte)(PayloadLength >> 24);
buffer[offset++] = (byte)(PayloadLength >> 16);
buffer[offset++] = (byte)(PayloadLength >> 8);
buffer[offset++] = (byte)(PayloadLength);
}
else if (PayloadLength > 125)
{ // write as a 16-bit length
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | 126);
buffer[offset++] = (byte)(PayloadLength >> 8);
buffer[offset++] = (byte)(PayloadLength);
}
else
{ // write in the header
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | PayloadLength);
}
if (Mask.HasValue)
{
int mask = Mask.Value;
buffer[offset++] = (byte)(mask >> 24);
buffer[offset++] = (byte)(mask >> 16);
buffer[offset++] = (byte)(mask >> 8);
buffer[offset++] = (byte)(mask);
}
// you might want to manually combine these into 1 packet
client.Send(buffer, 0, offset, SocketFlags.None);
client.Send(payload, 0, payload.Length, SocketFlags.None);
}
else
{
client.Send(new byte[] { 0x00 });
client.Send(Encoding.UTF8.GetBytes(Msg));
client.Send(new byte[] { 0xFF });
}
}
private void WaitForMessages(object client)
{
Socket sock = (Socket)client;
byte[] buffer = new byte[1024];
while (true)
{
sock.Receive(buffer);
ReceivedData(sock, Encoding.UTF8.GetString(buffer));
}
}
public byte[] CalculateSecurityBody(string secKey1, string secKey2, byte[] secKey3)
{
//Remove all symbols that are not numbers
string k1 = Regex.Replace(secKey1, "[^0-9]", String.Empty);
string k2 = Regex.Replace(secKey2, "[^0-9]", String.Empty);
//Convert received string to 64 bit integer.
Int64 intK1 = Int64.Parse(k1);
Int64 intK2 = Int64.Parse(k2);
//Dividing on number of spaces
int k1Spaces = secKey1.Count(c => c == ' ');
int k2Spaces = secKey2.Count(c => c == ' ');
int k1FinalNum = (int)(intK1 / k1Spaces);
int k2FinalNum = (int)(intK2 / k2Spaces);
//Getting byte parts
byte[] b1 = BitConverter.GetBytes(k1FinalNum).Reverse().ToArray();
byte[] b2 = BitConverter.GetBytes(k2FinalNum).Reverse().ToArray();
//byte[] b3 = Encoding.UTF8.GetBytes(secKey3);
byte[] b3 = secKey3;
//Concatenating everything into 1 byte array for hashing.
List<byte> bChallenge = new List<byte>();
bChallenge.AddRange(b1);
bChallenge.AddRange(b2);
bChallenge.AddRange(b3);
//Hash and return
byte[] hash = MD5.Create().ComputeHash(bChallenge.ToArray());
return hash;
}
public String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
{
const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String secWebSocketAccept = String.Empty;
// 1. Combine the request Sec-WebSocket-Key with magic key.
String ret = secWebSocketKey + MagicKEY;
// 2. Compute the SHA1 hash
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
// 3. Base64 encode the hash
secWebSocketAccept = Convert.ToBase64String(sha1Hash);
return secWebSocketAccept;
}
}
}
I know its very crude, it doesn't clean up after itself, and it doesn't separate the different connections properly and hold them in a list or anything, this is purely for a demonstration project.
So the main section I need help with is the WaitForMessages function:
private void WaitForMessages(object client)
{
Socket sock = (Socket)client;
byte[] buffer = new byte[1024];
while (true)
{
sock.Receive(buffer);
//Remove Frame and decrypt here
ReceivedData(sock, Encoding.UTF8.GetString(buffer));
}
}
I really just want to drop some code in here that will work, if you point me to a "demonstration that is kind of like your code and you should be able to figure it out from this" I'm really not going to understand it, I can almost guarantee you. I'm an app developer, not an API developer, I just don't want to wait until Microsoft finally gets around to writing an API into .NET 6.0 or something for this feature.

Categories

Resources