Lab 3: TCP vs UDP Protocol Implementation
Exercise 1: TCP Implementation
TCP Server Code (ESP32)
#include <WiFi.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
const char* ssid = "your-network-name";
const char* password = "your-password";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start TCP server
server.begin();
Serial.println("TCP Server started on port 80");
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New TCP client connected");
while (client.connected()) {
if (client.available()) {
String request = client.readStringUntil('\n');
Serial.println("Received: " + request);
// Echo back with timestamp
String response = "Echo: " + request + " [" + String(millis()) + "]\n";
client.print(response);
// Send sensor data simulation
float temperature = 20.0 + random(0, 100) / 10.0;
float humidity = 40.0 + random(0, 400) / 10.0;
String sensorData = "SENSOR,TEMP:" + String(temperature) +
",HUM:" + String(humidity) + "\n";
client.print(sensorData);
}
}
client.stop();
Serial.println("TCP client disconnected");
}
delay(100);
}
TCP Client Code (ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
const char* ssid = "your-network-name";
const char* password = "your-password";
const char* serverIP = "192.168.1.100"; // Server ESP32 IP
const int serverPort = 80;
WiFiClient client;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (!client.connected()) {
Serial.println("Connecting to TCP server...");
if (client.connect(serverIP, serverPort)) {
Serial.println("Connected to TCP server");
} else {
Serial.println("Connection failed");
delay(5000);
return;
}
}
// Send data to server
String message = "Hello from TCP client " + String(millis());
client.println(message);
Serial.println("Sent: " + message);
// Read response
unsigned long timeout = millis() + 5000;
while (client.available() == 0 && millis() < timeout) {
delay(10);
}
if (client.available()) {
String response = client.readStringUntil('\n');
Serial.println("Received: " + response);
}
delay(2000);
}
Exercise 2: UDP Implementation
UDP Server Code (ESP32)
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "your-network-name";
const char* password = "your-password";
WiFiUDP udp;
const int udpPort = 1234;
char packetBuffer[255];
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start UDP server
udp.begin(udpPort);
Serial.println("UDP Server started on port " + String(udpPort));
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
Serial.print("Received UDP packet of size ");
Serial.print(packetSize);
Serial.print(" from ");
Serial.print(udp.remoteIP());
Serial.print(":");
Serial.println(udp.remotePort());
// Read packet
int len = udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents: " + String(packetBuffer));
// Send response back
udp.beginPacket(udp.remoteIP(), udp.remotePort());
// Send sensor data
float temperature = 20.0 + random(0, 100) / 10.0;
float humidity = 40.0 + random(0, 400) / 10.0;
unsigned long timestamp = millis();
String response = "SENSOR_DATA,TEMP:" + String(temperature) +
",HUM:" + String(humidity) +
",TIME:" + String(timestamp);
udp.print(response);
udp.endPacket();
Serial.println("Sent response: " + response);
}
delay(10);
}
Exercise 3: Protocol Comparison
Compare the performance and reliability characteristics of TCP vs UDP by running both implementations and analyzing:
- Connection establishment time
- Data transfer speed
- Packet loss handling
- Resource usage (memory, CPU)
Analysis Questions
- Compare the connection establishment overhead between TCP and UDP
- How does packet loss affect each protocol differently?
- In what IoT scenarios would you choose TCP over UDP and vice versa?
- How do the protocols perform under network congestion?
- What are the memory and processing requirements for each protocol?
Lab Deliverables
- Working TCP client-server implementation
- Working UDP client-server implementation
- Performance comparison results and analysis
- Wireshark captures showing protocol differences
- Recommendations for protocol selection in different IoT scenarios