Dear All,
I have a task to send data to a connected client via a socket,scenario is as follow.
i am working on GPS tracking unit(vehicle tracking) project,which is used to tracking vehicle,each GPS tracking unit connected to a GPRS IP which is our host ip,suppose we have GPRS ip 182.18.81.187 and port is 8286 this is our host IP and port,and each GPS unit connected to this IP and port.
when ever GPS unit (client) connected to Host IP,we save the Client IP and port in data base when it connected ,we need to send some string format data to connected client IP from data base.following is my code but its not working.
string ip = listBoxClientList.SelectedItems[0].ToString();
int lenWS = ip.ToString().Length;
int idxWS = ip.ToString().IndexOf(":");
string IPPartWS = ip.ToString().Substring(0, idxWS);
string portPart = ip.ToString().Substring(idxWS + 1, lenWS - idxWS - 1);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteIpEndPoint = client.RemoteEndPoint as IPEndPoint;
remoteIpEndPoint = new IPEndPoint(IPAddress.Parse(IPPartWS), Int32.Parse(portPart));
client.Bind(remoteIpEndPoint);
Send(txtGPRSCommand.Text, client);
private void Send(string mesg, Socket client)
{
Byte[] sendBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(mesg);
client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, new AsyncCallback(OnSend), client);
}
private void OnSend(IAsyncResult ar)
{
Socket client = (System.Net.Sockets.Socket)ar.AsyncState;
client.EndSend(ar);
}