Exemplary transmitting and receiving socket UDP broadcast

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/mao0514/article/details/89203203

Network communications infrastructure

If an application on the network to communicate with each other two hosts, one should know each other's IP, the other to know that the program can monitor the port. Because the program on the same host using the network through the port number to distinguish.

UDP Socket of use:

1. initialize network library

2. Create a SOCK_DGRAM type of Socket.

3. Bind the socket.

4. send and receive data.

The destruction of the socket.

6. Release Network Library.

Broadcast packets principles:

A dedicated address for simultaneous transmission is called a broadcast address to all the network stations. Using TCP / IP network protocol, the host ID of the host identifies that segment 1 is the full IP address is a broadcast address. If your IP address is: 192.168.1.39, subnet mask: 255.255.255.0, the broadcast address is: 192.168.1.255; if the IP address is 192.168.1.39, subnet mask: 255.255.255.192, then the broadcast address : 192.168.1.63.

If you want to broadcast data within the network, long packets can be transmitted to the broadcast address, the packet can be routed via the router it will reach all hosts on the segment, also called a direct broadcast such broadcasts; if want to broadcast data across the network, 255.255.255.255 To send a packet, this packet will not be routed, it can reach all hosts on this physical network, such broadcasts called the limited broadcast.

Transmitted using the UDP protocol, the broadcast packet receiving process.

If we want to 192.168.0.X, subnet mask: 255.255.255.0 subnet broadcast packets sent in.

It comprises the following steps:

1. Initialize Winsock library.

2. Create SOCK_DIRAM type of Socket.

3. Set Socket property allows its broadcast.

4. sends the packet to 192.168.0.255

The broadcast their receive broadcast packets.

6. Close Socket

7. Release the network library.

Note the following:

1. The recipient must know the slogan broadcast side, then bind this port number for correct reception.

2. Socket recipient does not need to broadcast a set property.

3. Bound IP can not use "127.0.0.1", you can use the real IP address, or INADDR_ANY. Otherwise reception failed.

   ON =. 1 int const;
    our sock = Socket (PF_INET, SOCK_DGRAM, 0);
    the setsockopt (our sock, SOL_SOCKET, SO_BROADCAST is, (char *) ON, the sizeof (ON)); // Set Socket Option

send:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
int main(){
	char msg[128] = "I am broadCast message from server!";
	int brdcFd;
	if((brdcFd = socket(PF_INET, SOCK_DGRAM, 0)) == -1){
		printf("socket fail\n");
		return -1;
	}
	int optval = 1;//这个值一定要设置,否则可能导致sendto()失败
	setsockopt(brdcFd, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &optval, sizeof(int));
	struct sockaddr_in theirAddr;
	memset(&theirAddr, 0, sizeof(struct sockaddr_in));
	theirAddr.sin_family = AF_INET;
	theirAddr.sin_addr.s_addr = inet_addr("255.255.255.255");
	theirAddr.sin_port = htons(4001);
	int sendBytes;
	if((sendBytes = sendto(brdcFd, msg, strlen(msg), 0,
			(struct sockaddr *)&theirAddr, sizeof(struct sockaddr))) == -1){
		printf("sendto fail, errno=%d\n", errno);
		return -1;
	}
	printf("msg=%s, msgLen=%d, sendBytes=%d\n", msg, strlen(msg), sendBytes);
	close(brdcFd);
	return 0;
}

receive

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<arpa/inet.h>
int main(){
	int sockListen;
	if((sockListen = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
		printf("socket fail\n");
		return -1;
	}
	int set = 1;
	setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(int));
	struct sockaddr_in recvAddr;
	memset(&recvAddr, 0, sizeof(struct sockaddr_in));
	recvAddr.sin_family = AF_INET;
	recvAddr.sin_port = htons(4001);
	recvAddr.sin_addr.s_addr = INADDR_ANY;
	// 必须绑定,否则无法监听
	if(bind(sockListen, (struct sockaddr *)&recvAddr, sizeof(struct sockaddr)) == -1){
		printf("bind fail\n");
		return -1;
	}
	int recvbytes;
	char recvbuf[128];
	int addrLen = sizeof(struct sockaddr_in);
	if((recvbytes = recvfrom(sockListen, recvbuf, 128, 0,
		(struct sockaddr *)&recvAddr, &addrLen)) != -1){
		recvbuf[recvbytes] = '\0';
		printf("receive a broadCast messgse:%s\n", recvbuf);
	}else{
		printf("recvfrom fail\n");
	}
	close(sockListen);
	return 0;
}

 

 

Guess you like

Origin blog.csdn.net/mao0514/article/details/89203203