23,118
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#include <io.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#define closesocket close
#endif
#define BUF_SIZE 0x1000
char * host_name = "224.1.1.3";
int port = 12345;
int main(int argc, char* argv[])
{
int loop = 1;
/* 多播循环 */
int iter = 0;
int from_len;
char buffer[BUF_SIZE];
int socket_descriptor;
struct ip_mreq command;
struct sockaddr_in loc;
struct sockaddr_in rem;
int rec_len =0;
int ptr_point = 0;
size_t bytes_written = 0;
int loop_times = 0;
int rcv_port_0 = 0;
int rcv_port_1 = 0;
int first = 0;
#ifdef _WIN32
WSADATA wsaData;
if( WSAStartup(MAKEWORD(2,2), &wsaData) != 0 )
{
printf("Error in WSAStartup\n");
exit(1);
}
#endif
if (argv[1])
{
host_name = argv[1];
printf("%s \n", host_name);
}
if (argv[2])
{
port = atoi(argv[2]);
printf("%d \n", port);
}
memset(&loc, 0, sizeof(loc));
loc.sin_family = AF_INET;
loc.sin_addr.s_addr = inet_addr(host_name);
loc.sin_port = htons(port);
if((socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
/*can use the same port in the pc*/
loop = 1;
if(setsockopt(socket_descriptor, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
{
perror("setsockopt:SO_REUSEADDR.\n");
exit(EXIT_FAILURE);
}else
{
printf("setsockopt:SO_REUSEADDR.\n");
}
loop = 0x1000000;
if( setsockopt( socket_descriptor, SOL_SOCKET, SO_RCVBUF, (void *) &loop, sizeof( loop ) ) < 0 )
{
perror("setsockopt:SO_RCVBUF.\n");
exit(EXIT_FAILURE);
}
#if defined( WIN32 ) || defined( UNDER_CE )
/*
* Under Win32 and for multicasting, we bind to INADDR_ANY.
* This is of course a severe bug, since the socket would logically
* receive unicast traffic, and multicast traffic of groups subscribed
* to via other sockets. How this actually works in Winsock, I don't
* know.
*/
if( IN_MULTICAST( ntohl( loc.sin_addr.s_addr ) ) )
{
struct sockaddr_in stupid = loc;
stupid.sin_addr.s_addr = INADDR_ANY;
if( bind( socket_descriptor, (struct sockaddr *)&stupid, sizeof( stupid ) ) < 0 )
{
perror("bind socket.\n");
close( socket_descriptor );
return 0;
}
}
else
#endif
if(bind(socket_descriptor, (struct sockaddr *)&loc, sizeof(loc)) < 0)
{
perror("bind socket.\n");
exit(EXIT_FAILURE);
}
{
printf("%s,%d\n", host_name, port);
command.imr_multiaddr.s_addr = inet_addr(host_name);
command.imr_interface.s_addr = htonl(INADDR_ANY);
if(command.imr_multiaddr.s_addr == -1)
{
perror("224.1.1.3 not a legal multicast address.\n");
exit(EXIT_FAILURE);
}
if (setsockopt(socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
{
perror("setsockopt:IP_ADD_MEMBERSHIP. error\n");
}
}
printf("start reciving...\n");
from_len = sizeof(rem);
while(1)
{
memset(&rem, 0, from_len);
memset(buffer, 0, BUF_SIZE);
if( (rec_len = recvfrom(socket_descriptor, buffer, sizeof(char) * BUF_SIZE, 0, (struct sockaddr *)&rem, &from_len)) == -1) {
perror("recvfrom.\n");
}
printf( "From host:%s port:%d, len: %d\n", inet_ntoa(rem.sin_addr), ntohs(rem.sin_port), rec_len);
}
if(setsockopt(socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP, &command, sizeof(command)) < 0) {
perror("setsockopt:IP_DROP_MEMBERSHIP.\n");
}
closesocket(socket_descriptor);
exit(EXIT_SUCCESS);
}