Here is my code:
The thread is used to start server and communicate with client and im running it at higher priority than the other threads
void RunServer(unsigned long thread_input)
{
struct sockaddr_in fsin;
int sock,ClientSock;
int not_used;
/* Internet endpoint address */
int result;
int buffsize = RCV_BUFFER_SIZE;
int timeout = 0; /* timeout for send and recv calls -nearly 2 minutes*/
unsigned char fResult = 1;
/* Create the socket for TCP communication */
if((sock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Socket Creation Error...
");
return;
}
else
{
printf("Socket Creation Successfull…
");
}
/* set socket to use Blocking IO */
result = setsockopt(sock, SOL_SOCKET, SO_BIO, (char *)¬_used, sizeof(not_used));
if (result < 0)
{
printf("UDPecho: setsockopt SO_BIO failure: errno: %d
", getErrno());
socketclose(sock);
return;
}
/* reset structure contents to zero */
memset((char*)&fsin, '0', sizeof(fsin));
/* fill socket structure to the MODBUS port and the local machine IP */
fsin.sin_family = AF_INET;
fsin.sin_port = htons(8085);
fsin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, &fsin, sizeof(fsin)) < 0)
{
printf(" bind failure: errno: %d
", getErrno());
socketclose(sock);
return;
}
printf(" Server Ready on port %d.
", 8085);
if( listen( sock, 1) == SOCKET_ERROR )
{
printf("bind failure: errno: %d
", getErrno());
return ;
}
else
{
/* for ever */
while(1)
{
printf("Slave Active…
Waiting for New connection …
");
if( (ClientSock = accept( sock, NULL, NULL )) == INVALID_SOCKET )
{
printf(“Error Accepting New Connection…”);
return ;
}
printf("New Connection Request Accepted…
");
while(1)
{
do
{
//receive is handled here
fResult = HandleRequest((unsigned long)ClientSock);
}
while(fResult);
if( Slave_LastMbErr == ECOMMPATHERROR || Slave_LastMbErr == ETIMEOUT)
{
break;
}
}
/* HandleRequest() indicated a communication errorSo reset communication by closing the socket and reopening the same */
closesocket(ClientSock);
printf("Closing Socket...
");
ClientSock = 0;
}
}
}
Read function called in HandleRequest() looks so:
int ReadSerialPort(xdata DWORD CommnPathNo, xdata int nBytes, xdata unsigned char * xdata Buffer)
{
struct timeval wait;
int iPortReady;
struct fd_set fd_rcv;
wait.tv_sec=20;
wait.tv_usec=20*1000; //20ms
int errcode;
FD_ZERO(&fd_rcv);
#ifndef NETOS_GNU_TOOLS
FD_SET((SOCKET)CommnPathNo,&fd_rcv);
#else
FD_SET((int)CommnPathNo,&fd_rcv);
#endif
iPortReady=select(FD_SETSIZE,&fd_rcv,NULL,NULL,&wait);
if(iPortReady == 0)
{
printf("Rcv Socket timedout
");
nBytes = -1;
return nBytes;
}
//recvfrom(sock, rcvData, RCV_BUFFER_SIZE, 0, &fsin, &szfsin);
if(FD_ISSET(CommnPathNo,&fd_rcv)) {
if( (nBytes = recv((int)CommnPathNo, Buffer, nBytes, 0 )) <= 0 )
{
if(nBytes<0)
{
printf("Error reading bytes from socket, errorcode = %d
",nBytes);
errcode=getErrno();
printf("Error no -%d
",errcode);
if(errcode == ENOTCONN){
nBytes = - 1;
printf("Socket Rcv Error -ENOTCONN
");
}
else if(errcode == ENOTSOCK){
nBytes = - 1;
printf("Socket Rcv Error -ENOTSOCK
");
}
else if(errcode == EWOULDBLOCK){
nBytes = - 1;
printf("Socket Rcv Error -EWOULDBLOCK
");
}
else
{
//ESOCKNOTSUPPORT or errorcode =124 occurs offen here
nBytes=1; //Set this to 1 ,that the connection dont close
printf("Read error ,but continue to listen
");
}
}
else { printf("Socket connection closed by peer
");nBytes= -1;}
}
} else {nBytes = -1; }
return nBytes;
}
Hope this helps,
Kishore