/*
Test TCP read -> #define TEST_TCP
iperf.exe -c 192.168.1.122 -i 1 -t 25 -w 64k -p 5001
Test UDP read -> //#define TEST_TCP
iperf.exe -c 192.168.1.151 -i 1 -t 25 -w 320k -p 5001 -u -b 54M
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <myAP.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if 0 // ESP-32S
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#else
extern "C" {
#include <lwip/sockets.h>
}
#endif
#define TEST_TCP
#define BUFSIZE 2048
#define PORT 5001
//char ssid[] = "mynetwork"; // your network SSID (name)
//char pass[] = "mypassword"; // your network password (use for WPA, or use as key for WEP)
//int status = WL_IDLE_STATUS;
WiFiUDP Udp;
char server[] = "192.168.1.2";
int port = 5001;
uint8_t buf[BUFSIZE];
int sockfd, optval;
struct sockaddr_in serveraddr, clientaddr;
int clientaddr_len = sizeof(clientaddr);
struct hostent *hostp;
char *hostaddrp;
long datacount = 0;
uint32_t delta_time_ms, tik;
void setup() {
Serial.begin(38400);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println("WiFi Connect...");
delta_time_ms = millis();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
delta_time_ms = millis() - delta_time_ms;
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Time connect = ");
Serial.print(delta_time_ms);
Serial.println(" ms");
Serial.println("Wait data...");
#ifdef TEST_TCP
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // SOCK_STREAM, SOCK_DGRAM
#else
sockfd = socket(PF_INET, SOCK_DGRAM, 0); // SOCK_STREAM, SOCK_DGRAM
#endif
if (sockfd < 0) {
Serial.println("ERROR opening socket");
while (1);
}
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = PF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) {
Serial.println("ERROR on binding");
while (1);
}
#ifdef TEST_TCP
if (listen(sockfd, 1) < 0) {
Serial.println("ERROR listen");
while (1);
}
#endif
delta_time_ms = millis();
}
void loop() {
#ifdef TEST_TCP
int x = accept(sockfd, 0, 0);
while (x > 0) {
int n = read(x, buf, BUFSIZE);
if (n < 0) Serial.println("ERROR in recvfrom");
else if (n) {
datacount += n;
if (millis() - delta_time_ms >= 1000) {
Serial.print("Average: ");
if (datacount < 8192) {
Serial.print(datacount);
Serial.println(" bytes/sec");
}
else {
Serial.print(datacount / 1024);
Serial.println(" kbytes/sec");
}
datacount = 0;
delta_time_ms = millis();
}
}
else break;
}
#else
int n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, (socklen_t*)&clientaddr_len);
if (n < 0) Serial.println("ERROR in recvfrom");
else {
datacount += n;
if (millis() - delta_time_ms >= 1000) {
Serial.print("Average: ");
if (datacount < 8192) {
Serial.print(datacount);
Serial.println(" bytes/sec");
}
else {
Serial.print(datacount / 1024);
Serial.println(" kbytes/sec");
}
datacount = 0;
delta_time_ms = millis();
}
}
#endif
}