概述
使用lstLib把所有建立链接的FD加入到链表中
#include<vxworks.h>
#include"sockLib.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"lstLib.h"
int sockFd;
int newSockFd;
int maxRxFd=0;
typedef struct clientsList
{
NODE
fdNode;
int
fd;
}FDNODE;
LIST cl;
char *buffer;
void tcpListenTask (void)
{
struct sockaddr_in saddr,caddr;
int bufLen=1500;
lstInit(&cl);
int addrLen;
int nRead=0;
bzero ((char *) &saddr, sizeof (saddr));
bzero ((char *) &caddr, sizeof (caddr));
buffer=malloc(bufLen);
if(bufLen==NULL)
{
return;
}
if ((sockFd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
free (buffer);
return;
}
saddr.sin_family
= AF_INET;
saddr.sin_port = htons (50000);
saddr.sin_addr.s_addr=INADDR_ANY;
if(bind(sockFd, (struct sockaddr *)&saddr, sizeof(saddr))<0)
{
free (buffer);
return;
}
if(setsockopt(sockFd, SOL_SOCKET, SO_RCVBUF,(char *) &bufLen, sizeof (bufLen)) < 0)
{
printf ("setsockopt SO_RCVBUF failedn");
free (buffer);
return;
}
if (setsockopt (sockFd, SOL_SOCKET, SO_SNDBUF, (void *)&bufLen,
sizeof (bufLen)) < 0)
{
printf ("setsockopt SO_SNDBUF failedn");
free (buffer);
return;
}
if (listen (sockFd, 5) < 0)
{
printf ("listen failedn");
close (sockFd);
free (buffer);
return;
}
addrLen=sizeof(caddr);
while (1)
{
newSockFd = accept(sockFd, (struct sockaddr *)&caddr, &addrLen);
if(newSockFd>0)
{
FDNODE *pFd;
printf("new socket connectedn");
if(setsockopt(newSockFd, SOL_SOCKET, SO_RCVBUF,(char *) &bufLen, sizeof (bufLen)) < 0)
{
printf ("setsockopt SO_RCVBUF failedn");
free (buffer);
return;
}
if (setsockopt (newSockFd, SOL_SOCKET, SO_SNDBUF, (void *)&bufLen,
sizeof (bufLen)) < 0)
{
printf ("setsockopt SO_SNDBUF failedn");
free (buffer);
return;
}
pFd=malloc(sizeof(FDNODE));
pFd->fd=newSockFd;
lstAdd(&cl,pFd);
maxRxFd=newSockFd;
}
}
}
void tcpRxTask(void)
{
int num=0;
fd_set readFds;
FDNODE *pfd;
struct timeval timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
int nRead;
while(1)
{
FD_ZERO(&readFds);
pfd= lstFirst(&cl);
while(pfd)
{
FD_SET(pfd->fd,&readFds);
pfd=lstNext(pfd);
}
num=select(maxRxFd,&readFds,NULL,NULL,&timeout);
if(num>0)
{
pfd= lstFirst(&cl);
while(pfd)
{
if (FD_ISSET(pfd->fd, &readFds))
{
do
{
nRead=recv(pfd->fd,buffer,1500,0);
buffer=buffer+nRead;
}while(nRead>0);
}
}
pfd=lstNext(pfd);
}
}
void createTask(void)
{
(void)taskSpawn("LsnTask", 150, 0, 8192, (FUNCPTR)tcpListenTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
(void)taskSpawn("tRxTask", 150, 0, 8192, (FUNCPTR)tcpRxTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
最后
以上就是暴躁悟空为你收集整理的vxWorks上使用select接收TCP报文的全部内容,希望文章能够帮你解决vxWorks上使用select接收TCP报文所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复