資源簡介
Vxworks下UDP收發(fā)與組播收發(fā)例程源碼,也可用于Linux或者其它平臺。

代碼片段和文件信息
#include?“vxWorks.h“?
#include?“sockLib.h“?
#include?“inetLib.h“?
#include?“stdioLib.h“?
#include?“strLib.h“?
#include?“ioLib.h“?
#include?“fioLib.h“?
#include?“UDP.h“
int?sFd?=?0;???????????/*?Socket描述符?*/
/****************************************
函數名稱:void?InitUdpSocket()
返回值:無
入參:??無
函數描述:初始化Udp的Socket具體包含以下操作:
????????1、創(chuàng)建Socket;
????????2、綁定Server端的地址。
****************************************/
void?InitUdpSocket()
{
struct?sockaddr_in??LocalAddr;????/*?服務器端的socket地址信息*/
int?????????????????sockAddrSize;??/*?size?of?socket?address?structure?*/
/*?初始化地址?*/??????
sockAddrSize?=?sizeof?(struct?sockaddr_in);?????
bzero?((char?*)?&LocalAddr?sockAddrSize);?????
LocalAddr.sin_len?=?(u_char)?sockAddrSize;?????
LocalAddr.sin_family?=?AF_INET;?????
LocalAddr.sin_port?=?htons?(5500);?????
//LocalAddr.sin_addr.s_addr?=?htonl?(INADDR_ANY);
LocalAddr.sin_addr.s_addr?=?inet_addr?(“192.168.3.235“);???
/*?創(chuàng)建Socket?*/??????
if((sFd?=?socket(AF_INETSOCK_DGRAM0))?==?ERROR)?????????
{?????????
printf(“\r\n[ERROR]:socket()?error?in?InitUdpSocket()!“);
}????
#if?1
/*?bind?socket?to?local?address?*/??????
if?(bind?(sFd?(struct?sockaddr?*)&LocalAddr?sockAddrSize)?==?ERROR)?????????
{?????????
printf(“\r\n[ERROR]:bind()?error?in?InitUdpSocket()!“);?????????
close?(sFd);?????????
}
#endif
#if?0
if(setsockopt(sFd?IPPROTO_IP?IP_MULTICAST_IF?(char?*)&LocalAddr?sizeof(LocalAddr))?==?ERROR)???
{???
printf(“First?Setsockopt?Error!\n“);
}???
else???
{???
printf(“First?Setsockopt?Ok!\n“); ?
}???
#endif
}
/****************************************
STATUS?udpServer?(UINT8?*pRcvDataUINT32?RcvDataLenUINT32?Port)??
返回值:ERROR?---接收失敗;???非ERROR?---接收成功,接收到的字節(jié)個數
入參:pRcvData?????---接收到的數據指針
?????Port?????????---接收的端口號
函數描述:Udp的接收函數
****************************************/
STATUS?udpServer?(UINT8?*pRcvDataUINT32?Port)
{?????
struct?sockaddr_in??clientAddr;????/*?客戶端的socket地址信息?*/
int?????????????????sockAddrSize;??/*?size?of?socket?address?structure?*/
int?Ret?=?0;
bzero((UINT8*)&clientAddr?sizeof(clientAddr));
sockAddrSize?=?sizeof(clientAddr);
Ret?=?recvfrom?(sFdpRcvData10240(struct?sockaddr?*)&clientAddr&sockAddrSize);
if(Ret?==?ERROR)?????????????
{?????????????
printf(“\r\n[ERROR]:recvfrom()?error!“);
close?(sFd);?????????????
return?(ERROR);?????????????
}
return?(Ret);
}
/****************************************
函數名稱:STATUS?udpClient(char?*?serverNameUINT16?Port?UINT8*?sndDataUINT32?lenth)?????
返回值:0?---發(fā)送成功;???非0?---發(fā)送失敗
入參:serverName???---接收端(Server)的IP地址
?????Port?????????---端口號
?????sndData??????---要發(fā)送數據的指針
?????lenth????????---要發(fā)送的數據字節(jié)個數
函數描述:Udp的發(fā)送函數
****************************************/
STATUS?udpClient(char?*?serverNameUINT16?Port?UINT8*?sndDataUINT32?lenth)?????
{?????
struct?sockaddr_in??serverAddr;????/*?server‘s?socket?address?*/?????
int?????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9304??2018-07-16?20:17??udp\UDP.c
?????文件????????301??2015-10-10?15:31??udp\UDP.h
?????目錄??????????0??2018-07-03?13:47??udp
-----------?---------??----------?-----??----
?????????????????9605????????????????????3
- 上一篇:zkui-2.0 zookeeper可視化管理器
- 下一篇:WEBRTC多人視頻通話
評論
共有 條評論