Hi,
I'm trying to do an Http get request to google, but once I have sent the request I do not receive any answer from google. I'm using MXQ 4.0 on TWR-K53N512. My code is the next one:
#define HTTP_GOOGLE_COM "GET / HTTP/1.0\r\n\r\n"
#define IP_ADDRESS_GOOGLE_COM IPADDR(173,194,34,244)
void SocketClientTask(uint_32 PrivateData) {
uint_32 Client, Error;
int_32 BytesToProcess, BytesSent;
sockaddr ClientAddress, ServerAddress;
uint_16 ServerAddressLength;
char RxBuffer[100];
uint_32 SocketOption = FALSE;
uint_32 SocketOptionLength;
Client = socket(PF_INET, SOCK_STREAM, 0);
memset((char *) &ClientAddress, 0, sizeof(ClientAddress));
ClientAddress.sin_addr.s_addr = IPADDR(10,0,0,206);// "This is my IP"
ClientAddress.sin_family = AF_INET;
ClientAddress.sin_port = SERVER_PORT + 1;
SocketOptionLength = sizeof(SocketOption);
Error = setsockopt(Client, SOL_TCP, OPT_RECEIVE_NOWAIT, &SocketOption, SocketOptionLength);
if(Error != RTCS_OK) {
printf("setsockopt() failed with error %lx” --> Client Exiting\n", Error);
return;
}
Error = bind(Client, &ClientAddress, sizeof(ClientAddress));
if(Error != RTCS_OK){
printf("Failed to bind Client, error %lx\n", Error);
return;
}
memset((char *) &ServerAddress, 0, sizeof(ServerAddress));
ServerAddress.sin_addr.s_addr = IP_ADDRESS_GOOGLE_COM;
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = 80;
ServerAddressLength = sizeof(ServerAddress);
Error = connect(Client, &ServerAddress, ServerAddressLength);
if(Error != RTCS_OK){
printf("connect to server failed with error code %lx\n", RTCS_geterror(Client));
return;
}
printf("Connected\n");
memset(RxBuffer, 0, sizeof(RxBuffer));
sprintf(RxBuffer, "%s", HTTP_GET_GOOGLE_COM);
BytesSent = send(Client, RxBuffer,strlen(RxBuffer), RTCS_MSG_BLOCK);
printf("HTTP_GET_GOOGLE_COM sent %ld bytes\n", BytesSent);
while(1) {
BytesToProcess = recv(Client, RxBuffer, sizeof(RxBuffer), RTCS_MSG_PEEK);
if(BytesToProcess > 0) {
recv(Client, RxBuffer, BytesToProcess, 0);
while(BytesToProcess > 0) {
printf("%c", RxBuffer[BytesToProcess]);
BytesToProcess--;
}
memset(RxBuffer, 0, sizeof(RxBuffer));
}
else if(BytesToProcess == RTCS_ERROR) {
printf("ERROR!!!!\n");
return;
}
}
}
What I'm doing wrong?
Thanks in advance.
Óscar.
Solved! Go to Solution.
Hi Oscar,
I believe you are missing part of the header. I can only see the GET string in your code. Take a look to the appnote http://cache.freescale.com/files/microcontrollers/doc/app_note/AN4417.pdf I believe you will find educative in the web client topic.
Hope this helps.
Regards,
Garabo
Hi Garabo,
many thanks for your help.
Best regards,
Óscar.
My pleasure!!!
Hi Oscar,
I believe you are missing part of the header. I can only see the GET string in your code. Take a look to the appnote http://cache.freescale.com/files/microcontrollers/doc/app_note/AN4417.pdf I believe you will find educative in the web client topic.
Hope this helps.
Regards,
Garabo