Accept() error when spawning a task

I am trying to spawn a task to listen for socket connections which will then spawn a task to receive from the connection.

The problem I am having is that when I spawn the task the accept() returns an error of ENETDOWN (50) saying that the network is down. However, if instead of spawning the listening task I simply call the function the accept() returns fine.

Here is the code...

//Spawning a task
int main()
{
taskspawn("listenTask", ListenForConncetion, 100, 0);
Time t = {9999, 0};
Sleep(&t);
Exit(0);
}

void ListenForConnection(void)
{
bind(listenSocket, (sockaddr *)&listenSocketInfo, sizeof(listenSocketInfo));
.....

listenSocket = socket(AF_INET, SOCK_STREAM, 0);

.....

while(1)
{
acceptSocket = accept(listenSocket, (struct sockaddr *)&acceptSocketInfo,
&acceptSocketSize);

if(acceptSocket < 0)
{
printf("Error: accept() failed with return value %d.\n", acceptSocket);
printf("Error: #%d (%s)\n", errno, strerror(errno));
}
else
{
printf("Accepted Connection!!\n");
}
}

If I replace the taskSpawn(...) with just a function call to ListenForConnection() I do not get an error.

Thanks in advance!!

[1293 byte] By [hunie10] at [2007-12-23]
# 1
"taskSpawn", are you using VxWorks?
PeterRitchie at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...
# 2
I am using INTEGRITY and an in-house API for spawning tasks.
I posted with the taskSpawn function in place of the API call because it does the same thing.

Do you think it is a problem with the way the task is spawned?

hunie10 at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...
# 3

It's impossible to say without knowing what spawnTask is doing. Is the thread that calls spawnTask still running?

It doesn't help that your example code won't work, you're creating the socket after the bind. Assuming listenSocket has a valid socket at the start of ListenForConnection, the bind and the accept would be dealing with two different sockets--which could operate differently if ListForConnection is run in a thread compared to not run in a thread. But, again, it's hard to tell without more of the code and what spawnTask is actually doing.

PeterRitchie at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...
# 4
Here is the complete ListenForConnection function...

void ListenForConnection(void*)
{
int listenSocket;
int acceptSocket;
int response;
int acceptSocketSize;
char taskname [20];
const int listenBacklog = 10; // defines the maximum length to which the queue of pending connections may grow
struct sockaddr_in listenSocketInfo;
struct sockaddr_in acceptSocketInfo;

cout << "*** Begin TCP/IP test. ***" << endl;

/* create internetwork socket stream */
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if(listenSocket < 0)
{
printf("Error: socket() failed with return value %d.\n", errno, listenSocket);
printf("Error: #%d (%s)\n", errno, strerror(errno));
}

/* fill struct with zeros */
memset(&listenSocketInfo, 0, sizeof(listenSocketInfo));
memset(&acceptSocketInfo, 0, sizeof(acceptSocketInfo));

/* set socket info */
listenSocketInfo.sin_family = AF_INET; // internetwork address family (TCP, UDP, ..)
listenSocketInfo.sin_addr.s_addr = htonl(INADDR_ANY); // any internet address
listenSocketInfo.sin_port = htons(1500); // set port

/* bind socket info with socket */
response = bind(listenSocket, (sockaddr *)&listenSocketInfo, sizeof(listenSocketInfo));
if(response < 0)
{
printf("Error: bind() failed with return value %d.\n", response);
printf("Error: #%d (%s)\n", errno, strerror(errno));
}

cout << "*** Listening for connections. ***" << endl;

/* create queue for client connection requests */
response = listen(listenSocket, listenBacklog);
if(response < 0)
{
printf("Error: listen() failed with return value %d.\n", response);
printf("Error: #%d (%s)\n", errno, strerror(errno));
}

while(1)
{
cout << "*** Accepting connection. ***" << endl;

acceptSocket = accept(listenSocket, (struct sockaddr *)&acceptSocketInfo,
&acceptSocketSize);

if(acceptSocket < 0)
{
printf("Error: accept() failed with return value %d.\n", acceptSocket);
printf("Error: #%d (%s)\n", errno, strerror(errno));
}
else
{
printf("Accept Successful\n");
}
}
}

The calls to spawn the task inside the API are CommonCreateTask() then RunTask()

hunie10 at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...
# 5

CommonCreateTask and RunTask are proprietary to Integrity; there's no telling what side effects they're having on your application. I would suggest looking for an Integrity support forum for help with this. Your ListenForConnection looks fine to me; and if it's running fine without using spawnTask(), it sounds like ListenForConnection is not the culprit--leaving spawnTask and/or it's graph--meaning there's probably no one on this forum that can offer any guidance.

Some frameworks require that you initialize your sockets layer once for each thread; so you know if Integrity has that requirement?

PeterRitchie at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...
# 6
I am emailing Integrity support to see if there are any requirements for initializing sockets and to see if they have any ideas.

I will post my findings.

hunie10 at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,General Windows Vista Development Issues...

Software Development for Windows Vista

Site Classified