1
votes

I have developed TCP/IP Server in C# listening on port and a GPS device is sending the data to server.

Initially, the device sends the IMEI number to server and server acknowledges with 01. After receiving the acknowledgement by device, a new packet of data is sent to server.

I am able to get IMEI number by TCP Server and after sending acknowledgement, i am not able to receive new packet data from client. I am including my code below also. Please let me know where i am wrong.

I have tested using hercules tcp server tool and next data packet is receiving successfully but from my application it is not working.

           try
        {
            IPAddress ipAdress = IPAddress.Parse(ConfigurationManager.AppSettings["Server"].ToString());
            // You can use local IP as far as you use the 
            //same in client

            // Initializes the Listener
            TcpListener myList = new TcpListener(ipAdress, int.Parse(ConfigurationManager.AppSettings["Port"].ToString()));

            for (; ; )
            { // Run forever, accepting and servicing connections
                //Start Listeneting at the specified port

                myList.Start();

                Console.WriteLine("Server running - Port: 8000");
                Console.WriteLine("Local end point:" + myList.LocalEndpoint);
                Console.WriteLine("Waiting for connections...");

                Socket socket = myList.AcceptSocket();
                // When accepted
                Console.WriteLine("Connection accepted from " + socket.RemoteEndPoint);

                byte[] b = new byte[1000];
                int k = socket.Receive(b);


                Console.WriteLine("echoed {0} bytes.", k);
                Console.WriteLine("Reading IMEI....");

                string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
                File.WriteAllText(@"C:\ClientFiles\testIMEI", hexStr);


                ASCIIEncoding asen = new ASCIIEncoding();
                string response = "01";
                Console.WriteLine("Ackowledgement Data - " + response);
                //
                int sentBytes = socket.Send(asen.GetBytes(response));
                Console.WriteLine("Acknowledgement data sent!\n");

                int count = 0;

                while (socket.Poll(-1, SelectMode.SelectRead))
                {
                    b = new byte[1000];
                    k = socket.Receive(b);
                    if (k > 0)
                    {
                        count++;
                        Console.WriteLine("Reading Client Data - Count - " + count.ToString() + " and lenght " + k.ToString());
                        hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
                        File.WriteAllText(@"C:\ClientFiles\testData" + count.ToString(), hexStr);
                    }
                }

                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }


            myList.Stop();
        }
        catch (Exception ex)
        {
            File.WriteAllText(@"C:\ClientFiles\Error.txt", ex.Message + "****" + ex.InnerException);
        }
1
Please step through with the debugger and tell us where it stucks. - Andre
Thanks Zicore for response, the next packet is empty after sending acknowledgement - hellosayeed
You're trying to receive 1000 bytes, probably it's allready in that buffer. - Andre
The 1000 bytes is a new object and I am using it for next time to receive any new packet. - hellosayeed
int k = socket.Receive(b); receices all bytes which are currently received by the socket with the defined max. of your 1000 bytes. So possibly the second packet is already in that buffer. - Andre

1 Answers

0
votes

I don't see any error in the program, except these comments:

 // Don't use the b.Length in this command:
 string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
 // use k instead, the numbers actually read, not the length of the buffer.
 string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, k)); 

 while (socket.Poll(-1, SelectMode.SelectRead))
 {
     // b has been allocated, don't need this
     // b = new byte[1000];
     k = socket.Receive(b);
     if (k > 0)
     {
         count++;
         Console.WriteLine("Reading Client Data - Count - " + count.ToString() + " and lenght " + k.ToString();
        // use k not Length
        hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, k /*b.Length*/));
        File.WriteAllText(@"C:\ClientFiles\testData" + count.ToString(), hexStr);
     }
}

Do you know if the Poll command ever returns? May be it raises an error and you are missing that one. Check it out.