How to connect Ethernet Shield as client with PC, with router in between?

Hello!! Everyone!

Earlier i had sent data from PC, to Arduino using Ethernet, so i used a Arduino Mega and Ethernet Shield for this purpose, i connected Ardiuino Mega + Ethernet Shield directly with my PC and changed the IP address of my PC as per the server address set in the following sketch, and then used HW Hercules software to communicate with Arduino with PC over Ethernet.

#include <SPI.h>
#include <Ethernet.h>

#define ETH_CS    10
#define SD_CS  4

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xB0,0xCD,0xAE,0x0F,0xDE,0x10};

IPAddress ip(192,168,1,17);

IPAddress server(192,168,1,10); 

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting Ethernet Connection");
  pinMode(ETH_CS,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  digitalWrite(ETH_CS,LOW);		// Select the Ethernet Module.
  digitalWrite(SD_CS,HIGH);		// De-Select the internal SD Card
  if (Ethernet.begin(mac) == 0)  // Start in DHCP Mode
  {
    Serial.println("Failed to configure Ethernet using DHCP, using Static Mode");
    // If DHCP Mode failed, start in Static Mode
    Ethernet.begin(mac, ip);
  }
  Serial.print("My IP address: ");
  
  for (byte thisByte = 0; thisByte < 4; thisByte++)
  {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 5000))
  {
    Serial.println("connected");
  } 
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    client.write(c);
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();
    if (client.connected())
    {
      client.print(inChar); 
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

But Now the case is little bit different, my Arduino+Ethernet Shield is connected with my PC directly, but there is a router in between it.
How can i receive the data from Arduino Mega on PC and Vice Versa with router in between, my setup is as follow:

I'm triyng to do something like that, but I didn't jet.

What I understood is that router must be configured to forward ethernet shield used ports, to the server (pc) waiting with same port opened.

you're connecting to port 5000, so you have to setup router to forward request on that port to the server IP.

Yeah, i to think that router must be configured to route the messages from that IP to my PC, but don't know how to do this.
Hope someone can help us.

What do you mean by "router in between"? Do you mean they are both connected to the same router, or each connected to its own router?

Like this?
Arduino -- router -- PC

Or this?
Arduino -- router -- internet -- router -- PC

No both are connected to same router and router is connecting my pc with external network

I think i am not able to explain my problem properly.

My PC is connected to a router, and this router is having one input port and 5 output port + Wifi, my PC is connected one one of the output ports.
I had to connect the Ethernet Shield to the router and wants to receive the data coming from Ethernet Shield to PC and from PC to Ethernet Shield

The IP addresses you are using on the PC and Arduino are within the router's localnet range?

The IP's assigned automatically to Arduiono Ethernet Shield and PC using DHCP.
So i think as it is assigned automatically by router using its DHCP server, then it must falls in its range.

Are you using the IP assigned to the PC as the server IP in the Arduino sketch?

The most common problem is the PC's firewall. Insure you are allowing the port (5000?) through the firewall.

Initially i had set it to detect automatically and then after that, i had set as per my arduino sketch.

I want to that, suppose arduino is trying to send data to particular IP and a router is in between, do router has the capability to route that message to its destination.

I don't know what i can do, to get it working.
I am confused by seeing these network issues.

First of all you have to check this settings:

  • pc firewall unactivated (best way).
  • set PC lan-IP static (in PC network configuration panel)

then you have to configure router, for sure you have to:

  • assign static IP to PC (as defined in the pc configuration panel).
  • assign static IP to arduino (ethernet or wifi shield).
  • assign port-forwarding to PC-IP for port 5000. (*)

(*) for port-forwarding, you should find something a proper page in router setting panel.
The port-forwarding ask an IP, then a port (opened on that ip) to whom forward request by every other client in the lan.

I had some problems, but I'm quite sure that should be work.

Hi!! Thanks to everyone.

It just started working, let me share what i had done.

First of all i open the command prompt and by typing the command ipconfig, i noted my IP address of PC, which is assigned by my Router.
It is as follow:
Ip->192.168.0.102
Subnet->255.255.255.0
Gateway->192.168.0.1
Then i checked the IP address assigned to my Ethernet Shield, by Router, it is 192.168.0.101

I did nothing after this, i just opened my Sketch and in place of Server IP address i write 192.168.0.102, which is IP address assigned to my PC by Router.

Then upload the sketch and just opened the 5000 port in HW Hercules software, within a second i got a message 192.168.0.101 connected and now i am able to successfully send and receive data from my PC to Ethernet Shield and from Ethernet Shield to PC both are connected to router.

I also checked ping 192.168.0.101 -t from command line and i get proper response for this.

With this setup, i am able to use internet on PC, as well as receive Messages from Ethernet Shield.
But as IP has been assigned dynamically to PC, it can change at any time or in the next cycle and no one will check it again and again, and re-upload the Server IP address in Arduino every-time.
So i did one more test, i noted down the Ip assigned to PC and set that manually in Network Configuration.
By doing this, my PC has static Ip address.
Now Ethernet Shield and my PC are working properly, but internet on PC is not working.
Though its not an issue, i just want to get Ethernet Shield and PC communicating with each other, both connected to router.

Your problem is that router give dinamical IP, and everytime you connect some client to router (pc, arduino, phone..) you receive the first IP free (102, 103, ..)

I didn't understand how you:

i noted down the Ip assigned to PC and set that manually in Network Configuration.
By doing this, my PC has static Ip address.

but I think you have to go on the setup page o your router, typically is: http://192.168.1.1

In the setup menù, you'll find a section like "static IP" or "static adresses"
In that section you'll see connected client (Arduino, Pc, ..) and their relative MAC adress and dinamic IPs.

You have to choose a static IP to assign for the Pc (server), in that way, the router will reserve that ip adress only for the server (when connected).

the last thing you have to do is to search, in the configuration page of the router (http://192.168.1.1), the "forwarding section" and assign the port 5000 as forwarded to pc (relative IP)

So, every client will look for : 192.168.1."server":5000, will be connected to the pc (pay attection to firewalls on pc and router, giving right exclusion to port, elsewhere they will block the queries).

Last thing..
I'm not completely sure, but you shoud do an error using 192.168.0.xxx instead of 192.168.1.xxx.
typically the router setup page is on 192.168.1.1, try this adress and,
if you'll find correctly the setup page, you'll be certain about your lan is 192.168.1 and not 192.168.0 (as you're doing).

xpress_embedo:
Hi!! Thanks to everyone.

It just started working, let me share what i had done.

First of all i open the command prompt and by typing the command ipconfig, i noted my IP address of PC, which is assigned by my Router.
It is as follow:
Ip->192.168.0.102
Subnet->255.255.255.0
Gateway->192.168.0.1
Then i checked the IP address assigned to my Ethernet Shield, by Router, it is 192.168.0.101

I did nothing after this, i just opened my Sketch and in place of Server IP address i write 192.168.0.102, which is IP address assigned to my PC by Router.

Then upload the sketch and just opened the 5000 port in HW Hercules software, within a second i got a message 192.168.0.101 connected and now i am able to successfully send and receive data from my PC to Ethernet Shield and from Ethernet Shield to PC both are connected to router.

I also checked ping 192.168.0.101 -t from command line and i get proper response for this.

With this setup, i am able to use internet on PC, as well as receive Messages from Ethernet Shield.
But as IP has been assigned dynamically to PC, it can change at any time or in the next cycle and no one will check it again and again, and re-upload the Server IP address in Arduino every-time.
So i did one more test, i noted down the Ip assigned to PC and set that manually in Network Configuration.
By doing this, my PC has static Ip address.
Now Ethernet Shield and my PC are working properly, but internet on PC is not working.
Though its not an issue, i just want to get Ethernet Shield and PC communicating with each other, both connected to router.

I connected ethernet shield to router but router's configuration page do not display any MAC and IP assigned to ethernet shield, in short ethernet shield is not getting connected to router.
I have D-Link router and I used 'Dhcpaddressprinter' code from in built ethernet library but no DHCP is assigned to arduino, please help!

I highly recommend using a static IP assignment outside the dhcp range of the router. Take a look in your router DHCP setup, and see what range the dhcp server is using. Use an IP outside that. As I recall, D-Link uses 192.168.0.100 to 192.168.0.199 for its DHCP IPs. Don't use those.