EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: Bicurico on January 18, 2021, 11:10:59 pm

Title: .Net: Want to connect to a server over TCP
Post by: Bicurico on January 18, 2021, 11:10:59 pm
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 equipment
Port 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: [Select]
7	0.460145	192.168.1.161	192.168.1.2	TCP	60	20 → 55080 [FIN, ACK] Seq=1 Ack=7 Win=5834 Len=0

So 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 20
Device replies on random port, i.e.  56789 --> PC
PC --> acknowledges on port 56789 --> Device
Device --> sends requested data to port 56789 --> PC

Again, 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
Title: Re: .Net: Want to connect to a server over TCP
Post by: retiredfeline on January 18, 2021, 11:24:16 pm
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.
Title: Re: .Net: Want to connect to a server over TCP
Post by: Bicurico on January 18, 2021, 11:33:25 pm
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...

Title: Re: .Net: Want to connect to a server over TCP
Post by: retiredfeline on January 18, 2021, 11:36:38 pm
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.
Title: Re: .Net: Want to connect to a server over TCP
Post by: Bicurico on January 18, 2021, 11:39:36 pm
You are right. I have to send another command before being able to send *IDN.

The program as is works.

Thanks!
Title: Re: .Net: Want to connect to a server over TCP
Post by: amwales on February 10, 2021, 09:45:55 pm
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 see

80   TCP   HTTP (HyperText Transfer Protocol) - used for transferring web pages
20   TCP   FTP Protocol (data) - port for transferring FTP data
21   TCP   FTP Protocol (control) - port for FTP commands and flow control

Assuming 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 dance ;)

Good Luck :)
Title: Re: .Net: Want to connect to a server over TCP
Post by: Bicurico on February 10, 2021, 11:45:20 pm
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