这个程序需要绑定两个端口:
1.通过端口复用的方法,绑定同一个端口;
2.接收和发送用不同的端口,在发送函数填入组播地址和发送端的端口号。
#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 10
SOCKET 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);
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);
printf("from address:%s\n", inet_ntoa(addrBro.sin_addr));
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;
optval = 1;
if (setsockopt(socketMul, SOL_SOCKET, SO_REUSEADDR, (char FAR *)&optval,
sizeof(optval)) == SOCKET_ERROR)
{
printf("setsockopt failed:%d", WSAGetLastError());
closesocket(socketMul);
WSACleanup();
return;
}
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()
{
CHAR sendbuf[BUFSIZE];
DWORD i;
int ret;
mulControl();
addrMul.sin_port = htons(mPort);
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;
CHAR 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();
}
else
{
multicastRec();
return 0;
}
}
return 0;
}