1

I am running an application on my microcontroller(MSP432), which writes data to an Ethernet cable to send it over to PC.

I am using Packet sender to view the data received on the port(502) on PC from MC. Data received on PC

As we can see in the above picture, the port numbers of MC are increment for every packet sent. What will happen when it reaches to the maximum number? Will it restart at some other port number and proceed with the process or will it stop?

Edit1: Modbus protocol library used from http://myarduinoprojects.com/modbus.html

Edit2: Making a call to this function everytime i have a new data to send through MODBUS. Mb.Req(MB_FC_WRITE_MULTIPLE_REGISTERS, 0,11,0);

 if (MbmClient.connect(ServerIp,502)) {
  digitalWrite(GREEN_LED, HIGH);
#if DEBUG
  //Serial.println("connected with modbus slave");
 // Serial.print("Master : ");
  for(int i=0;i<MbmByteArray[5]+6;i++) {
    if(MbmByteArray[i] < 16){
    //Serial.print("0");
  }
    //Serial.print(MbmByteArray[i],HEX);
    if (i != MbmByteArray[5]+5) {
    //Serial.print(".");
  } else {
  //Serial.println();
  }
  }
#endif    
MbmClient.write(MbmByteArray,13+(Count*2));

MbmCounter = 0;
MbmByteArray[7] = 0;
MbmPos = Pos;
MbmBitCount = Count;

*state= true;
MbmClient.stop();
delay(100);
digitalWrite(GREEN_LED, LOW);   
} else {  

  *state= false;
  MbmClient.stop();
}
JKV
  • 13
  • 5
  • Is there a way to see all TCP messages? I mean including 3-way handshake? In the MC, how do you send the messages? Using TCP sockets? or just sending an Ethernet frame containing an IP datagram containing a TCP message? – rodolk Nov 21 '16 at 21:06
  • I made a connection using IP address(Ethernet IP on PC), port number. Is that what u asked?. – JKV Nov 21 '16 at 21:17
  • It looks like it is using a new connection per message, which is pretty poor. The port numbers will wrap around, or if they are being used too quickly the client will get bind errors. – user207421 Nov 21 '16 at 21:18
  • @EJP: So a good approach is to use the same port? Also for a quick connection and transfer is it advised to connect from same port? – JKV Nov 21 '16 at 21:25
  • No, use the same *connection*. That makes it impossible to use more than one source port. You should not specify the source port in the client at all. The system will give you one. – user207421 Nov 21 '16 at 21:28
  • I did not quite get that when u said, "Use the same connection". How different is it from my current approach. Isn't it same as what I am doing currently? Sorry if it sound silly – JKV Nov 21 '16 at 21:35
  • You are using a new connection per message. I already said that. You should use the same connection for all messages. I already said that too. Hard to see what you don't understand about that. – user207421 Nov 21 '16 at 22:05
  • @JanakVarma are you running the example in that link for modbus? Can you show the code? it seems your code is calling this all the time: connect(ServerIp,502) – rodolk Nov 21 '16 at 22:49
  • @JanakVarma can you run Wireshark in the server? We should see more TCP messages than what you are showing here. – rodolk Nov 21 '16 at 22:50

1 Answers1

0

It seems you are using this Modbus example I have never worked with that but I suppose that because the destination port in the code is the same you have in your sniffing image: 502

Probably you are repeatedly calling this method:

void MgsModbus::Req(MB_FC FC, word Ref, word Count, word Pos)

Inside this method you can see this line:

if (MbmClient.connect(ServerIp,502)) { 
...

So every time you call that function a new connection is open. When you open a connection through a socket, the operating system or the network stack needs to select a source port and IP address from where the TCP message is sent.

This is why you see always a new source port and that port is increasing. This is what is called an ephemeral port. How the source port is selected by the TCP stack you are using is implementation dependent, though it's very common to begin with some port and every time a connection is open, it selects the next available port.

If the stack is well programmed, most probably your TCP stack will wrap around and begin with some specific port from 1024 up (First 1024 ports are restricted). The code I saw seems to close the port with this function:

MbmClient.stop()

You need to check ports, after being used, are closed. Otherwise, at some point you will run out of available ports (resource leak).

If you want your socket bound to a specific source port, you need to use a function similar to Linux socket bind

Now, a wiser way is to use all the time the same connection. You may need to modify that example.

rodolk
  • 5,606
  • 3
  • 28
  • 34
  • I have added code on the original post. So whenever i make a connection by calling **MbmClient.connect(ServerIp,502)**, I am also closing the connection. So why is it still connecting to a different port every time? Could you direct me on how to move ahead on using the same port approach. Thanks – JKV Nov 22 '16 at 16:32
  • @JanakVarma, every time your application initiates a connection, it is selecting an [ephemeral port](https://en.wikipedia.org/wiki/Ephemeral_port). This is often chosen randomly, but your TCP implementation seems to not do it that way. Your application is asking TCP to choose a source port for it. You _could_ tell TCP which port to use, rather than leaving it up to TCP to choose the port. – Ron Maupin Nov 22 '16 at 16:41
  • @JanakVarma, I think my response is clear in explaining why it is using always a different source port. It is connecting always to the same destination port 502. I added more wording trying to make it clearer. You can also look at Ron Maupin's comment and read the link he provided. – rodolk Nov 22 '16 at 17:25