#ifndef _VXSVR_H
#define _VXSVR_H
#define LOCAL_SERVER_PORT 2001
#define NET_MSG_MAX_SIZE 100
#define TNAME_RECV "tNetRecv"
#define USER_STACE_SIZE 2000
#define TPRI_CMDRECV 101
#define NET_INIT_LISTENSKT 0x01
#define NET_INIT_COMMUSKT 0x02
#define NET_INIT_NULL 0x00
#endif
#include "VxWorks.h"
#include "taskLib.h"
#include "sockLib.h"
#include "inetLib.h"
#include "ioLib.h"
#include "logLib.h"
#include "string.h"
#include "stdio.h"
#include "netinet\\tcp.h"
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 has 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);
}
socketAddrSize=sizeof(struct sockaddr_in);
bzero((char *)&serverAddr,sockAddrSize);
serverAddr.sin_family=AF_INET;
serverAddr.sin_len=(u_char)sockAddrSize;
serverAddr.sin_port=htons(LOCAL_SERVER_PORT);
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
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);
}
logMsg("netInit:successfully bind to port\n",LOCAL_SERVER_PORT,0,0,0,0,0);
if(listen(listenSkt,q)==error)
{
logMsg("netInit:can not listen to socket\n",0,0,0,0,0,0);
close(listenSkt);
return (ERROR);
}
else
{
flagNetInit=NET_INIT_LISTENSKT;
}
}
commuSkt=accept(listenSkt,(struct sockaddr *)(&clientAddr),&sockAddrSize);
if(commuSkt==ERROR)
{
logMsg("netInit:can not accept command socket\n",0,0,0,0,0,0);
close(listenSkt);
return (ERROR);
}
else
{
logMsg("netInit:net Init finished\n",0,0,0,0,0,0);
setsockopt(commuSkt,IPPROTO_TCP,TCP_NODELAY,&optval,sizeof(optval));
if(taskNameToId(TNAME_RECV)==ERROR)
{
taskSpawn(TNAME_RECV,TPRI_CMDRECV,0,USER_STACK_SIZE,(FUNCPTR)netRecv,0,0,0,0,0,0,0,0,0,0);
}
}
flagNetInit=NET_INIT_COMMUSKT;
return (OK);
}
STATUS netSend(char *pBuff)
{
int buffLen;
if((pBuf!=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 net 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("netSewnd: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,pBiff,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);
}
void netCloseAll()
{
int taskId=taskNameToId(TNAME_RECV);
if(taskId!=taskIdSelf()&&taskId!=ERROR)
{
taskDelete(taskId);
}
if(flagNetInit==NET_INIT_COMMUSKT)
{
close(commuSkt);
close(listenSkt);
}
else if(flagNetInit==NET_INIT_LISTENSKT)
{
close(listenSkt);
}
flagNetInit=NET_INIT_NULL��
logMsg("netCloseAll:net connection shut down\n",0,0,0,0,0,0);
}