Home/Support/Support Forum/Calls to accept() cause a halt/crash
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Calls to accept() cause a halt/crash

0 votes
I have a simple test application that opens a TCP port, and then calls accept(). The problem is that EVERY time accept is called, the board halts/crashes. It appears to be in the tfSocketCheckAddrLenLock function, but I'm not 100% certain about that.

Here's the stack output from the call stack:
6 tfAccept() ..\..\src\treck\source\sockapi\traccept.c:153 0x0001d044

Here's the functions I'm using:
SOCKET create_new_socket()
{
SOCKET s;
struct sockaddr_in addr;
unsigned short port=502;
int ret;
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1) {
return s;
}

//rc=setsocketoptions(&s);
//if(rc==-1)
// goto newsock;

memset(&addr, 0, sizeof(addr));
addr.sin_family = PF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if (ret == -1) {
errno = getErrno();
closesocket(s);
s=-1;
}

if (listen(s, 5) == -1) {
closesocket(s);
s=-1;
}

return s;
}

SOCKET our_accept(SOCKET s)
{
struct sockaddr_in addr;
socklen_t addrlen;
static int connectioncount=0;
SOCKET newsock;

memset(&addr, 0, sizeof(addr));
addr.sin_family=PF_INET;
addrlen = sizeof(addr);
newsock= accept(s, (struct sockaddr *)&addr, (int*)&addrlen); //halts here!!!
if (newsock == -1) {
return -1;
}

connectioncount++;
printf("The client connection from %s is accepted (# of connections=%d, available heap=%d)\n",
inet_ntoa(addr.sin_addr),connectioncount,NATcpipGetCurrentHeapUsage());

return newsock;
}

Then I have a thread that calls "our_accept()":
static void jobs_thread_func(ULONG arg)
{
static uint8_t query[260];
int rc=0;
SOCKET s=(SOCKET)arg;
SOCKET clisock;
struct sockaddr_in fromaddr;
int err;
socklen_t addrlen;

wait:
addrlen=sizeof(fromaddr);
clisock=our_accept(s);
if(clisock<0)
{
err=getErrno();
if(err==EWOULDBLOCK)
{
tx_thread_sleep(NS_MILLISECONDS_TO_TICKS(10));
goto wait;
}
}
while(1)
{
memset(&query[0],0x00,260);
rc = recv(clisock, (char*)query,260,0);
if(rc==0)
{
deadconn:
closesocket(clisock);
//addr.sin_addr.s_addr = INADDR_ANY;
printf("Opening a new TCP socket, heap usage is %d\n",NATcpipGetCurrentHeapUsage());
goto wait;
}
if(rc<0)
{
if(rc==-ECONNRESET)
{
goto deadconn;

}
if(errno==0)
{
tx_thread_sleep(NS_MILLISECONDS_TO_TICKS(10));
goto wait;
}
else
{
goto deadconn;
}
}
}
}

int threadStartup(SOCKET s)
{
int ccode;
TX_THREAD jobs_thread = { 0 };
unsigned char *jobs_stack = NULL;

jobs_stack = malloc(8192);
ccode = tx_thread_create(&jobs_thread, "Jobs", jobs_thread_func, (ULONG)s,
(void*) jobs_stack, 8192, APP_DEFAULT_API_PRIORITY,
APP_DEFAULT_API_PRIORITY,1, TX_AUTO_START);
if (ccode != TX_SUCCESS) {
printf("Unable to allocate Jobs thread stack.");
return ccode;
}
return ccode;
}

And, finally:
void applicationStart (void)
{
SOCKET s;
/* Initialize the system services for the application. */
initAppServices();

/*
* Code to start the user application goes here.
*/
s=create_new_socket();
threadStartup(s);
printf ("Hello World. %s Ready\n", APP_DIALOG_APP_NAME);
tx_thread_suspend(tx_thread_identify());
}

I've tried this outside of the thread, and the result is the same. I've created several projects from scratch thinking that I had an option wrong, but the results are the same.

Can someone tell me what might be wrong?

Thanks in advance.
asked Jul 27, 2016 in NET+OS by cbrumley New to the Community (5 points)

Please log in or register to answer this question.

...