1. 程式人生 > >socket程式設計:UDP廣播(C/S)

socket程式設計:UDP廣播(C/S)

便於有需要的小夥伴測試使用(linux環境C語言):

UDP Broadcast Server,接受Client端傳送的廣播資訊,以及獲取並列印Client的IP地址,在接收到done後停止並退出。

大致流程是建立一個socket並繫結一個INADDR_ANY的本地地址,接受訊息。

/*######udp broadcast server######*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>


int main(void)
{
	int sock;
	struct sockaddr_in own_addr, peer_addr;
	const int opt = 1;
	char recv_msg[100] = {0};
	socklen_t peer_addrlen = 0;
	char peer_name[30] = {0};
	int ret = 0;


	bzero(&own_addr, sizeof(struct sockaddr_in));
	bzero(&peer_addr, sizeof(struct sockaddr_in));
	own_addr.sin_family = AF_INET;
	own_addr.sin_port = htons(6868);
	own_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
		printf("Ceate sock fail\n");

//	ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
//	if (ret)
//		printf("Set sock to broadcast format fail\n");
	ret = bind(sock, (struct sockaddr *)&own_addr, sizeof(struct sockaddr_in));
	if (ret == -1)
		printf("Bind addr fail\n");

	while (1) {
		ret = recvfrom(sock, recv_msg, sizeof(recv_msg), 0,
				(struct sockaddr *)&peer_addr, &peer_addrlen);
		if (ret > 0) {
			inet_ntop(AF_INET, &peer_addr.sin_addr.s_addr,
							peer_name, sizeof(peer_name));
			printf("Recv from %s, msg[%s]\n", peer_name, recv_msg);
			if (!strcmp("done", recv_msg))
				break;
		} else
			printf("Recv msg err\n");

		bzero(recv_msg, sizeof(recv_msg));
	}

	printf("Done\n");
}

 

UDP Broadcast Client:向外廣播UDP包,退出時傳送“done”訊息。

大致流程是建立一個socket,並用setsockopt函式宣告這是一個broadcast型別的socket,然後通過這個socket向INADDR_BROADCAST("255.255.255.255")傳送訊息。

/*######udp broadcast client######*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int g_exit = 0;

static void sighandle(int sign)
{
	g_exit = 1;
}

static void send_msg(int sock, char *msg, int msg_len,
			struct sockaddr *peer_addr, socklen_t peer_addrlen)
{
	int ret = 0;

	ret = sendto(sock, msg, msg_len, 0,
				peer_addr, peer_addrlen);
		if (ret > 0) {
			printf("Send msg success\n");
		} else
			printf("Send msg err\n");
}


int main(void)
{
	int sock;
	struct sockaddr_in peer_addr;
	const int opt = 1;
	char msg[100] = "Msg from udp broadcast client!";
	socklen_t peer_addrlen = 0;
	int ret = 0;

	signal(SIGINT, sighandle);
	signal(SIGTERM, sighandle);

	bzero(&peer_addr, sizeof(struct sockaddr_in));
	peer_addr.sin_family = AF_INET;
	peer_addr.sin_port = htons(6868);
	peer_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
	peer_addrlen = sizeof(struct sockaddr_in);

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
		printf("Ceate sock fail\n");

	ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
	if (ret == -1)
		printf("Set sock to broadcast format fail\n");

	while (!g_exit) {
		send_msg(sock, msg, sizeof(msg),
				(struct sockaddr *)&peer_addr, peer_addrlen);
		sleep(1);
	}

	memset(msg, 0, sizeof(msg));
	memcpy(msg, "done", 5);
	send_msg(sock, msg, sizeof(msg),
			(struct sockaddr *)&peer_addr, peer_addrlen);
	printf("Done\n");
}