Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
12 years, 4 months ago.
problem3 with example program: TCPSocket_Hello world
First of all this is the entire example:
#include "mbed.h" #include "EthernetInterface.h" int main() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(75000); printf("IP Address is %s\n", eth.getIPAddress()); TCPSocketConnection sock; sock.connect("mbed.org", 80); //connect socket to host char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; sock.send_all(http_cmd, sizeof(http_cmd)-1); char buffer[300]; int ret; while (true) { ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) break; buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer); } sock.close(); eth.disconnect(); while(1) {} }
Im trying to understand the code in this examle:
TCPSocketConnection sock; sock.connect("mbed.org", 80);
this means: the mbed makes a socket connected to his own port nr 80??? and makes a connection to the mbed.org website???
char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
this means i put in char http_cmd[] what i get from connecting to the mbed.org/media/.....???
sock.send_all(http_cmd, sizeof(http_cmd)-1);
this means when someone askes data from the mbed server i send whats in http_cmd???
char buffer[300]; int ret; while (true) { ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) break; buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer); }
i dont understand what this does, in hyperterminal i get a lot of information about the server and the connection. but how does sock.receive works??? how do i add event handlers to this program?
I found on other example in the cookbook, working with the old libraries. HTTPServerHelloWorld:
#include "mbed.h" #include "EthernetNetIf.h" #include "HTTPServer.h" EthernetNetIf eth; HTTPServer svr; DigitalOut led1(LED1); int main() { printf("\nSetting up...\n"); EthernetErr ethErr = eth.setup(75000); if(ethErr) { printf("\nError %d in setup.\n", ethErr); return -1; } printf("\nSetup OK\n"); svr.addHandler<SimpleHandler>("/hallo"); //Default handler svr.bind(80); printf("\nListening...\n"); Timer tm; tm.start(); //Listen indefinitely while(true) { Net::poll(); if(tm.read()>.5) { led1=!led1; //Show that we are alive tm.start(); } } return 0; }
Here i can add a handler, so if i go in my browser to the ip adres the dhcp server has given the mbed, i can see the simple handler (which is an hallo world page). If i load the first program in the mbed and go in my browser to the ip adres the dhcp server has given the mbed, he doesnt find him. how can i add event handlers to the first example? Ok this is a really long explanation, there are a lot of questions in it, i would already be thankfull if you can just answer some. thnx in advance greetings adriaan
2 Answers
12 years, 4 months ago.
Hi again adriaan, ok lets begin,
The first program open a connection to the mbed site in the port 80 (normally this port is associated with HTTP protocol) and with the GET instruction is asking for a text file called hello.txt, then with a while you are receiving all the characters in this file until there were no more left putting then in a char buffer
if you see the api documentation this is what does the receive function:
- int receive (char *data, int length)
- Receive data from the remote host.
normally the length is always size of the buffer - 1 because you have to let and space for the finish char ('\0')
i think that there is an error in the code, this two lines should be outside of the while because the break instruction will not let this lines execute never:
buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer);
so the code is you want to see the content of the file should be:
while (true) { ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) break; } buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer);
If hope this will help you, you can find all the api references in here:
http://mbed.org/handbook/Ethernet-Interface
Greetings
12 years, 4 months ago.
Hi Adriaan. I assume to want to write a HTTP Server. There is an Example (search for HTTP) HTTPDcgi from Greg Steiert using the new TCPIP Interface or you may have a look at my code.
*************** HTTP Server Task //
- include "main.h"
- include "Server.h"
extern PROGRM *prg; extern EthernetInterface eth; char netrxbfr[NETBFRSZ]; char nettxbfr[NETBFRSZ];
void Server(const void *arg) { REQUEST request; int bytcnt; int retsts;
printf("HTTP Server started\r\n");
TCPSocketServer server; Create new socket if(server.bind(HTTP_SERVER_PORT) < 0 ) Bind port to socket { printf("sock.bind() has failed\r\n"); server.close(); eth.disconnect(); osThreadTerminate(prg->srvThreadId); return; ?????? }
if(server.listen(1) < 0 ) Listen for connect request { printf("sock.listen() has failed\r\n"); server.close(); eth.disconnect(); osThreadTerminate(prg->srvThreadId); return; ??????? }
while(TRUE) Server restart loop { TCPSocketConnection sock; Get a socket for client connaction while(server.accept(sock) < 0); Wait for a connect request printf("Connection accepted from %s\r\n", sock.get_address());
In the follwing loop read data from HTTP Client and parse it
while(TRUE) Server Message processing loop { if((bytcnt = sock.receive(netrxbfr, NETBFRSZ)) <= 0) { printf("sock.recv() has timed out\r\n"); sock.close(); break; } netrxbfr[bytcnt] = '\0';
if(ParseRequest(netrxbfr, bytcnt, &request) == FALSE) { printf("Server:ParseRequest() Invalid HTTP Request\r\n"); sock.close(); break; }
switch(request.reqType) Dispatch type of request(Get, Post, ...) { case tGet: retsts = ProcessGetRequest(&sock, &request); break; case tPost: retsts = ProcessPostRequest(&sock, &request); break; case tHead: default: HTMLSendNotImplemented(&sock); retsts = FALSE; break; }End switch
if(retsts == FALSE || request.dispos == FALSE) { printf("Server() Invalid HTTP Request, closing socket\r\n"); sock.close(); break;
} } end while printf("Socket closed\r\n"); } end while loop }
************** HTTP Server definitions file
- ifndef SERVER_H_
- define SERVER_H_
- include "main.h"
- define tUnknown 0 Unknown request
- define tGet 1 Get Request
- define tPost 2 Post request
- define tHead 3 Head request
typedef struct request_tag { char *pURL; int szURL; char *pAuthorization; int szAuthorization; char *pFirstCookie; int szFirstCookie; char *pData; int szData; int rx_length; IPADDR client_ipaddr; int reqType; int dispos; } REQUEST;
*****************
Because this code runs as a separate thread, simply comment out the calls to osThreadTerminate.
Unfortunately there are two TCPIP libraries, an old TCPIP Interface and a new one. The new Interface uses the classes: EthernetInterface, TCPSocketConnection, TCPSocketServer, UDSocket and EndPoint. For new program development I would recommend to use the new TCP Interface classes. The documentation on the new Interface is very poor, but if you search for the above class names, you should find the Information you need. I hope this helps. Guenter.