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);
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE