3

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

Jawad
  • 111
  • 8
  • 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 Aug 10 '15 at 13:23
  • 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 Aug 10 '15 at 13:25
  • 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 Aug 10 '15 at 13:31
  • 1
    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 Aug 10 '15 at 13:38
  • Also, include the output of your client program from accepting connection to sent. Lets see what you get and parse and send actually – ata Aug 10 '15 at 13:39
  • @Ata: I have added the output of the program, it shows that it is correctly parsing the data sent from the PLC. It also shows that it sends data out to PLC. but PLC never receives it. – Jawad Aug 10 '15 at 14:22
  • Looks like your issue is on the PLC side. When you code both sides of the app at the same time it is always handy to have other tools you can rely on to quickly spot where the problem is. – Guillermo Ruffino Aug 10 '15 at 14:28
  • May be your PLC library is expecting a null terminated string as some suggested, use the NetworkStream, change WirteLine by Write and try adding a writer.WriteByte(0) at the end – Guillermo Ruffino Aug 10 '15 at 14:33
  • @GLM : I have added the syslog server output that shows, PLC (192.168.2.27) connects to the program but reads nothing from it (data READ from server: ". Also, if I use the program as mentioned in the provided link, it reads just fine. – Jawad Aug 10 '15 at 14:38
  • The TCP abstraction is just streams of bytes - there's no inherent messaging concept. So, if you want to send/receive messages, it's up to *you* to implement messaging on top of TCP. Calls to `Send` at one end are not guaranteed to be matched 1-1 with calls to `Receive` at the other end. – Damien_The_Unbeliever Aug 10 '15 at 14:39
  • @GLM : I have also tried different types of terminates such as '\r', '\n' and now I tried writer.WriteByte(0) but it doesnt work as well. Is there any way to read all the bytes sent from my PC over the local network? – Jawad Aug 10 '15 at 14:43
  • You can try a network sniffer like Wireshark, so you are completely sure bytes are coming out of your machine. – Guillermo Ruffino Aug 10 '15 at 14:45
  • I have added the output from wireshark, it seems like it's sending from my machine. I dont know what "TCP acked unseen segment" refers to in this case. – Jawad Aug 10 '15 at 15:13
  • Try adding a Console.ReadLine() before closing your network stream, just in case, you might be disposing the network stream before it gets flushed. You should see the data you send and receive using the "follow tcp stream" option in wireshark – Guillermo Ruffino Aug 10 '15 at 15:41

1 Answers1

-2

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

NicoRiff
  • 4,803
  • 3
  • 25
  • 54