tcp_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Bind to an endpoint
tcp_listener.Bind(new IPEndPoint(IPAddress.Parse(“41.215.42.55”), 10005));
tcp_listener.BeginAccept(new AsyncCallback(OnConnectRequest), tcp_listener);
public void OnConnectRequest( IAsyncResult ar )
{
try
{
Socket listener = (Socket)ar.AsyncState;
NewConnection(listener.EndAccept(ar));
listener.BeginAccept(new AsyncCallback(OnConnectRequest), listener);
}
catch (SocketException e)
{
writeErrMsg(e.Message);
}
}
private byte [] m_byBuff = new byte[1024]; // Received data buffer
public void SetupReceiveCallback( Socket sock )
{
try
{
AsyncCallback recieveData = new AsyncCallback( OnReceivedData );
sock.BeginReceive( m_byBuff, 0, m_byBuff.Length,
SocketFlags.None, recieveData, sock );
}
catch( Exception ex )
{
MessageBox.Show( this, ex.Message, "Setup Recieve Callback failed!" );
}
}
if (protocol_check[3] == "01")
{
string[] arr_data = new string[aryRet.Length];
for (int i = 0; i < aryRet.Length; i++)
arr_data[i] = int.Parse(aryRet[i].ToString()).ToString("X").PadLeft(2, '0');
StringBuilder sb = new StringBuilder();
sb.Append(arr_data[0] + arr_data[1] + "05");
sb.Append(arr_data[3]);
if (protocol_check[2]=="11")
sb.Append(arr_data[16] + arr_data[17]);
else
sb.Append(arr_data[12] + arr_data[13]);
byte[] dataPacket = hexStringToByteArray(sb.ToString());
var crc16 = GetCrc16(dataPacket, 2, 4);
string error_check = string.Format("{0:X}", (int)crc16);
sb.Append(error_check + "0D0A" //get the IMEI
for (int i = 4; i < 12; i++)
nUnitID += arr_data[i];
nUnitID = nUnitID.Substring(1, 15);
//assign to active socket
client.DEVICE_ID = nUnitID;
cdataHandler cdh = new cDataHandler();
Pass it the data received and IMEI of the device:cdh.processMsg(Message, IMEI);
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.