本文介绍: 绑定vlan网卡demon,如果有多个网卡,多个vlan网卡ip设置成一致,那就不能只简单绑定ip创建socket,将两个程序编译二进制执行程序,放到开发板运行,就行测试服务端udp_service.c客户端udp_client.c

绑定vlan网卡demon,如果有多个网卡,多个vlan,网卡的ip设置成一致,那就不能只简单绑定ip创建socket
需要绑定网卡设备

客户端udp_client.c


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h&gt;
#include <sys/types.h&gt;
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>

#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <netpacket/packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

 
#define BUF_SIZE    1024
#define INTERFAXENAME "mgbe3_0.14"

int sock;

int main(int argc, char *argv[])
{
    int ret = 0;
	char buf[1024];
	memset(buf,1,1024);
	
	if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
	{
		perror("socket");
		exit(1);
	}

	struct ifreq interface;
	strncpy(interface.ifr_ifrn.ifrn_name, INTERFAXENAME, sizeof(INTERFAXENAME));
	if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&amp;interface, sizeof(interface))  < 0) // 绑定vlan网卡设备
	{
		perror("SO_BINDTODEVICE failed");
	}

	struct sockaddr_in addr;
	memset(&amp;addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(31004);
	addr.sin_addr.s_addr = inet_addr("198.18.36.3");

	ret = bind(sock, (struct sockaddr *)&amp;addr, sizeof(addr));
	if (ret < 0) {
		printf("%s: bind 198.18.36.3 failedn", __FUNCTION__);
		close(sock);
		return 0;
	}
	else
	printf("bind 198.18.36.3 success rn");
	
    struct sockaddr_in server_addr;
    memset(&amp;server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(31004);
	server_addr.sin_addr.s_addr = inet_addr("198.18.36.1");
	
	while(1)
	{
		ret = sendto(sock, buf, 1024, 0,(struct sockaddr *)&amp;server_addr, sizeof(server_addr));
		printf("sendto 198.18.36.1 ret:%dn", ret);
		usleep(200000);
	}
	
	close(sock);
	return 0;
}


服务端udp_service.c


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>

#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <netpacket/packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

 
#define BUF_SIZE    1024
#define INTERFAXENAME "mgbe3_0.14"

int sock;

int main(int argc, char *argv[])
{
    int ret = 0;
	char buf[1024];
	memset(buf,1,1024);

	if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
	{
		perror("socket");
		exit(1);
	}

	struct ifreq interface;
	strncpy(interface.ifr_ifrn.ifrn_name, INTERFAXENAME, sizeof(INTERFAXENAME));
	if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&amp;interface, sizeof(interface))  < 0)  // 绑定vlan网卡设备
	{
		perror("SO_BINDTODEVICE failed");
	}

	struct sockaddr_in server_addr;
    memset(&amp;server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(31004);
	server_addr.sin_addr.s_addr = inet_addr("198.16.37.4");
	
	ret = bind(sock, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));
	if (ret < 0) 
    {
		printf("%s: bind 198.16.37.4 failedn", __FUNCTION__);
		close(sock);
		return 0;
	}
	else
	printf("bind 198.16.37.4 success rn");
	
	struct sockaddr_in client_addr;
	socklen_t	len;
	len = sizeof(client_addr);
	while(1)
	{
		ret = recvfrom(sock, buf, 1024, 0,(struct sockaddr *)&amp;client_addr, &amp;len);
		printf("recvfrom 198.16.37.3 ret:%dn", ret);
	}
	
	close(sock);
	return 0;
}

将两个程序编译成二进制执行程序,放到开发板运行,就行测试

原文地址:https://blog.csdn.net/rjszcb/article/details/134534236

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_8073.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注