I have multiple threads of the function below and can only connect to 1 port at a time. I'm testing this code by running Hyperterminal on a PC and simply connecting and disconnecting the port. Each port works, each thread works, but I must close the open port on Hyperterminal before I can connect to the other port and verse visa. I've read about a bunch of other ways to support multiple ports, using select( ) and scanning through a list of sockets, but I'd like to get this to work.
Not sure if I have something configured improperly with MQX or RTCS or I'm just doing something silly with my code. This same approach worked with Nucleus OS.
Again, not trying to get advice on how to do it differently unless what I'm trying ain't gonna work, just trying to figure out why this approach isn't working.
Any help appreciated....
void tcp_task (uint32 channel)
CHANNEL_PTR p = com_ptrs[channel];
/* Prepare socket for incoming connections */
listen_sock = socket(PF_INET, SOCK_STREAM, 4);
option = SERIAL_SOCKET_BUFFER_SIZE;
(void) setsockopt(listen_sock, SOL_TCP, OPT_TBSIZE, &option, sizeof(option));
(void) setsockopt(listen_sock, SOL_TCP, OPT_RBSIZE, &option, sizeof(option));
((sockaddr_in*) &addr)->sin_family = AF_INET;
((sockaddr_in*) &addr)->sin_port = ports[channel-TCP1];
((sockaddr_in*) &addr)->sin_addr.s_addr = INADDR_ANY;
bind(listen_sock, &addr, sizeof(addr));
/* Accept incoming connection */
client_sock = accept(listen_sock, NULL, NULL);
if(client_sock == RTCS_SOCKET_ERROR)
/* Set socket option OPT_RECEIVE_NOWAIT = TRUE */
option = TRUE;
setsockopt(client_sock, SOL_TCP, OPT_RECEIVE_NOWAIT, &option, sizeof(option));
status[channel] = CONNECTED;