How can i make this socket connection works as is on Arduino

Hi,
I use the regular Arduino Ethernet shield
I’m trying to use this ~exact code in my arduino project.
It’s C, must be easy to do.

I’m not very good with Socket programming already.

Thanks for the help.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>

int conn;
char sbuf[512];

int main() {
  char *host = "some.domain";
  char *port = "8080";

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  getaddrinfo(host, port, &hints, &res);
  conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  connect(conn, res->ai_addr, res->ai_addrlen);

  while ((sl = read(conn, sbuf, 512))) {
    ...
    ... do somthing here
    ...
  }
}

I'm not quite sure what you are trying to accomplish.

Do you want to connect to the Arduino using a C program from your computer?
Or do you want to program the Arduino with Ethernet capabilities using the shield?

You don't get a standard sockets API with the ethernet shield. Try one of the examples that come with the library instead.

It's Arduino Ethernet Programming, the socket connection process is my problem.
The While loop i can handle what is going to be inside it.
In fact i just have to read from a server.

TheCodeMonkey:
I'm not quite sure what you are trying to accomplish.

Do you want to connect to the Arduino using a C program from your computer?
Or do you want to program the Arduino with Ethernet capabilities using the shield?

You need to use the Arduino Ethernet library

An example of connecting to a web server can be found here: Ethernet - Arduino Reference

Yes, I did a few test and it works. That was easy indeed. :fearful:

TheCodeMonkey:
You need to use the Arduino Ethernet library

An example of connecting to a web server can be found here: Ethernet - Arduino Reference