📄 testrecv.c
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>#include <unistd.h>#include "../pmtp.h"uint8_t recv_buffer_test (int s);int main () { struct sockaddr_in s_in; struct ip_mreqn m_in; int s; if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { perror ("socket"); exit (1); } memset(&m_in, 0, sizeof (m_in)); if (!inet_aton ("232.5.5.5", &(m_in.imr_multiaddr))) { fprintf (stderr, "Couldn't convert 232.5.5.5\n"); } if (setsockopt (s, SOL_IP, IP_ADD_MEMBERSHIP, &m_in, sizeof (m_in))) { perror ("setsockopt"); exit (1); } memset ((char *) &s_in, 0, sizeof (s_in)); s_in.sin_family = AF_INET; s_in.sin_port = htons (PMTP_PORT); if (bind(s, (struct sockaddr *) &s_in, sizeof (s_in)) < 0) { perror ("bind"); exit (1); } recv_buffer_test(s); close (s); return 1;}uint8_t recv_buffer_test (int s) { struct sockaddr_in s_in; int n, i, count = 0; unsigned char packet[1500]; unsigned char source[20], *source_ptr; unsigned int port; int len = sizeof (s_in); while (count < 1400 && (n = recvfrom (s, packet, sizeof(packet), 0, (struct sockaddr *) &s_in, &len))) { count++; port = ntohs (s_in.sin_port); source_ptr = inet_ntoa (s_in.sin_addr); strncpy (source, source_ptr, 16); source[15] = 0; if (n != count) { printf ("Received from %s:%u packet %u with %u bytes\n", source, port, count, n); exit (1); } for (i = 0; i < count; i++) { if (packet[i] != (count & 0XFF)) { printf ("Received from %s:%u packet %u with %u bytes, wrong test pattern\n", source, port, count, n); exit (1); } } } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -