.Net: Want to connect to a server over TCP - Page 1
Forum section: electronics / general

author: Bicurico
author_rank: Super Contributor
timestamp: January 18, 2021, 11:10:59 pm
content: Hello,I am trying to understand how to communicate with a test equipment I have, which features an ethernet port and has two open network ports:Port 80: can be accessed with a browser and will allow remote control of the test equipmentPort 20: this is the port I am trying to connect to, because I know that it will allow me to play with manufacturer settings (calibration date, options, etc.).I have the list of possible commands, but i am not able to setup a connection.I made a small test program in VB.net. It allows me to send the commands from my list and they are accepted, because... nothing happens. If I send a wrong command, the screen will show an error message stating the wrong command I just sent. So i can send data to th eequipment.To send commands I just open a simple connection to the device IP on port 20.The problem is that i don't get anything back!So I used Wireshark to see what is happening and in fact, i do get responses like these:Code:70.460145192.168.1.161192.168.1.2TCP6020 → 55080 [FIN, ACK] Seq=1 Ack=7 Win=5834 Len=0So basically, the device is sending a packet from its port 20 to a random port (in this case 55080) to my PC's IP. This port number changes on every message I send to the device.I think this is what "asynchronous communication" is all about and the purpose is to simultaneously allow different clients to the same server. The FTP protocol does that, too.What I don't know is: how to I know inside my VB.Net program, which port the device is sending the response to?Also, I think this is part of a three way handshake, as the above example has Len=0 and does not contain the expected reply. If I send "*IDN", which is a valid command, I would expect to receive a string with the device name and serial number. So what do I need to send back?Any idea on this?I already tried telnet and here the communication is closed after each byte sent. Using SSH does not work, either, the connection process will cause an error message to appear on the device, as the ssh protocol starts by sending unsupported commands.I think the protocol is the simplest possible:PC --> Device on Port 20Device replies on random port, i.e. 56789 --> PCPC --> acknowledges on port 56789 --> DeviceDevice --> sends requested data to port 56789 --> PCAgain, the question is: how do I know what port to listen to?I think I am just missing some direction on how to implement basic TCP networking (I have little knowledge on that).Any help is much appreciated!Regards,Vitor

author: retiredfeline
author_rank: Frequent Contributor
timestamp: January 18, 2021, 11:24:16 pm
content: I think you'll find that 55080 or whatever, is the port that was chosen by the PC program to communicate with the equipment. So your PC's network stack already "knows" the port and you just have to figure out how to read the response, if any, as the connection is bidirectional.

author: Bicurico
author_rank: Super Contributor
timestamp: January 18, 2021, 11:33:25 pm
content: That should not be the case.But it won't receive any response.I think there is something missing.It *could* be me not sending some magic character to the device...

author: retiredfeline
author_rank: Frequent Contributor
timestamp: January 18, 2021, 11:36:38 pm
content: Yeah, maybe the command needs to be well-formed or the device shuts down the connection. I'm not familiar with .NET, but the sort of testing you are doing could be done with a CLI command like nc/netcat on Linux.

author: Bicurico
author_rank: Super Contributor
timestamp: January 18, 2021, 11:39:36 pm
content: You are right. I have to send another command before being able to send *IDN.The program as is works.Thanks!

author: amwales
author_rank: Regular Contributor
timestamp: February 10, 2021, 09:45:55 pm
content: Port numbers that servers listen on are typically registered with IANA, the piece of equipment you have described is listening on ports 80 and 20.If you look these up the in the IANA list we see80 TCP HTTP (HyperText Transfer Protocol) - used for transferring web pages20 TCP FTP Protocol (data) - port for transferring FTP data21 TCP FTP Protocol (control) - port for FTP commands and flow controlAssuming that the device has an FTP server running attempt to connect an FTP client to your device, point it at the IP address and see what you get back.If you want to write you own client you need to look at the FTP protocol to understand what is happening.Now you life is getting difficult for you as FTP is a curious beast since it uses both ports 20 and 21 in a strange danceGood Luck

author: Bicurico
author_rank: Super Contributor
timestamp: February 10, 2021, 11:45:20 pm
content: Thanks for your help.As I wrote in the last post - exactly over you reply - I already sorted out the problem.In my attempt to figure out the protocol, I ended up using Wireshark and recorded the raw communication packages. While this helped me in tracking the protocol layer, it did confuse me as I forgot that I was looking at the raw TCP/IP packages. I mistakenly tried to program a client at this level, which doesn't make sense as the .net implementation already covers the TCP/IP layer.After a bit more hacking I got everything working.Regards,Vitor