Thanks Tom. Memory will certainly be a concern. Even looking at the examples, I’m not sure how a server handles multiple clients. It looks like the freescale TCP server example is setup to handle this, but I’m not sure what changes are needed.
For example, does listen() get called once or multiple times?
The freescale TCP server example has declared a server socket, communication socket, and message ring of 10 sockets:
struct sockaddr_in emg_tcp_sin;
M_SOCK emg_tcp_server_socket = INVALID_SOCKET;
static struct msring emg_tcp_msring;
static M_SOCK emg_tcp_msring_buf[10];
M_SOCK emg_tcp_communication_socket = INVALID_SOCKET;
During initialization m_listen() is called and return value assigned to the server socket.
emg_tcp_sin.sin_addr.s_addr = (INADDR_ANY);
emg_tcp_sin.sin_port = (PORT_NUMBER);
emg_tcp_server_socket = m_listen(&emg_tcp_sin, freescale_tcp_cmdcb, &e);
The socket callback function “freescale_tcp_cmdcb()” handles connecting and disconnecting. When a connection is opened the socket is put in the msring buffer using msring_add():
int freescale_tcp_cmdcb(int code, M_SOCK so, void * data)
{
int e = 0;
switch(code)
{ // socket open complete
case M_OPENOK:
msring_add(&emg_tcp_msring, so);
break;
// socket has closed
case M_CLOSED:
while( semaphore ){};
semaphore = 1;
emg_tcp_communication_socket = INVALID_SOCKET;
m_close(so); //FSL close the socket
semaphore = 0;
break;
}
A separate task handles changes to the msring buffer and processes data from a connection on the socket:
void freescale_tcp_check(void)
{
M_SOCK so;
if ( emg_tcp_server_socket == INVALID_SOCKET )
return ;
while(msring_del(&emg_tcp_msring, &so) == 0)
{
while( semaphore ){};
semaphore = 1;
if( emg_tcp_communication_socket == INVALID_SOCKET )
{
m_ioctl(so, SO_NONBLOCK, NULL); /* socket non-blocking */
emg_tcp_communication_socket = so;
semaphore = 0;
}
if( emg_tcp_communication_socket != INVALID_SOCKET )
freescale_tcp_loop();
semaphore = 0;
} // while
if( emg_tcp_communication_socket != INVALID_SOCKET )
freescale_tcp_loop();
}
The freescale_tcp_loop() handles receiving/transmitting data from “emg_tcp_communication_socket” using m_recv().
It would seem that the msring buffer is in place to handle multiple client connections on the socket. Is this correct?
If yes, then for each connection there would need to be an associated "emg_tcp_communication_socket". Something like “emg_tcp_communication_socket[10]" to handle up to 10 clients.
Conceptually one problem I am having is how to handle when the connections open or closed via the socket callback function. Now the callback function closes the single client connection and marks "emg_tcp_communication_socket" as invalid.
emg_tcp_communication_socket = INVALID_SOCKET;
m_close(so); //FSL close the socket
With multiple client connections open (in my example emg_tcp_communication_socket[10] ) how does the callback function know which connection to close?
Maybe this is simple for someone familiar with socket communications.
Thanks for the help.