vxworks下UDP通信代码

代码内容

1. udp.h文件

#ifndef _UDP_H
#define _UDP_H

#include "vxWorks.h"
#include "msgQLib.h"
#include "semLib.h"
#include "sockLib.h"
#include "inetLib.h"

#include "string.h"
#include "hostLib.h"
#include "ioLib.h"
#include "timers.h"


#include "taskLib.h"
#include "tickLib.h"



#define MTU_MAX				1500
#define UDP_RECV_MAX_SIZE   (4*1024*1024)
#define UDP_SEND_MAX_SIZE   65507


typedef struct 
{
    int sd; /*udp descrip flag*/
    struct sockaddr_in addr;
	struct sockaddr    remote_addr;
	int port;
} UdpData;


int udp_init(UdpData *data, char *ipAddr, int port);
int net_bind(UdpData *data, int port, char *ipAddr);
int udp_send(UdpData *data, void *buff, int len);
int udp_recv(UdpData *data, void *buff, int len);

int udp_sendto(UdpData *data, void *buff, int len,struct sockaddr *addr);
int udp_recvfrom(UdpData *data, void *buff, int len,struct sockaddr *addr,int ms);


#endif



2.udp.c文件

#include "udp.h"

/*	代码功能:	初始化本地UdpData结构
*	输入参数:	UdpData *data	UdpData空结构体
*				char *ipAddr	待初始化的本地IP地址
*				int port		待初始化的本地端口号
*	返回参数:	-1未初始化成功
*				0初始化成功
*/
int udp_init(UdpData *data, char *ipAddr, int port)
{
    int sd;
    struct sockaddr_in addr;
    sd = socket(PF_INET, SOCK_DGRAM, 0);
    if (sd < 0) {
        printf("failed to open udp socket\n");
        return -1;
    }
	char on=1;
	setsockopt(sd,SOL_SOCKET, SO_REUSEADDR,&on,sizeof(on));   

    /* If server, then bind */
    bzero((char*)&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ipAddr);

    data->sd = sd;
    data->addr = addr;
	data->port = port;
    return 0;
}

/*	代码功能:	绑定本地udp
*	输入参数:	UdpData *data			UdpData空结构体
*				int nSocketPort			待初始化的本地端口号
*				char *SocketAddress		待初始化的本地IP地址
*	返回参数:	0  未绑定成功
*				其他  绑定成功
*/
int net_bind(UdpData *data, int nSocketPort, char *SocketAddress)
{

	struct sockaddr_in addr;
	char *IpAddr;
	memset(&addr,0,sizeof(addr) );
	
	IpAddr = SocketAddress;
	addr.sin_family = AF_INET;

	if (SocketAddress == 0)
		addr.sin_addr.s_addr = htonl(INADDR_ANY);
	else
	{
		unsigned long lResult = inet_addr(IpAddr);
		if (lResult == INADDR_NONE)
		{
			return 0;
		}
		addr.sin_addr.s_addr = lResult;
	}

	addr.sin_port = htons((u_short)nSocketPort);

	return bind(data->sd, (struct sockaddr *)&addr, sizeof(addr)) == 0;
}

/*	代码功能:	发送udp消息
*	输入参数:	UdpData *data			目标机的udpdatat结构体
*				void *buff				待发送信息缓冲字符串
*				int len					待发送信息长度,即strlen(buff)
*	返回参数:	-1 	 未发送成功
*				0    发送成功
*/
int udp_send(UdpData *data, void *buff, int len)
{
    int sd;
    struct sockaddr_in addr;
    int nsend = 0;
    unsigned int bufferLen = 0;
    unsigned int bufferPos = 0;
    unsigned int udpsendLen = UDP_SEND_MAX_SIZE;

    sd = data->sd;
    addr = data->addr;

    bufferLen = len;
    while (bufferLen > 0) {
        if(!(bufferLen / UDP_SEND_MAX_SIZE))
            udpsendLen = bufferLen % UDP_SEND_MAX_SIZE;

        nsend = sendto(sd,  (char *)buff + bufferPos, udpsendLen, 0, (struct sockaddr *)&addr, sizeof(addr));
        if (nsend < 0) {
            printf("udp_send failed \n");
            return -1;
        }
        bufferPos += udpsendLen;
        bufferLen -= udpsendLen;
    }
    return 0;
}

/*	代码功能:	接收udp信息
*	输入参数:	UdpData *data			目标机UdpData结构体
*				void *buff				待接收消息的缓冲区
*				int len					最大接收长度
*	返回参数:	0  	接收到消息
*				-1  未接收到消息	
*/
int udp_recv(UdpData *data, void *buff, int len)
{
	int sd;
	struct sockaddr_in addr;
	int addr_len = sizeof(struct sockaddr_in);
	int nrecv = 0;
	fd_set readfds;
	struct timeval tv;

	sd = data->sd;
	addr = data->addr;

	if(buff == NULL || len > UDP_RECV_MAX_SIZE) {
		printf("param error, exit.\n");
		return -1;
	}

	FD_ZERO(&readfds);
	FD_SET(sd, &readfds);
	tv.tv_sec = 0;
	tv.tv_usec = 100000;

	select(sd+1, &readfds,NULL, NULL, &tv);
	if(FD_ISSET(sd, &readfds)) {
		nrecv = recvfrom(sd, (char *)buff, len, 0, (struct sockaddr *)&addr, (socklen_t *)&addr_len);
		if(nrecv <= 0) {
			printf("recv error.\n");
			return -1;
		}
	} else {
		return -1;
	}
	return nrecv;
}

/*	代码功能:	点播发送消息,方便主机同时与多个不同ip的目标机通信
*	输入参数:	UdpData *data			目标机UdpData结构体
*				void *buff				发送消息缓冲区
*				int len					待发送消息长度
*				struct sockaddr *addr	待发送目标机ip地址、端口号结构体
*	返回参数:	0 		 发送成功
*				-1 	 	 发送失败
*	使用: 
*	struct sockaddr_in addr;
*	bzero((char*)&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ipAddr);
*/
int udp_sendto(UdpData *data, void *buff, int len,struct sockaddr *addr)
{
    int sd;
    int addr_len = sizeof(struct sockaddr_in);
    int nsend = 0;
    unsigned int bufferLen = 0;
    unsigned int bufferPos = 0;
    unsigned int udpsendLen = UDP_SEND_MAX_SIZE;
    sd = data->sd;
    bufferLen = len;
    while (bufferLen > 0) {
        if(!(bufferLen / UDP_SEND_MAX_SIZE))
            udpsendLen = bufferLen % UDP_SEND_MAX_SIZE;

        nsend = sendto(sd,  (char *)buff + bufferPos, udpsendLen, 0,addr, addr_len);
        if (nsend < 0) {
            return -1;
        }
        bufferPos += udpsendLen;
        bufferLen -= udpsendLen;
    }
    return 0;
}

/*	代码功能:	定点接收消息,方便主机同时与多个不同ip的目标机通信
*	输入参数:	UdpData *data			目标机UdpData结构体
*				void *buff				待接收消息缓冲区
*				int len					待接收消息最大长度
*				struct sockaddr *addr	待接收消息ip、端口号等信息结构体
*				int ms					select函数的阻塞时间
*	返回参数:	-1   -2  		接受失败
*				nrecv  			收到的字节数,接收成功
*/
int udp_recvfrom(UdpData *data, void *buff, int len,struct sockaddr *addr,int ms)
{
		int sd;

		int addr_len = sizeof(struct sockaddr_in);
		int nrecv = 0;
		fd_set readfds;
		struct timeval tv;
	
		sd = data->sd;
	
		if(buff == NULL || len > UDP_RECV_MAX_SIZE) {
			printf("param error, exit.\n");
			return -1;
		}
	
		FD_ZERO(&readfds);
		FD_SET(sd, &readfds);
		tv.tv_sec = 0;
		tv.tv_usec = ms*1000;
		if(ms < 0)
			select(sd+1, &readfds,NULL, NULL, NULL);//本程序中使用select来非阻塞的判断是否有信息要接收
		else			
			select(sd+1, &readfds,NULL, NULL, &tv);
		if(FD_ISSET(sd, &readfds)) {
			nrecv = recvfrom(sd, (char *)buff, len, 0, addr, (socklen_t *)&addr_len);
			if(nrecv <= 0) {
				printf("recv error.\n");
				return -1;
			}
		} else {
			return -2;
		}
		return nrecv;
}





关于select函数可以参考
或者我的一篇文章也有相关内容

3.main函数

初始化

	UdpData local_data = {0};
	UdpData dest_data = {0};
	char *local_ipAddr=0;							//本机ip
	char dest_ipAddr[256];							//目标机ip
	int local_port = 2300;							//本机端口号
	int dest_port = 2300;							//目标机端口号

	if(udp_init(&local_data,local_ipAddr,local_port) == 0)//初始化本地
		printf("udp local init success!\n");
	if(net_bind(&local_data,local_port,local_ipAddr)==0)//绑定本地
		printf("udp bind success!\n");
	if(udp_init(&dest_data,dest_ipAddr,dest_port) == 0)//初始化目标机
		printf("udp dest init success!\n");

循环发送信息

while(1)
	{
			if(udp_send(pdata,buff,strlen(buff)) == 0)
			{
				//printf("udp send success!\n");
				memset(buff,0,sizeof(buff));	//发送成功则清空存留消息
				//taskDelay(1600);
			}
	}

循环接收信息

while(1)
	{
			if(udp_recv(pdata,rbuff,sizeof(rbuff)) == -1)
			{
				printf("receive.....................................\n");
				break;
			}
			printf("received message: %s\n",rbuff);
			memset(rbuff, 0, sizeof(rbuff));//清空存留消息
		}
	}

你可能感兴趣的:(vxworks,c语言)