generated from eunomia-bpf/libbpf-starter-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
udp_server | ||
udp_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CC=gcc | ||
CFLAGS=-Wall | ||
LDFLAGS=-pthread | ||
TARGET_SERVER=udp_server | ||
TARGET_CLIENT=udp_client | ||
|
||
all: $(TARGET_SERVER) $(TARGET_CLIENT) | ||
|
||
$(TARGET_SERVER): udp_server.c | ||
$(CC) $(CFLAGS) -o $(TARGET_SERVER) udp_server.c $(LDFLAGS) | ||
|
||
$(TARGET_CLIENT): udp_client.c | ||
$(CC) $(CFLAGS) -o $(TARGET_CLIENT) udp_client.c $(LDFLAGS) | ||
|
||
clean: | ||
rm -f $(TARGET_SERVER) $(TARGET_CLIENT) | ||
|
||
.PHONY: all clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# test udp server and client | ||
|
||
## build | ||
|
||
```sh | ||
make | ||
``` | ||
|
||
## run | ||
|
||
```sh | ||
./udp_server | ||
UDP Server listening on port 9876 | ||
... | ||
Packets received in the last second: 560500 | ||
Packets received in the last second: 563273 | ||
Packets received in the last second: 575896 | ||
Packets received in the last second: 574643 | ||
Packets received in the last second: 564236 | ||
Packets received in the last second: 564961 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
#define PORT 9876 | ||
#define SERVER_IP "127.0.0.1" | ||
#define NUM_PACKETS 100000000 | ||
|
||
int main() { | ||
int sockfd; | ||
struct sockaddr_in serverAddr; | ||
char message[] = "Hello Server!"; | ||
|
||
// Create socket | ||
sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
if (sockfd < 0) { | ||
perror("Socket creation failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
// Set server address | ||
memset(&serverAddr, 0, sizeof(serverAddr)); | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_port = htons(PORT); | ||
serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP); | ||
|
||
// Send packets | ||
for (int i = 0; i < NUM_PACKETS; i++) { | ||
// printf("Sending packet %d\n", i + 1); | ||
if (sendto(sockfd, message, strlen(message), 0, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) { | ||
perror("Send failed"); | ||
break; | ||
} | ||
} | ||
|
||
close(sockfd); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <pthread.h> | ||
|
||
#define PORT 9876 | ||
#define BUFFER_SIZE 1024 | ||
|
||
volatile int num_packets = 0; // Global variable to track the number of packets | ||
|
||
// Function to monitor packets per second | ||
void *monitor_packets(void *arg) { | ||
while (1) { | ||
int current_count = num_packets; | ||
sleep(1); | ||
int new_count = num_packets; | ||
printf("Packets received in the last second: %d\n", new_count - current_count); | ||
} | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
int sockfd; | ||
struct sockaddr_in serverAddr, clientAddr; | ||
char buffer[BUFFER_SIZE]; | ||
socklen_t addr_size; | ||
pthread_t thread_id; | ||
|
||
// Create socket | ||
sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
if (sockfd < 0) { | ||
perror("Socket creation failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
// Set server address | ||
memset(&serverAddr, 0, sizeof(serverAddr)); | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_addr.s_addr = INADDR_ANY; | ||
serverAddr.sin_port = htons(PORT); | ||
|
||
// Bind socket | ||
if (bind(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) { | ||
perror("Bind failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
printf("UDP Server listening on port %d\n", PORT); | ||
|
||
// Start monitoring thread | ||
if (pthread_create(&thread_id, NULL, monitor_packets, NULL) != 0) { | ||
perror("Failed to create thread"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
while (1) { | ||
addr_size = sizeof(clientAddr); | ||
// Receive packets | ||
if (recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &clientAddr, &addr_size) < 0) { | ||
perror("Receive failed"); | ||
} else { | ||
num_packets++; | ||
} | ||
} | ||
|
||
return 0; | ||
} |