how to receive response in c#

mithlesh9 years ago

Hi Guys

I have configured device TK103 on tcp ip and port .How i can get response from that device ?.I am
using c# but i am not able to get response.This is the sample code .IP and port using of my server.
Please suggest me where i am wrong.

            UdpClient receivingUdpClient = new UdpClient(8080);

            //Creates an IPEndPoint to record the IP Address and port number of the sender. 
            // The IPEndPoint will allow you to read datagrams sent from any source.
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), x);
            try
            {

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

                string returnData = Encoding.ASCII.GetString(receiveBytes);

                Console.WriteLine("This is the message you received " +
                                            returnData.ToString());
                Console.WriteLine("This message was sent from " +
                                            RemoteIpEndPoint.Address.ToString() +
                                            " on their port number " +
                                            RemoteIpEndPoint.Port.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
Anton Tananaev9 years ago

Why are you trying to receive TCP data through UDP client? It's never going to work.

mithlesh9 years ago

I want get gps device detail like(latitude and longitude) in c#.How i can get ?Please suggest me.

Anton Tananaev9 years ago

What protocol does your GPS device use? You need to create a server, not a client, to receive data.

mithlesh9 years ago

Hi Anton

I am new in gps device but i have good experience in c#.Please write a step how get data from
gps device in c# or share me sample code if you have.

Thank you very much for spending time for me and helping me.

Anton Tananaev9 years ago

It has nothing to do with GPS devices. It's just basic networking. Does your device use TCP or UDP?

I don't have much C# experience, so I can't provide any code samples, but I'm guessing that you need to use TcpListener class if your device is using TCP protocol (most popular option). UdpClient is probably a right class in case your device uses UDP protocol.

mithlesh9 years ago

Hi Anton

Its using tcp and port 5001.Its showing error when i am trying to use tcp listener class.
Please share me code in php or in c#.That will helpful for me.

Thanks .

Anton Tananaev9 years ago

I don't have code examples in PHP or C#, but I'm sure you can easily find some on Google.

mithlesh9 years ago

Hi Anton

I have written code for server & client both but when i am running it on my local machine its showing error.
//---------------server code ------------
try {
IPAddress ipAd = IPAddress.Parse("xxx.xxx.xxx.xxx"); //This is ip of server where gps device configured
// use local m/c IP address, and
// use the same in the client

/* Initializes the Listener */
TcpListener myList=new TcpListener(ipAd,5001);

/* Start Listeneting at the specified port */
myList.Start();

    Console.WriteLine("The server is running at port 8001...");    
    Console.WriteLine("The local End point is  :" + 
                      myList.LocalEndpoint );
    Console.WriteLine("Waiting for a connection.....");
    
    Socket s=myList.AcceptSocket();
    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
    
    byte[] b=new byte[100];
    int k=s.Receive(b);
    Console.WriteLine("Recieved...");
    for (int i=0;i<k;i++)
        Console.Write(Convert.ToChar(b[i]));

    ASCIIEncoding asen=new ASCIIEncoding();
    s.Send(asen.GetBytes("The string was recieved by the server."));
    Console.WriteLine("\nSent Acknowledgement");

/* clean up */
s.Close();
myList.Stop();

}
catch (Exception ex) {
    Console.WriteLine("Error..... " + ex.StackTrace);
}    
}
//-

----------------client code

try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");

        tcpclnt.Connect("109.237.27.244",5001);
        // use the ipaddress as in the server program
        
        Console.WriteLine("Connected");
        Console.Write("Enter the string to be transmitted : ");
        
        String str=Console.ReadLine();
        Stream stm = tcpclnt.GetStream();
                    
        ASCIIEncoding asen= new ASCIIEncoding();
        byte[] ba=asen.GetBytes(str);
        Console.WriteLine("Transmitting.....");
        
        stm.Write(ba,0,ba.Length);
        
        byte[] bb=new byte[100];
        int k=stm.Read(bb,0,100);
        
        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(bb[i]));
        
        tcpclnt.Close();
    }
    
    catch (Exception ex) {
        Console.WriteLine("Error..... " + ex.StackTrace);
    }
}
Anton Tananaev9 years ago

What is the error? I guess you are doing something wrong if it shows an error.

mithlesh9 years ago

I am running both client and server on my local machine .Is this work?.Or i have to run both on server
where device configured?.

Anton Tananaev9 years ago

I think you need to learn some basics about computer networks first and TCP protocol in particular. This is not a right forum for these kinds of questions.

If you have any questions specific to GPS tracking devices, you are welcome.