Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.
C#
   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);
       }
Posted
Updated 16-Aug-13 21:14pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Aug-13 3:54am    
By this limited fragment of code it's hard to see. Why do you think the problem is here? What's "not working", exactly?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900