int listenSkt;
int commuSkt;
int flagNetInit=NET_INIT_NULL;
STATUS netInit();
STATUS netRecv();
STATUS netSend(char *pBuff);
void netCloseAll();
STATUS netInit()
{
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
int sockAddrSize;
int i;
char optval=1;
if(flagNetInit==NET_INIT_COMMUSKT)
{
logMsg("netinit:server has a already been initialized\n",0,0,0,0,0,0);
return(OK);
}
else if(flagNetInit==NET_INIT_NULL)
{
if((listenSkt=socket(AF_INET,SOCK_STREAM,0))==ERROR)
{
logMsg("netinit:can not open listen socket\n",0,0,0,0,0,0);
return(ERROR);
}
if(bind(listenSkt,(struct sockaddr*) &serverAddr,sockAddrSize)==ERROR)
{
logMsg("netinit:unable to bind to port %d\n",LOCAL_SERVER_PORT,0,0,0,0,0);
close(listenSkt);
return(ERROR);
}
/*else*/
logMsg("netinit:successfully bind to prot\n",LOCAL_SERVER_PORT,0,0,0,0,0);
if(listen(listenSkt,1)==ERROR)
{
logMsg("netInit:can not listen to listen tolisten socker\n",0,0,0,0,0,0);
close(listenSkt);
return(ERROR);
}
else
{
flagNetInit=NET_INIT_LISTENSKT;
}
}
flagNetInit=NET_INIT_COMMUSKT;
return(OK);
}
STATUS netSend(char *pBuff)
{
int buffLen;
if((pBuff!=NULL)&&(flagNetInit==NET_INIT_COMMUSKT))
{
buffLen=strlen(pBuff);
if(buffLen>=NET_MSG_MAX_SIZE)
{
buffLen=NET_MSG_MAX_SIZE;
pBuff[buffLen-1]=0;
}
if(send(commuSkt,pBuff,buffLen,0)==ERROR)
{
logMsg("netsend:error in sending buff to client\n",0,0,0,0,0,0);
netCloseAll();
return(ERROR);
}
return(OK);
}
else
{
if(flagNetInit==NET_INIT_NULL)
logMsg("netsend:server has not been initialized\n",0,0,0,0,0,0);
else if (flagNetInit==NET_INIT_LISTENSKT)
{
logMsg("netSend:no connection created yet\n",0,0,0,0,0,0);
logMsg("netSend:please connect to vxworks server with pc programm\n",0,0,0,0,0,0);
}
else
{
logMsg("netSend:you message is invalid\n",0,0,0,0,0,0);
}
return(ERROR);
}
}
STATUS netRecv()
{
char pBuff[NET_MSG_MAX_SIZE+1];
int recvLen;
while(flagNetInit==NET_INIT_COMMUSKT)
{
recvLen=recv(commuSkt,pBuff,NET_MSG_MAX_SIZE,0);
if((recvLen!=ERROR)&&(recvLen!=0))
{
pBuff[recvLen]=0;
logMsg("netRecv:received message from client:\n%s\n",(int)pBuff,0,0,0,0,0);
}
else
{
netCloseAll();
return(ERROR);
}
}
logMsg("netRecv:net connection is shut down,quit\n",0,0,0,0,0,0);
return(OK);
}