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]
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.
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()