当前位置:   article > 正文

linux驱动.之 网络udp应用层测试工具demon(一)_linux udp server 工具

linux udp server 工具

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

客户端udp_client.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 *)&interface, sizeof(interface))  < 0) // 绑定vlan网卡设备
	{
		perror("SO_BINDTODEVICE failed");
	}

	struct sockaddr_in addr;
	memset(&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 *)&addr, sizeof(addr));
	if (ret < 0) {
		printf("%s: bind 198.18.36.3 failed\n", __FUNCTION__);
		close(sock);
		return 0;
	}
	else
	printf("bind 198.18.36.3 success \r\n");
	
    struct sockaddr_in server_addr;
    memset(&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 *)&server_addr, sizeof(server_addr));
		printf("sendto 198.18.36.1 ret:%d\n", ret);
		usleep(200000);
	}
	
	close(sock);
	return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

服务端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 *)&interface, sizeof(interface))  < 0)  // 绑定vlan网卡设备
	{
		perror("SO_BINDTODEVICE failed");
	}

	struct sockaddr_in server_addr;
    memset(&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 *)&server_addr, sizeof(server_addr));
	if (ret < 0) 
    {
		printf("%s: bind 198.16.37.4 failed\n", __FUNCTION__);
		close(sock);
		return 0;
	}
	else
	printf("bind 198.16.37.4 success \r\n");
	
	struct sockaddr_in client_addr;
	socklen_t	len;
	len = sizeof(client_addr);
	while(1)
	{
		ret = recvfrom(sock, buf, 1024, 0,(struct sockaddr *)&client_addr, &len);
		printf("recvfrom 198.16.37.3 ret:%d\n", ret);
	}
	
	close(sock);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/583641
推荐阅读
相关标签
  

闽ICP备14008679号