int aSocket[NUM_SOCKET]; //Accepted Socket int PortConnection[NUM_SOCKET]={0x1500,0x1501,0x1502,0x1600,0x1601,0x1602,0x1603,0x1604};
portCHAR BufferRx[TAM_RX_RTU];
void static CloseConnectionPC(int * s1,int* s2 );
portTASK_FUNCTION( vProtocolRTUServer, pvParameters ) { //Local variables of function ==> maybe put global
int i,maxfd,maxfd2,numconection,ret; int lSocket[NUM_SOCKET]; //Local Socket
//Create the NUM_SOCKET=8 connection to wait the accept for(i=0;i<NUM_SOCKET;i++) { //Create the 8 socket lSocket[i] = lwip_socket(AF_INET, SOCK_STREAM, 0); if (lSocket[i]<0)
return;
//Bind to this socket memset((char *)&sLocalAddr[i], 0, sizeof(sLocalAddr[i])); sLocalAddr[i].sin_family = AF_INET; sLocalAddr[i].sin_len = sizeof(sLocalAddr[i]);
sLocalAddr[i].sin_addr.s_addr = htonl(INADDR_ANY); //!!!!CUIDADO SINO VALE CUALQUIER DIRECCION CAMBIAR!!!! sLocalAddr[i].sin_port = PortConnection[i]; //Ports of the table
if (lwip_bind(lSocket[i],(struct sockaddr *)&sLocalAddr[i], sizeof(sLocalAddr[i])) < 0)
{ //Error. Close the socket lwip_close(lSocket[i]); }
//Listen to the 8 socket.Maximum 1 connection for socket if ( lwip_listen(lSocket[i],1) != 0 )
{ lwip_close(lSocket[i]); return; } }
//Using of select function to wait for the accept connection FD_ZERO(&acceptset); FD_ZERO(&readset);
maxfd=0; maxfd2=0; numconection=0; //Add the 8 socket to wait the accept connection for(i=0;i<NUM_SOCKET;i++) { FD_SET(lSocket[i], &acceptset);
if (lSocket[i]> maxfd) maxfd = lSocket[i]; }
do{ // Check for received packets on configured sockets (blocking ) ret = select(maxfd, &acceptset, NULL, NULL, NULL);
if (ret > 0) //No error { for(i=0;i<NUM_SOCKET;i++) { if (FD_ISSET(lSocket[i], &acceptset)) { //connection receive ==> then accept
aSocket[i]= accept(lSocket[i],(struct sockaddr*)&sRemoteAddr[i],(socklen_t *)sizeof(sRemoteAddr[i])); if(aSocket[i]>0) { if (i<3)
{ FD_SET(aSocket[i], &readset); //Add the three socket to wait for the receive data if (aSocket[i]> maxfd2) maxfd2 = aSocket[i];
} numconection++; } else lwip_close(aSocket[i]);
//Process the request for three socket. the other socket are only for answer
while(1) { // Check for received packets on configured sockets (blocking ) ret = select(maxfd2, &readset, NULL, NULL, NULL);
if (ret > 0) //No error { //Check data for socket 1 if (FD_ISSET(aSocket[0], &readset)) {
}
//Check data for socket 2
if (FD_ISSET(aSocket[1], &readset)) {
}
//Check data for socket 3 if (FD_ISSET(aSocket[2], &readset)) {