you're doing an echo (both way traffic) for too short periods. Iperf3 is doing a one way, single tcp stream over 10 seconds. that's not the same thing.
try coding just sending data from client to server for 10 seconds.
For instance, I've an old code here:
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <arpa/inet.h>
#include <stdio.h>
#define PORT 8192
#define TIMEOUT 10
int client;
void sendReply();
void closeConnection();
int main()
{
signal(SIGCHLD, SIG_IGN);
//
struct sockaddr_in6 serverInfo;
memset(&serverInfo, 0, sizeof serverInfo);
serverInfo.sin6_family = AF_INET6;
serverInfo.sin6_addr = in6addr_any;
serverInfo.sin6_port = htons(PORT);
// socket creation
int server = socket(AF_INET6, SOCK_STREAM, 0);
int yes = 1;
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
bind(server, (struct sockaddr *) &serverInfo, sizeof serverInfo);
listen(server, 20);
printf("listening on port %d\n",PORT);
char clientip[INET6_ADDRSTRLEN];
for(;;)
{
struct sockaddr_in6 clientInfo;
int clientInfoSize = sizeof clientInfo;
client = accept(server, (struct sockaddr *) &clientInfo, &clientInfoSize);
inet_ntop(clientInfo.sin6_family, &clientInfo.sin6_addr, clientip, sizeof(clientip));
printf("incoming request from %s\n",clientip);
if (fork() == 0)
{
sendReply(client);
}
}
return 0;
}
void sendReply()
{
// stop after TIMEOUT seconds
signal(SIGALRM, closeConnection);
alarm(TIMEOUT);
// send HTTP headers
char headers[] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: octet-stream\r\n"
"Cache-Control: no-cache, no-transform\r\n"
"\r\n";
ssize_t rc = write(client, headers, strlen(headers));
// send random data
#define BUFFSIZE (4096)
char buffer[BUFFSIZE];
memset(&buffer, 0, sizeof buffer);
srand(time(NULL));
// Get buffer size
int sendbuff;
socklen_t optlen = sizeof(sendbuff);
int res = getsockopt(client, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);
if(res == -1)
printf("Error getsockopt one");
else
printf("send buffer size = %d\n", sendbuff);
for(;;)
{
// uncomment to send random data, cpu might affect performance
// int i;
// for(i = 0; i < BUFFSIZE; i++)
// {
// buffer[i] = rand() & 255;
// }
if(write(client, buffer, sizeof buffer) < 0)
{
closeConnection();
}
}
}
void closeConnection()
{
shutdown(client, 2);
//close(client);
exit(0);
}
this is a small http server that will send data during 10 seconds then stop.
on the client do:
wget -o /dev/null http://ip_of_server:8192
or use curl
this should yield results similar to iperf3.