Hi all,
I am trying to create a project with a reliable tcp connection using the lwip 1.3.2 with safertos. I found some sample code online and updated it a bit to check for additional errors and port utilization however i am having a few issues, has anyone run into something similar and were you able to work around it:
1) If i deliberately go to an address that doesn't exist (bad IP or server down...) It looks like my hc_error function never gets called. Additionally after I call tcp_connect my hc_poll function is never called either so i am unable to detect the error case and abort the connection.
2) I have to constantly increment my port number for each connection i create for some reason when hitting the IIS server the original connection is never properly closed even though I am calling tcp_close. ( Please note that i had to switch using to tcp_bind(pcb, IP_ADDR_ANY, 0); instead of specifying a single port number. )
Here is the code i am using to open up a connection:
int hc_open(struct ip_addr* remoteIP, char *Page, char *PostVars, void (*returnpage)(u8_t, hc_errormsg, char *, u16_t, void*), void* arg) { struct tcp_pcb *pcb = NULL; struct hc_state *state; static u8_t num = 0; // local port //u16_t port = 4545;
// Get a place for a new webclient state in the memory state = malloc(sizeof(struct hc_state));
// Create a new PCB (PROTOCOL CONTROL BLOCK) pcb = tcp_new(); if (pcb == NULL || state == NULL) { //UARTprintf("hc_open: Not enough memory for pcb or state\n"); //Not enough memory return 0; }
// Define webclient state vars num++; state->Num = num; state->RecvData = NULL; state->ConnectionTimeout = 0; state->Len = 0; state->ReturnPage = returnpage; state->CallingClass = arg;
// Make place for PostVars & Page if (PostVars != NULL) state->PostVars = malloc(strlen(PostVars) + 1); state->Page = malloc(strlen(Page) + 1);
// Check for "out of memory" if (state->Page == NULL || (state->PostVars == NULL && PostVars != NULL)) { free(state->Page); free(state->PostVars); free(state); tcp_close(pcb); return 0; } // Place allocated copy data strcpy(state->Page, Page); if (PostVars != NULL) strcpy(state->PostVars, PostVars); else state->PostVars = 0;
// Bind to local IP & local port tcp_bind(pcb, IP_ADDR_ANY, 0);
// Use conn -> argument(s) tcp_arg(pcb, state);
// Setup the TCP error function tcp_err(pcb, hc_error);
tcp_poll(pcb, hc_poll, 10);
// Open connect (SEND SYN) err_t conResult = tcp_connect(pcb, remoteIP, 80, hc_connected);
if (conResult != ERR_OK) return -1;
return num;}
Efe,
You may want to cast a wider net in your search for lwIP answers by asking a question or searching through the user mailing lists at http://savannah.nongnu.org/mail/?group=lwip. Your questions look to be high level enough to be hardware independent.