I' m using arduino uno + ethernet shiled(Wiznet w5100) chip. I want to send data from pc configured as client to arduino uno + ethernet shield as server.
here is my code
#include<SPI.h>
#include<Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //declare mac address of Uno + Ethernet shield
IPAddress ip (192,168,0,102); //declare ip address of Uno + Ethernet shield
IPAddress gateway(192,168,0,252);
IPAddress subnet(255,255,255,0);
EthernetServer server = EthernetServer(502); //declare server port
void setup()
{ // put your setup code here, to run once:
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet); //Initializes ethernet library and network settings
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{ // put your main code here, to run repeatedly:
EthernetClient client = server.available(); // wait for a new client:
if(client) // client requested connection
{
Serial.println("New Client");
while(client.connected()) //Checks Whether or not the client is connected.
{ //if connected
if(client.available()) //Returns the number of bytes available for reading
{
byte c = client.read(); // client transmitted something
Serial.write(c); //read characters incoming from the client:
}
}
}
delay(1000);
}
For setup portion of code, m getting the result as shown in setup image attachment.
To send data from pc m using HERCULES setup utility. it gives me some error whose image is attached.
Thanks for the reply.
i tried web browser but the result is same. As u have mentioned Ethernet.begin(mac, ip, gateway, gateway, subnet); in which 1 is Default gateway, so for 2 gateway should i enter the DNS server gateway??
which is same as default gateway, so i m bit confused....
Also my code doesn't move ahead after this
void loop()
{ // put your main code here, to run repeatedly:
EthernetClient client = server.available();
code get stucks here
that means the client is not available to server and the reason for this is unkown
Yes my pc is on same localnet with ip adderss of 192.168.0.21. Also ip 192.168.0.102 is dedicated to arduino board only. i tried web browser too... but no result.
So finally i decided to make a physical connection between server(arduino) and client pc through ethernet cable and used the code as it is which i mentioned in my first query, and i got the required result.
My half of the problem is solved, i will tell u later why i m saying half of the problem. Right now whenever i m trying to send data from client(i.e pc) to arduino as server, it successfully receives. That means server is able to listen the client via port(and not through physical cable).
Here is the working code
#include<SPI.h>
#include<Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //declare mac address of Uno + Ethernet shield
IPAddress ip (192,168,0,102); //declare ip address of Uno + Ethernet shield
IPAddress gateway(192,168,0,252);
IPAddress subnet(255,255,255,0);
EthernetServer server(502); //declare server port
byte num, val;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet); //Initializes ethernet library and network settings
server.begin();
server.write("server is at ");
server.write(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available(); // wait for a new client:
if(client) // client requested connection
{
server.write("New Client");
while(client.connected()) //Checks Whether or not the client is connected.
{ //if connected
if(client.available()> 0) //Returns the number of bytes available for reading
{
byte data = client.read(); // client transmitted something
server.write(data); //echo back the data from server to client:
}
}
}
delay(1000);
}
The problem is with serial.print(), as soon as i remove it (compare with old code in this forum), code starts working.
Now coming to my other half problem, refer to attached image. Here it goes client gets connected to the server for first time, exchanging of data also goes well as u can see in image after that when i disconnect the client it shows Connection closed but when i connect it again
it shows this error TCP connection timeout.
So my question is it possible to detect if a client is still connected to the server or disconnected from the server?
hey surfer tim,
You can check my 2nd post in this forum where i had mentioned that 1st is the default gateway then what 2nd gateway i should use?? DNS server gateway?? if yes then it is same as default gateway, so i m bit confused..... Also as u said server reads the request but does not respond and closes the connection so what response server(arduino) should give ?? and how server will detect that client is disconnected.
You are missing the dns server parameter. I use the gateway as the dns server.
I don't use Hercules to test my ethernet stuff. I use PuTTY.
edit: Read reply #6 again. Your original Arduino server code doesn't send a response or close the connection. If you get nothing, then you are getting exactly what it is sending. Nothing.