2
votes

I have written a TCPClient program to run on my PC. It first initiates a TCP listener to listen on a specific port then reads/writes from/to multiple TCP clients on multiple threads.
I am able to read from the client but whenever I try to send data to it, the program displays that it has sent the data, but the client does not receive anything.

Here's the code:

            TcpClient client = listener.AcceptTcpClient();
            var childSocketThread = new Thread(() =>
            {
                if (client.Connected)
                {
                    using (NetworkStream stream = client.GetStream())
                 {
                    Console.WriteLine("connected");
                    byte[] data = new byte[1000];
                    try
                    {
                        if (stream.CanRead)
                        {
                            stream.Read(data, 0, 1000);
                            string dataStr = Encoding.ASCII.GetString(data);
                            string dataa = dataStr.TrimEnd('\0');
                            //Console.WriteLine(dataa);
                            if (dataa.Length > 10)
                            {
                                deviceid = ParseRequest(dataa);

                                byte[] sendnow = Encoding.ASCII.GetBytes(reply[deviceid]);

                                Array.Clear(data, 0, 1000);
                                Console.WriteLine("Recieved data: " + dataa);
                                Console.WriteLine("Sending data");
                                using (StreamWriter writer = new StreamWriter(stream))
                                {
                                    writer.AutoFlush = true;
                                    writer.WriteLine(reply[deviceid]);
                                }
                                Console.WriteLine(reply[deviceid]);
                                Console.WriteLine("Sent");
                            }

                            Console.WriteLine();
                        }
                    }
                    catch (Exception es)
                    {
                        Console.WriteLine(es);
                    }
                 }
                }

            });
            childSocketThread.Start();    

The server device that I am using is a PLC. Also, things I have already tried:
1) sending directly using Socket.Send method.
2) sending directly using NetworkStream method.
3) accepting the TCP connection as sockets. (Socket device = listener.AcceptSocket).

None of these methods seem to send to the device, even though the program tells me that it had no issues sending data since it displays "Sent" after attempting to send data. I downloaded another program from this link http://www.codeproject.com/Articles/488668/Csharp-TCP-Server. The test app they provide with it is able to send and receive data on the same port as my program running on the same PC.

I haven't been able to get any direction on how to diagnose and more importantly solve this issue. Any ideas?

Update 2015-08-10 11:18 a.m.:
Output of the Program is as follows: enter image description here

Update 2015-08-10 11:32 a.m.:
Output of Syslog Console: enter image description here

Update 2015-08-10 12:07 p.m.:
Output of Wireshark:
enter image description here

1
You need to show us both the client and the server code. I can already see plenty of things you're doing wrong, but we really need both sides. - Luaan
The client code is a PLC code in BCL language, I am not sure if many people are aware of that. But on the other hand, the test code app (link to code project) works fine with the same client code. - Jawad
Are you sure you're supposed to send a string terminated with CRLF? The data you seem to be getting from the device indicate \0 as the terminator. And definitely avoid using StreamWriter/StreamReader anywhere near a network stream :)) - Luaan
More importantly, if \0 is a terminator of current request he might read more data and try to parse it as the buffer is 1000 long. Also, the Read function returns an int telling you how much it is actually able to read. It is important that you keep reading from stream until you reach \0 or 1000 because your client might only be able to read 11 bytes which might not be enough to correctly parse it. - ata
Also, include the output of your client program from accepting connection to sent. Lets see what you get and parse and send actually - ata

1 Answers

-2
votes

We need you to post both sides code. Nevertheless, here is some code that works just fine, you can use it to see if you are doing something wrong.

http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C