时间:2021-05-20
本文实例为大家分享了C语言UDP传输系统的具体代码,供大家参考,具体内容如下
#pragma comment( lib, "ws2_32.lib" )#include <winsock2.h>#include <ws2tcpip.h>#include <stdio.h>#include <stdlib.h>#define MCASTADDR "224.3.5.8"#define MCASTPORT 25000#define BUFSIZE 1024#define MCOUNT 10#define BCASTPORT 5050#define BCOUNT 10SOCKET socketBro;SOCKET socketRec;struct sockaddr_in addrBro;struct sockaddr_in addrRec;BOOL broadSendFlag;BOOL broadFlag;DWORD bCount;DWORD bcastAddr;short bPort;SOCKET socketMul;SOCKET sockJoin;struct sockaddr_in addrLocal;struct sockaddr_in addrMul;BOOL multiSendFlag;BOOL bLoopBack; BOOL multiFlag;DWORD dwInterface; DWORD dwMulticastGroup;DWORD mCount; short mPort; void initial();void GetArgments(int argc, char **argv);void userHelpAll();void userHelpBro();void userHelpMul();void broadcastSend();void broadcastRec();void mulControl();void multicastSend();void multicastRec();void initial(){ bPort = BCASTPORT; bCount = BCOUNT; bcastAddr = INADDR_BROADCAST; broadSendFlag = FALSE; broadFlag = FALSE; multiFlag = FALSE; dwInterface = INADDR_ANY; dwMulticastGroup = inet_addr(MCASTADDR); mPort = MCASTPORT; mCount = MCOUNT; multiSendFlag = FALSE; bLoopBack = FALSE;}void GetArgments(int argc, char **argv){ int i; if(argc<=1) { userHelpAll(); return ; } if(argv[1][0]=='-'&&argv[1][1]=='b') { broadFlag = TRUE; for(i=2; i < argc ;i++) { if (argv[i][0] == '-') { switch (tolower(argv[i][1])) { case 's': broadSendFlag = TRUE; break; case 'h': if (strlen(argv[i]) > 3) bcastAddr = inet_addr(&argv[i][3]); break; case 'p': if (strlen(argv[i]) > 3) bPort = atoi(&argv[i][3]); break; case 'n': bCount = atoi(&argv[i][3]); break; default: { userHelpBro(); ExitProcess(-1); } break; } } } return ; } if(argv[1][0]=='-'&&argv[1][1]=='m') { multiFlag = TRUE; for(i=2; i < argc ;i++) { if (argv[i][0] == '-') { switch (tolower(argv[i][1])) { case 's': multiSendFlag = TRUE; break; case 'h': if (strlen(argv[i]) > 3) dwMulticastGroup = inet_addr(&argv[i][3]); break; case 'i': if (strlen(argv[i]) > 3) dwInterface = inet_addr(&argv[i][3]); break; case 'p': if (strlen(argv[i]) > 3) mPort = atoi(&argv[i][3]); break; case 'l': bLoopBack = TRUE; break; case 'n': mCount = atoi(&argv[i][3]); break; default: userHelpMul(); break; } } } } return;}void userHelpAll(){ printf("Please choose broadcast[-b] or multicast[-m] !\n"); printf("userHelpAll: -b [-s][p][-h][-n] | -m[-s][-h][-p][-i][-l][-n]\n"); userHelpBro(); userHelpMul();}void userHelpBro(){ printf("Broadcast: -b -s:str -p:int -h:str -n:int\n"); printf(" -b Start the broadcast program.\n"); printf(" -s Act as server (send data); otherwise\n"); printf(" receive data. Default is receiver.\n"); printf(" -p:int Port number to use\n "); printf(" The default port is 5050.\n"); printf(" -h:str The decimal broadcast IP address.\n"); printf(" -n:int The Number of messages to send/receive.\n"); printf(" The default number is 10.\n");}void userHelpMul(){ printf("Multicast: -m -s -h:str -p:int -i:str -l -n:int\n"); printf(" -m Start the multicast program.\n"); printf(" -s Act as server (send data); otherwise\n"); printf(" receive data. Default is receiver.\n"); printf(" -h:str The decimal multicast IP address to join\n"); printf(" The default group is: %s\n", MCASTADDR); printf(" -p:int Port number to use\n"); printf(" The default port is: %d\n", MCASTPORT); printf(" -i:str Local interface to bind to; by default \n"); printf(" use INADDRY_ANY\n"); printf(" -l Disable loopback\n"); printf(" -n:int Number of messages to send/receive\n"); ExitProcess(-1);}void broadcastSend(){ char *smsg="The message received is from sender!"; BOOL opt=TRUE; int nlen=sizeof(addrBro); int ret; DWORD i=0; socketBro=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED); if(socketBro==INVALID_SOCKET) { printf("Create socket failed:%d\n",WSAGetLastError()); WSACleanup(); return; } addrBro.sin_family=AF_INET; addrBro.sin_addr.s_addr=bcastAddr; addrBro.sin_port=htons(bPort); if (setsockopt(socketBro,SOL_SOCKET,SO_BROADCAST,(char FAR *)&opt, sizeof(opt))==SOCKET_ERROR) { printf("setsockopt failed:%d",WSAGetLastError()); closesocket(socketBro); WSACleanup(); return; } while(i<bCount) { Sleep(1000); ret=sendto(socketBro,smsg,256,0,(struct sockaddr*)&addrBro,nlen); if(ret==SOCKET_ERROR) printf("Send failed:%d",WSAGetLastError()); else { printf("Send message %d!\n",i); } i++; } closesocket(socketBro); WSACleanup();}void broadcastRec(){ BOOL optval = TRUE; int addrBroLen; char buf[256]; DWORD i=0; addrRec.sin_family=AF_INET; addrRec.sin_addr.s_addr=0; addrRec.sin_port=htons(bPort); addrBro.sin_family=AF_INET; addrBro.sin_addr.s_addr=bcastAddr; addrBro.sin_port=htons(bPort); addrBroLen=sizeof(addrBro); //创建UDP套接字 socketRec=socket(AF_INET,SOCK_DGRAM,0); if(socketRec==INVALID_SOCKET) { printf("Create socket error:%d",WSAGetLastError()); WSACleanup(); return; } if(setsockopt(socketRec,SOL_SOCKET,SO_REUSEADDR,(char FAR *)&optval, sizeof(optval))==SOCKET_ERROR) { printf("setsockopt failed:%d",WSAGetLastError()); closesocket(socketRec); WSACleanup(); return; } if(bind(socketRec,(struct sockaddr *)&addrRec, sizeof(struct sockaddr_in))==SOCKET_ERROR) { printf("bind failed with: %d\n", WSAGetLastError()); closesocket(socketRec); WSACleanup(); return ; } while(i<bCount) { recvfrom(socketRec,buf,256,0,(struct sockaddr FAR *)&addrBro,(int FAR *)&addrBroLen); Sleep(2000); printf("%s\n",buf); ZeroMemory(buf,256); i++; } closesocket(socketRec); WSACleanup();}void mulControl(){ int optval; if ((socketMul = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF | WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) { printf("socket failed with: %d\n", WSAGetLastError()); WSACleanup(); return ; } addrLocal.sin_family = AF_INET; addrLocal.sin_port = htons(mPort); addrLocal.sin_addr.s_addr = dwInterface; if (bind(socketMul, (struct sockaddr *)&addrLocal, sizeof(addrLocal)) == SOCKET_ERROR) { printf("bind failed with: %d\n", WSAGetLastError()); closesocket(socketMul); WSACleanup(); return ; } addrMul.sin_family = AF_INET; addrMul.sin_port = htons(mPort); addrMul.sin_addr.s_addr = dwMulticastGroup; optval = 8; if (setsockopt(socketMul, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&optval, sizeof(int)) == SOCKET_ERROR) { printf("setsockopt(IP_MULTICAST_TTL) failed: %d\n",WSAGetLastError()); closesocket(socketMul); WSACleanup(); return ; } if (bLoopBack) { optval = 0; if (setsockopt(socketMul, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&optval, sizeof(optval)) == SOCKET_ERROR) { printf("setsockopt(IP_MULTICAST_LOOP) failed: %d\n", WSAGetLastError()); closesocket(socketMul); WSACleanup(); return ; } } if ((sockJoin = WSAJoinLeaf(socketMul, (SOCKADDR *)&addrMul, sizeof(addrMul), NULL, NULL, NULL, NULL, JL_BOTH)) == INVALID_SOCKET) { printf("WSAJoinLeaf() failed: %d\n", WSAGetLastError()); closesocket(socketMul); WSACleanup(); return ; }}void multicastSend(){ TCHAR sendbuf[BUFSIZE]; DWORD i; int ret; mulControl(); for(i = 0; i < mCount; i++) { sprintf(sendbuf, "server 1: This is a test: %d", i); ret=sendto(socketMul, (char *)sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&addrMul, sizeof(addrMul)); if(ret==SOCKET_ERROR) { printf("sendto failed with: %d\n",WSAGetLastError()); closesocket(sockJoin); closesocket(socketMul); WSACleanup(); return ; } else printf("Send message %d\n",i); Sleep(500); } closesocket(socketMul); WSACleanup();}void multicastRec(){ DWORD i; struct sockaddr_in from; TCHAR recvbuf[BUFSIZE]; int ret; int len = sizeof(struct sockaddr_in); mulControl(); for(i = 0; i < mCount; i++) { if ((ret = recvfrom(socketMul, recvbuf, BUFSIZE, 0, (struct sockaddr *)&from, &len)) == SOCKET_ERROR) { printf("recvfrom failed with: %d\n",WSAGetLastError()); closesocket(sockJoin); closesocket(socketMul); WSACleanup(); return ; } recvbuf[ret] = 0; printf("RECV: '%s' from <%s>\n", recvbuf,inet_ntoa(from.sin_addr)); } closesocket(socketMul); WSACleanup();}int main(int argc, char **argv){ WSADATA wsd; initial(); GetArgments(argc, argv); if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) { printf("WSAStartup() failed\n"); return -1; } if(broadFlag) { if(broadSendFlag) { broadcastSend(); return 0; } else { broadcastRec(); return 0; } } if(multiFlag) { if(multiSendFlag) { multicastSend(); return 0; } else { multicastRec(); return 0; } } return 0;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
TFTP即简单文本传输协议(TrivialFileTransferProtocol),允许用户通过UDP协议在系统之间传输文件。默认情况下,协议使用的是UDP的
一、摘要总结基于C#的UDP协议的同步通信。二、实验平台VisualStudio2010三、实验原理UDP传输协议同TCP传输协议的区别可查阅相关文档,此处不再
一、概述UDP和TCP是网络通讯常用的两个传输协议,C#一般可以通过Socket来实现UDP和TCP通讯,由于.NET框架通过UdpClient、TcpList
提供不可靠的数据传输的协议是UDP用户数据报协议。 UDP用户数据报协议即UDP(UserDatagramProtocol,用户数据包协议),它是属于传输层并
数据报组件的使用在网络传输中相当普遍,该组件的传输是基于UDP的传输协议,一种无连接的传输层协议。UDP传输协议在传输过程中,一次只传送少量数据、不可靠的网络应