Hi, I'm very new to the MQX RTCS stack, and writing embedded web clients in general, so I would greatly appreciate some guidance on this issue I'm having.
I am using CodeWarrior 10 with MQX3.6, and have the web_hvac demo working fine on a TWR52259 platform. I'm trying to add functionality to post database entries to a web server. To start I created a new shell command ("test") and added the following code that calls socket, bind, connect, and send. This is based on the MQX RTCS User Guide, and the sample code. I couldn't find much else in the forum or elsewhere online. When I call the function from the shell I get error code 0x1704, which is RTCSERR_SOCK_INVALID. The shell output follows the code.
int_32 Shell_test(int_32 argc, char_ptr argv[] ) { boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; uint_32 new_socket; sockaddr_in local_sin, remote_sin; uint_32 result; uint_16 namelen; char buffer[50], resp_buffer[50]; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc > 1) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { printf("Trying to send message to www.google.ca\n"); new_socket = socket(AF_INET, SOCK_STREAM, 0); if (new_socket == RTCS_SOCKET_ERROR) { printf("RTCS_SOCKET_ERROR\n"); return; } else { printf ("Socket opened: %d\n", new_socket); } memset((char *) &local_sin, 0, sizeof(sockaddr_in)); local_sin.sin_family = AF_INET; local_sin.sin_port = 0; local_sin.sin_addr.s_addr = ENET_IPADDR;//INADDR_ANY; result = bind(new_socket, &local_sin, sizeof (sockaddr_in)); if (result != RTCS_OK){ printf("Error, bind() failed with error code %lx\n", result); return; } printf ("Socket bound...\n"); namelen = sizeof (sockaddr_in); result = getsockname(new_socket, &local_sin, &namelen); if (result != RTCS_OK) { printf("Error, getsockname() failed with error code %lx\n", result); } else { printf("Local address family is %x\n", local_sin.sin_family); printf("Local port is %d\n", local_sin.sin_port); printf("Local IP address is %lx\n", local_sin.sin_addr.s_addr); } memset((char *) &remote_sin, 0, sizeof(sockaddr_in)); remote_sin.sin_family = AF_INET; remote_sin.sin_port = 80; remote_sin.sin_addr.s_addr = 0xADC22168; //173.194.33.104 (google.ca) result = connect(new_socket, &remote_sin, sizeof (sockaddr_in)); if (result != RTCS_OK){ printf("Error, connect() failed with error code %lx\n", result); return; } printf("Connected to %lx, port %d.\n", remote_sin.sin_addr.s_addr, remote_sin.sin_port); strcpy (buffer, "GET index.html HTTP/1.0\n\n"); printf ("Outgoing message len: %d, string: %s\n", strlen(buffer) + 1, buffer); result = send(new_socket, buffer, strlen(buffer) + 1, 0); if (result != RTCS_OK){ printf("Error, send() failed with error code %lx\n", RTCS_geterror(result)); shutdown(new_socket, FLAG_CLOSE_TX); return; } printf("Sent %d bytes.\n", result); result = recv(new_socket, resp_buffer, 50, 0); if (result == RTCS_ERROR){ printf("Error, recv() failed with error code %lx\n", RTCS_geterror(new_socket)); } else { printf("Received %ld bytes of data.\n%s\n", result, buffer); } } } if (print_usage) { if (shorthelp) { printf("%s \n", argv[0]); } else { printf("Usage: %s \n", argv[0]); } } return return_code;}
shell> testTrying to send message to www.google.caSocket opened: 536880972Socket bound...Local address family is 1Local port is 5000Local IP address is a9fe0303Connected to adc22168, port 80.Outgoing message len: 26, string: GET index.html HTTP/1.0Error, send() failed with error code 1704shell>
Does anyone see what I'm doing wrong here?
Regards,
Angelo
Hi,
problem is with send() because return value are 1) Number of bytes sent 2) RTCS_ERROR
and the second problem is RTCS_geterror have parameter socket [in] — Socket handle , in your example is it new_socket .
result = send(new_socket,buffer, strlen(buffer) + 1, 0); if (result == RTCS_ERROR){ printf("Error, send() failed with error code %lx\n", RTCS_geterror(new_socket)); shutdown(new_socket, FLAG_CLOSE_TX); return 1; }