Pages: [1]   Go Down
Author Topic: How can i make this socket connection works as is on Arduino  (Read 619 times)
0 Members and 1 Guest are viewing this topic.
ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  

Code:
#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
    ...
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to use the Arduino Ethernet library

An example of connecting to a web server can be found here: http://arduino.cc/en/Reference/EthernetClient
Logged

ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I did a few test and it works. That was easy indeed.  smiley-eek-blue

You need to use the Arduino Ethernet library

An example of connecting to a web server can be found here: http://arduino.cc/en/Reference/EthernetClient
Logged

Pages: [1]   Go Up
Jump to: