tcp/quic lab patched

This commit is contained in:
2026-01-10 10:54:46 +08:00
parent 33c8e3f3da
commit b09a29b7e8
11 changed files with 618 additions and 8 deletions

View File

@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define PORT 8081
#define SERVER_IP "100.115.45.1" // Default IP, change if needed
#define BUFFER_SIZE 4096
#define TOTAL_MB 100
#define NUM_CONNS 5
#define MB_PER_CONN (TOTAL_MB / NUM_CONNS)
void *send_data(void *arg) {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE];
memset(buffer, 'B', BUFFER_SIZE);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return NULL;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return NULL;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return NULL;
}
long long bytes_to_send = (long long)MB_PER_CONN * 1024 * 1024;
long long bytes_sent = 0;
while (bytes_sent < bytes_to_send) {
int to_send = (bytes_to_send - bytes_sent > BUFFER_SIZE) ? BUFFER_SIZE : (bytes_to_send - bytes_sent);
send(sock, buffer, to_send, 0);
bytes_sent += to_send;
}
close(sock);
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t threads[NUM_CONNS];
printf("Starting %d TCP connections, sending %d MB each (Total %d MB).\n", NUM_CONNS, MB_PER_CONN, TOTAL_MB);
for (int i = 0; i < NUM_CONNS; i++) {
if (pthread_create(&threads[i], NULL, send_data, NULL) != 0) {
perror("Thread create failed");
return 1;
}
}
for (int i = 0; i < NUM_CONNS; i++) {
pthread_join(threads[i], NULL);
}
printf("All connections finished sending.\n");
return 0;
}