UDP SERVER的例子(VxWorks6.9)

include

include

include

include

include”sockLib.h”

include “stdio.h”

include “stdlib.h”

include “string.h”

include “cq.h”

struct sockaddr_in serverAddr1;
struct sockaddr_in clientAddr;
int sock;

unsigned int sndBuffer[256];
unsigned int RcvBuffer[256];
SEM_ID SND_SEM;

static int cmd=0;
static int cmdId=0;
CMD_BUFFER *udpCmd;

void udpRecvTask (void)
{

int                 nrecv;  /* number of bytes received */
int sockAddrSize = sizeof(struct sockaddr);




if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
{
    printf ("cannot open socket\n");
    return;
}


/*udpCmd.cmdParameter=udpBuffer;*/

inet_aton("192.168.1.20",(struct in_addr *)&serverAddr1.sin_addr);

serverAddr1.sin_family  = AF_INET;
serverAddr1.sin_port    = htons (50000);


bzero((char *)&serverAddr1.sin_zero, 8);  /* zero rest of struct */

if (bind (sock, (struct sockaddr *)&serverAddr1, sizeof(serverAddr1)) < 0)
{
    printf ("bind error\n");
    close (sock);
    return;
}


for(;;)
{
    nrecv = recvfrom (sock, (char *)RcvBuffer, 1024, 0, (struct sockaddr *)&clientAddr,
                                  (int *)&sockAddrSize);


    //printf("client Addr is 0x%x\n",clientAddr.sin_addr.s_addr);
    if(nrecv>0)
    {
        udpCmd=(CMD_BUFFER *)RcvBuffer;
        udpCmd->cmdId=0x5A5A5A5A;
        udpCmd->cmdParameter=(unsigned char *)&RcvBuffer[2];
        CQ_IN(udpCmd);
        semGive(SND_SEM);

    }
 }

}

void cmdProcess(void)
{
STATUS ret;
CMD_BUFFER sndCmd;
int sockAddrSize = sizeof(struct sockaddr);

sndCmd.cmdParameter=sndBuffer;
while(1)
{
    semTake(SND_SEM,WAIT_FOREVER);
    ret=CQ_OUT(&sndCmd);
    if(ret!=ERROR)
    {
        sndBuffer[0]=sndCmd.cmd;
        sndBuffer[1]=sndCmd.cmdId;
        sendto(sock, (char *)sndBuffer, 1024, 0, (struct sockaddr *)&clientAddr,
                    sockAddrSize);
    }
    //taskDelay(10);

}

}

你可能感兴趣的:(OS,Protocol)