Skip to content

Instantly share code, notes, and snippets.

@linneman
Created December 13, 2018 19:26
Show Gist options
  • Select an option

  • Save linneman/ff7bb57877b6687cdbdc49c14e6cbd79 to your computer and use it in GitHub Desktop.

Select an option

Save linneman/ff7bb57877b6687cdbdc49c14e6cbd79 to your computer and use it in GitHub Desktop.
/*
check out whether telephony speech voice data packets of 32 ms period length
get scrambled when send out via UDP.
When the packages are send in realtime that is with a delay of 32 ms between each send
operation, this was not the case on the used test system Ryzen 1700X running Linux kernel
4.19.5. Otherwise we observe in fact a change of the send order.
12/2019/OL
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define vss_debug printf
#define vss_error printf
#define vss_message printf
#define PORT 9000
static int udp_open_device( const char* addr_str, const int port, struct sockaddr_in* addr )
{
const int server = ( strlen( addr_str ) == 0 ) ||
!strcmp( addr_str, "0.0.0.0" ) ||
!strcmp( addr_str, "127.0.0.1" );
int fd = -1;
memset( (char *) addr, 0, sizeof( struct sockaddr_in ) );
addr->sin_family = AF_INET;
addr->sin_port = htons( port );
if( server ) {
vss_debug( "%s, %d: open udp server at port %d\n", __func__, __LINE__, port );
addr->sin_addr.s_addr = htonl(INADDR_ANY);
} else {
vss_debug( "%s, %d: open udp client to %s at port %d\n",
__func__, __LINE__, addr_str, port );
if( inet_aton( addr_str , & addr->sin_addr ) == 0 ) {
vss_error( "%s, %d: inet_aton() failed error!\n", __func__, __LINE__ );
return -1;
}
}
if( ( fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
return -1;
}
if( server ) {
if( bind( fd, (struct sockaddr *)addr, sizeof(struct sockaddr) ) < 0 ) {
vss_error( "%s, %d: could not bind to soccket at address: %s, port %d\n",
__func__, __LINE__, addr_str, port );
close( fd );
return -1;
}
}
return fd;
}
int send_test( const char* client_ip, const int port )
{
int fd, retval = 0;
struct sockaddr_in addr;
char buf[1072];
long n;
const long max_n = 1000000;
int* p_val = (int *)buf, val = 0;
memset( buf, 0, sizeof( buf ) );
fd = udp_open_device( client_ip, port, & addr );
if( fd <= 0 ) {
vss_error( "%s,%d: cound not open udp socket error!\n", __func__, __LINE__ );
return -1;
}
for( n =0; n<max_n; ++n ) {
*p_val = val++;
retval = sendto( fd, buf, sizeof(buf), 0 /* flags */,
(const struct sockaddr *) &addr, sizeof( addr ) );
if( retval < 0 ) {
vss_error( "%s,%d: sendto returned %d error!\n", __func__, __LINE__, retval );
} else {
// printf("%s,%d: successfully send frame %ld\n", __func__, __LINE__, n );
}
if( ! (n%10) )
printf( "%ld frames processed.\n", n );
usleep( 32000 );
}
return 0;
}
int receive_test( const int port )
{
int fd, retval = 0;
struct sockaddr_in addr;
char buf[1072];
long n;
const long max_n = 1000000;
int* p_val = (int *)buf;
int oldval = 0;
memset( buf, 0, sizeof( buf ) );
fd = udp_open_device( "0.0.0.0", port, & addr );
if( fd <= 0 ) {
vss_error( "%s,%d: cound not open udp socket error!\n", __func__, __LINE__ );
return -1;
}
for( n = 0; n < max_n; ++n ) {
socklen_t len = (socklen_t)sizeof( addr );
retval = recvfrom( fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr *) &addr, &len );
if( retval < 0 ) {
vss_error( "%s,%d: sendto returned %d error!\n", __func__, __LINE__, retval );
} else {
if( n != 0 && *p_val - oldval != 1 )
vss_error( "%s,%d: package order mismatch error, expect: %d, got: %d!\n",
__func__, __LINE__, oldval+1, *p_val );
}
if( ! (n%10) )
printf( "%ld frames processed.\n", n );
oldval = *p_val;
}
return 0;
}
/* invocation:
on the host: ./a.out
on the client: ./a.out -a <client ip address>
*/
int main( int argc, char* argv[] )
{
if( argc > 2 && strstr( argv[1], "-s" ) != NULL )
return send_test( argv[2], PORT );
else
return receive_test( PORT );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment