TCP Connection on Specific Port

Hello everyone,

I’m trying to make a GET request from the arduino to my computer in a specific port (10).
And I’m having serious troubles…

Here is the function I’m using:

void wakeOnLan() {
  char character;
  char response[LRG_BUF];
  byte index = 0;
  int timeout = 0;
  Serial.print(F("Connecting"));
  if (connection.connect(serverIP, 10) == 1) {
    connection.println(F("GET / HTTP/1.1\r\n"));
    Serial.println(F("..."));
    while (connection.connected()) {
      while (connection.available()) {
        timeout = 0;
        character = connection.read();
        if (index < (sizeof(response) - 1)) {
          response[index] = character;
          Serial.write(character);
          index++;
        }
      }
      timeout++;
      delay(1);
      if (timeout >= 2500) {
        break;
      }
    }
    connection.stop();
  }
  else {
    Serial.println(F(": Failed to connect."));
  }
}

Here is the Serial Monitor output:

Connecting...

So, as I can see the problem is that the outgoing port (from my arduino as a client, to the server) is port 10, but when the connection arrives on my server the destination port (the one I need to be 10) it’s not, it’s the 49154. And in each try the destination port increases by 1.

Is it possible to force the connection on port 10?

but when the connection arrives on my server the destination port (the one I need to be 10) it's not, it's the 49154.

Is that possibly the client return port that the server would use to send a reply to the client?

zoomkat:
Is that possibly the client return port that the server would use to send a reply to the client?

Hi zoomkat, thanks for you help.

I don’t think it’s possible, and even if it was, it should at least answer with a HTTP 200 Ok, not a 400 Bad Request (if the destination port was correctly).

The packet you have displayed looks like a valid response to a connection to port 10; do you need the connection to be from port 10 as well? Add some intermediate debugging; I don't think your problem is where you think it is.

westfw: The packet you have displayed looks like a valid response to a connection to port 10; do you need the connection to be from port 10 as well? Add some intermediate debugging; I don't think your problem is where you think it is.

Helo westfw, thanks for your help.

Are you saying that those connection was succefully made on port 10? I don't need to it to be from port 10, just to port 10.

I need to access "localhost:10", but instead of local host I'm using my IP (not really needed since it's both on the same network, but I couldn't manage to connect using my local IP).

I'm sorry, but I'm not an expert, can you be more specifc with "some intermediate debugging"? I just started using WireShark today, just trying to solve this problem that is driving me nuts for months...

The traces show a successful connection (packet 637, 637 - the syn, syn-ack pair that open the connection.) Then you get there’s a mysterious “segment of a reassembled PDU”, and then you get the 400 error response (and have the detailed packet contents) that shows the response was FROM port 10 to . That indicates to me that you successfully made a connection to port 10. Something went wrong after that.

 connection.println(F("GET / HTTP/1.1\r\n"));

is probably wrong (has a newline in the print, AND uses println, so you’ll get a double line-ending. But I don’t know if that would cause this problem.)

I don't think it's possible,

You might try your browser connecting to google and see if those wireshark results show a large port number.

Using the F() macro will send the request one character per packet. Maybe the server doesn't like that. Try it without the F() macro and using HTTP/1.0.

// change this
 connection.println(F("GET / HTTP/1.1\r\n"));
// to this
 connection.println("GET / HTTP/1.0\r\n");

westfw:
The traces show a successful connection (packet 637, 637 - the syn, syn-ack pair that open the connection.) Then you get there’s a mysterious “segment of a reassembled PDU”, and then you get the 400 error response (and have the detailed packet contents) that shows the response was FROM port 10 to . That indicates to me that you successfully made a connection to port 10. Something went wrong after that.

 connection.println(F("GET / HTTP/1.1\r\n"));

is probably wrong (has a newline in the print, AND uses println, so you’ll get a double line-ending. But I don’t know if that would cause this problem.)

Ok, I get, so the connection is succefully made to port 10?! I tried that line you said without the “\r\n” on the end, and the problem persists.

zoomkat:
You might try your browser connecting to google and see if those wireshark results show a large port number.

Ok, it does.

SurferTim: Using the F() macro will send the request one character per packet. Maybe the server doesn't like that. Try it without the F() macro and using HTTP/1.0.

// change this
 connection.println(F("GET / HTTP/1.1\r\n"));
// to this
 connection.println("GET / HTTP/1.0\r\n");

Hello SurferTim, thanks for your help.

Tried it again, the same problem still persists. I didn't know about the F() macro, but I tought about the HTTP/1.0 before, it didn't work.

What kind of server is on port 10?

localhost won't work on any device except the server. You must use the local IP of the server.

So, how can I connect to this link from Arduino: "localhost:10/?action=System.Shutdown"?

"localhost" is only known to your computer and not to the arduino or router if you are going thru one. Are you trying to use localhost in the URL in the arduino code?

SurferTim: What kind of server is on port 10?

localhost won't work on any device except the server. You must use the local IP of the server.

I don't know exactly what kind of server it is, as I said it's a web interface from Airytec and I couldn't find any API settings or anything like that.

I've just copied the full link I use on my browser (and that works on my HTML page hosted by the Arduino), and I'm trying to connect to it.

zoomkat: "localhost" is only known to your computer and not to the arduino or router if you are going thru one. Are you trying to use localhost in the URL in the arduino code?

No, no, sorry for the confusion. I know it can't be using "localhost" it was only an example, I'm using my DynDNS IP to connect (since I tried the local IP and it didn't work).

Can you connect to the server with another PC on that localnet using the DynDNS IP? Or the local IP?

edit: Not the server itself. Not a good test.

SurferTim: Can you connect to the server with another PC on that localnet using the DynDNS IP? Or the local IP?

edit: Not the server itself. Not a good test.

Yes, yes, It works normaly, even from my phone, with both.

I'm using my DynDNS IP to connect (since I tried the local IP and it didn't work).

Do you have port forwarding setup on your router for port 10? The DynDNS will convert to your router's external IP address, which may require your router to have port forwarding set up.

zoomkat: Do you have port forwarding setup on your router for port 10? The DynDNS will convert to your router's external IP address, which may require your router to have port forwarding set up.

Yes, the port forwarding is setup and fully working, it was my first test.

So the Arduino never prints the 400 error response? Just "connecting"? Maybe it is time for you to post your entire sketch.

SurferTim: So the Arduino never prints the 400 error response? Just "connecting"? Maybe it is time for you to post your entire sketch.

No, just the connecting, and the "..." I inserted to print after the connection, it acctually don't get any response. That was one of the things I tougth it's strange.

Please, feel free to comment about any other improvements, it will be really welcome.

Does the server use HTTP/1.0 or HTTP/1.1 protocol?