Hello every body, I´m developing my application. I use 3 socket for request
of the PC and i have 5 socket for answer. The connection is TCP.
My code is that, maybe somebody can use it.
Before, i have one question, if i want to send as soon as possible, then
i´ve heard about flush send. Can you help me ?
Thanks
Oscar
#include <string.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "ProtocolRTU.h"
#include "portmacro.h"
/* lwIP includes. */
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/arch.h"
#include "lwip/sys.h"
#include "netif/loopif.h"
#include "lwip/sockets.h"
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
struct sockaddr_in sLocalAddr[NUM_SOCKET];
struct sockaddr_in sRemoteAddr[NUM_SOCKET];
fd_set readset, acceptset;
//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]);
FD_CLR(lSocket[i], &acceptset);
}
}
}
else
{
CloseConnectionPC(aSocket,lSocket );
return;
}
}while ( numconection <NUM_SOCKET );
//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))
{
}
}
else
{
CloseConnectionPC(aSocket,lSocket );
return;
}
}
}
void static CloseConnectionPC(int * s1,int* s2 )
{
int i;
for (i=0;i<NUM_SOCKET;i++)
{
lwip_close(s1[i]);
lwip_close(s2[i]);
}
}
_______________________________________________
lwip-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/lwip-users