-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedynet-1.1.2.c
112 lines (90 loc) · 3.19 KB
/
speedynet-1.1.2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <time.h>
#include <signal.h>
#define BUFFER_SIZE 1024
#define TEST_DURATION 10
int sockfd;
void cleanup() {
if (sockfd >= 0) {
close(sockfd);
}
}
void sigIntHandler(int signo) {
printf("\nTest interrupted.\n");
cleanup();
exit(0);
}
void displayIntro() {
printf("\n");
printf("╔══╗──────╔╗─╔═╦╗─╔╗\n");
printf("║══╬═╦═╦═╦╝╠╦╣║║╠═╣╚╗\n");
printf("╠══║╬║╩╣╩╣╬║║║║║║╩╣╔╣\n");
printf("╚══╣╔╩═╩═╩═╬╗╠╩═╩═╩═╝\n");
printf("───╚╝──────╚═╝\n");
printf("************************************\n");
printf("* SpeedyNet *\n");
printf("* Internet Speed Test Utility *\n");
printf("************************************\n");
printf("\n");
printf("With love from Chile\n");
printf("Developed by Felipe Alfonso González - Computer Science Engineer\n");
printf("Contact: [email protected]\n");
printf("GitHub: github.com/felipealfonsog\n");
printf("\n");
printf("This software is licensed under the MIT License.\n");
printf("\n");
}
int performSpeedTest(const char *selected_server) {
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
struct timespec start_time, end_time;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket for speed test");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr.s_addr = inet_addr(selected_server);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error connecting to server for speed test");
cleanup();
return 1;
}
clock_gettime(CLOCK_MONOTONIC, &start_time);
printf("Performing speed test...\n");
struct sigaction sa;
sa.sa_handler = sigIntHandler;
sigaction(SIGINT, &sa, NULL);
int total_bytes_received = 0;
for (int i = 0; i < TEST_DURATION * 10; i ) {
int send_result = send(sockfd, buffer, sizeof(buffer), 0);
int recv_result = recv(sockfd, buffer, sizeof(buffer), 0);
if (send_result < 0 || recv_result < 0) {
perror("Error sending/receiving data during speed test");
cleanup();
return 1;
}
total_bytes_received = recv_result;
}
clock_gettime(CLOCK_MONOTONIC, &end_time);
double total_time = (end_time.tv_sec - start_time.tv_sec) (end_time.tv_nsec - start_time.tv_nsec) / 1e9;
double download_speed = (double)(total_bytes_received) / (total_time * 1024 * 1024);
printf("\nTest completed.\n");
printf("Selected Server: %s\n", selected_server);
printf("Download Speed: %.2f Mbps\n", download_speed);
cleanup();
return 0;
}
// ... (unchanged)
int main() {
// ... (unchanged)
return performSpeedTest(selected_server);
}