Connecting pc client to Arduino Ethernet server

Hi all,

I’m programming a tcp/ip pc/arduino project. The Arduino has a ethernetshield and serves as client. The PC runs boost and makes use of the asio library and serves as client.

When i try to connect to the server the connection can not be establised. The server has a network adapter that has the static aderes of 192.168.1.1. And the Arduino has IP adress of 192.168.1.2. The two are directly connected with a UTP cable. Both are using port 5000.

When i ping the arduino from my pc, then that works fine.

For the Arduino code i use a sample program to for testing but this fails. The setup looks like this:

// Enter the IP address of the server you’re connecting to:
IPAddress server(192,168,1,1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you’re using Processing’s ChatServer, use port 10002):
EthernetClient client;

void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// 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”);
}
}

The PC server code is also fairly simple, in a class constructor i do the following:

cout << “Setting up server” << endl;
// Protocol and port
boost::asio::ip::tcp::endpoint Endpoint(boost::asio::ip::tcp::v4(), 5000);

// Create acceptor
boost::asio::ip::tcp::acceptor Acceptor(IOService, Endpoint);

// Create socket
SmartSocket Sock(new boost::asio::ip::tcp::socket(IOService));

cout << “Before accept…” << endl;

// Waiting for client
Acceptor.accept(*Sock);

cout << “Server set up” << endl;

SmartSocket is a typdef:

typedef boost::shared_ptrboost::asio::ip::tcp::socket SmartSocket;

I i start the server the console prints “Before Accept” and waits in the accept function for a incomming client. But when i run my Arduino code then i get connection failed (in the ardunion serial monitor).

Does anybody have some idea’s what is going wrong? It seems that the server and client don’t see each other. I also put down my firewall but this didn’t help. Any comments are usefull!

Datoeter:
The two are directly connected with a UTP cable.

That's unusual. I assume you mean a CAT5 or equivalent Ethernet cable. Perhaps your PC's NIC is auto-sensing the polarity, but normally you'd go through a hub or switch which would cross the connection over for you. Do you have access to a hub/switch/router?

Basic client test code you can try to check your network connection. Also, read the “how to use this forum” sticky to see how to post code using the # in the tool bar.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Thanks for the comments!

Next time i will format the code accordingly. I have connected the Adruino to my router and used the client test program and that works! (Thanks for that). So i starting to think that my server code on the pc is wrong. I programmed a client (for PC) with it also using boost libraries and that works fine. The connection is accepted. Has anybody used this boost to make a client/server connection with the arduino? Or some other api? Like the winsock api?

I assumed that when using IPv4 that as long as the api supports te protocol then it should be possible to connect to the Arduino ethernetshield.

Does anybody have an idea?

Thanks all,

i have fixed the problem, turn out that my firewall of my router was still on.

Many Thanks for the comments.