点击(此处)折叠或打开
-
#include <sys/types.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
#ifdef WIN_DEVCPP
-
#include <winsock.h>
-
#include <io.h>
-
#pragma comment(lib,"ws2_32.lib")
-
int WSAStartup
-
(
-
WORD wVersionRequested,
-
LPWSADATA lpWSAData
-
);
-
typedef unsigned short in_port_t;
-
#else
-
#include <sys/socket.h>
-
#include <arpa/inet.h>
-
-
#define TRUE (1)
-
#define FALSE (0)
-
#define ERROR (-1)
-
typedef unsigned long BOOL;
-
#endif /* WIN_DEVCPP */
-
-
#define BUFLEN 255
-
#define TC_BOARD_IP "192.168.2.3"
-
#define TC_HOST_IP "192.168.2.253"
-
#define TC_BOARD_PORT 7602
-
#define TC_HOST_PORT 7601
-
-
#define FIRST_SHAKE "the first handshake"
-
#define SECOND_SHAKE "the second handshake"
-
#define THIRD_SHAKE "the third handshake"
-
-
/*********************************************************************
-
*filename: eth_test.c
-
*********************************************************************/
-
#ifdef WIN_DEVCPP
-
int inet_aton( const char * pString, struct in_addr * inetAddress )
-
{
-
u_long rtnAddress;
-
-
if (inetAddress == NULL)
-
return (ERROR);
-
-
rtnAddress = inet_addr (pString);
-
inetAddress->s_addr = rtnAddress;
-
return (0);
-
}
-
#endif /* WIN_DEVCPP */
-
-
int main(int argc, char **argv)
-
{
-
#ifdef WIN_DEVCPP
-
WSADATA wsaData;
-
#endif /* WIN_DEVCPP */
-
int retVal;
-
struct sockaddr_in to;
-
struct sockaddr_in from;
-
struct sockaddr_in local;
-
int sockfd;
-
char buff[BUFLEN + 1];
-
struct in_addr localIpAddr;
-
struct ip_mreq mreq;
-
int pkgLen;
-
BOOL isMultiCAST;
-
struct in_addr targetIp;
-
struct in_addr selfIp;
-
-
printf("argc=%d\n",argc);
-
-
#ifdef WIN_DEVCPP
-
retVal = WSAStartup( MAKEWORD(2, 2), &wsaData );
-
if (retVal == SOCKET_ERROR) {
-
printf("WSAStartup error\n");
-
exit(33);
-
}
-
#endif /* WIN_DEVCPP */
-
-
if ((argc == 2) && (0 == strcmp(argv[1], "help"))) {
-
printf("Usage: mcast_server [targetIp [selfIp]]\n");
-
printf("default: mcast_server %s %s\n", TC_BOARD_IP, TC_HOST_IP);
-
exit (0);
-
}
-
-
if (argc >= 2) {
-
if (0 > inet_aton(argv[1], &targetIp))
-
exit (1);
-
} else {
-
if (0 > inet_aton(TC_BOARD_IP, &targetIp))
-
exit (2);
-
}
-
-
if (0xe == (htonl(targetIp.s_addr) >> 28)) {
-
isMultiCAST = TRUE;
-
printf("## multi-cast ##\n");
-
} else {
-
isMultiCAST = FALSE;
-
printf("## uni-cast ##\n");
-
}
-
-
if (argc >= 3) {
-
if (0 > inet_aton(argv[2], &selfIp))
-
exit (3);
-
} else {
-
if (0 > inet_aton(TC_HOST_IP, &selfIp))
-
exit (4);
-
}
-
-
printf("set targetIp to %s, port to %d\n", (char *)inet_ntoa(targetIp), TC_BOARD_PORT);
-
printf("set selfIp to %s, port to %d\n", (char *)inet_ntoa(selfIp), TC_HOST_PORT);
-
-
/* create socket */
-
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
-
if (sockfd < 0) {
-
perror("socket");
-
printf("socket creating error\n");
-
exit(1);
-
}
-
-
/* set self info */
-
memset(&local, 0, sizeof(local));
-
local.sin_family = AF_INET;
-
local.sin_port = htons(TC_HOST_PORT);
-
local.sin_addr.s_addr = INADDR_ANY;
-
-
/*bind socket*/
-
printf("bind socket to %s:%d\n", (char *)inet_ntoa(selfIp), TC_HOST_PORT);
-
retVal = bind(sockfd,(struct sockaddr*)&local, sizeof(local));
-
if (retVal < 0) {
-
perror("bind");
-
printf("bind error!\n");
-
close(sockfd);
-
exit(5);
-
}
-
-
if (isMultiCAST) {
-
/* join multi-cast group */
-
mreq.imr_multiaddr = targetIp;
-
mreq.imr_interface = selfIp;
-
-
retVal = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
-
if (retVal == ERROR) {
-
perror("ADD_MEMBERSHIP");
-
printf("setsockopt join error!\n");
-
close(sockfd);
-
exit(6);
-
}
-
-
/* enable multi-cast */
-
localIpAddr = selfIp;
-
retVal = setsockopt (sockfd, IPPROTO_IP, IP_MULTICAST_IF,
-
(char *)&localIpAddr, sizeof(localIpAddr));
-
if (retVal == ERROR) {
-
perror("MULTICAST_IF");
-
printf("setsockopt enable multi-cast error!\n");
-
close(sockfd);
-
exit(7);
-
}
-
}
-
-
/* set dst info */
-
memset(&to, 0, sizeof(to));
-
to.sin_family = AF_INET;
-
to.sin_port = htons(TC_BOARD_PORT);
-
to.sin_addr = targetIp;
-
-
/* send the first handshake */
-
retVal = sendto(sockfd, FIRST_SHAKE, sizeof(FIRST_SHAKE), 0, (struct sockaddr *)&to, sizeof(to));
-
if (retVal == ERROR) {
-
perror("sendto");
-
printf("sendto error!\n");
-
close(sockfd);
-
exit(8);
-
}
-
-
printf(">>>> [%s:%d] %s is sent >>>>\n",
-
(char *)inet_ntoa(to.sin_addr), htons(to.sin_port), FIRST_SHAKE);
-
-
/* receive the second handshake */
-
do {
-
memset(buff, 0, sizeof(buff));
-
memset(&from, 0, sizeof(from));
-
pkgLen = sizeof(from);
-
retVal = recvfrom(sockfd, buff, sizeof(buff) - 1, 0,
-
(struct sockaddr*)&from, &pkgLen);
-
if (retVal < 0) {
-
perror("recvfrom");
-
printf("recvfrom error!\n");
-
close(sockfd);
-
exit(9);
-
}
-
-
printf("received msg: len=%d, ip=%s, port=%d, msg=%s\n", retVal,
-
(char *)inet_ntoa(from.sin_addr), htons(from.sin_port), buff);
-
} while ((retVal != sizeof(SECOND_SHAKE)) || (0 != strcmp(buff, SECOND_SHAKE)));
-
-
printf("<<<< [%s:%d] %s is received <<<<\n",
-
(char *)inet_ntoa(from.sin_addr), htons(from.sin_port), SECOND_SHAKE);
-
-
/* send the first handshake */
-
retVal = sendto(sockfd, THIRD_SHAKE, sizeof(THIRD_SHAKE), 0,
-
(struct sockaddr *)&to, sizeof(to));
-
if (retVal == ERROR) {
-
perror("sendto");
-
printf("sendto error!\n");
-
close(sockfd);
-
exit(10);
-
}
-
-
printf(">>>> [%s:%d] %s is sent >>>>\n",
-
(char *)inet_ntoa(to.sin_addr), htons(to.sin_port), THIRD_SHAKE);
-
-
if (isMultiCAST)
-
{
-
retVal = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
-
if (retVal == ERROR) {
-
perror("DROP_MEMBERSHIP");
-
printf("setsockopt join error!\n");
-
close(sockfd);
-
exit(11);
-
}
-
}
-
-
close(sockfd);
- }