Userland: Add a simple utility for UDP testing.

This commit is contained in:
Andreas Kling 2019-03-13 15:00:02 +01:00
parent 19a51132f5
commit ea6a537b70
Notes: sideshowbarker 2024-07-19 15:04:19 +09:00
4 changed files with 61 additions and 0 deletions

View file

@ -75,6 +75,7 @@ cp -v ../Userland/su mnt/bin/su
cp -v ../Userland/env mnt/bin/env
cp -v ../Userland/stat mnt/bin/stat
cp -v ../Userland/ping mnt/bin/ping
cp -v ../Userland/uc mnt/bin/uc
chmod 4755 mnt/bin/su
cp -v ../Applications/Terminal/Terminal mnt/bin/Terminal
cp -v ../Applications/FontEditor/FontEditor mnt/bin/FontEditor

1
Userland/.gitignore vendored
View file

@ -38,3 +38,4 @@ env
chown
stat
ping
uc

View file

@ -34,6 +34,7 @@ OBJS = \
env.o \
stat.o \
ping.o \
uc.o \
rm.o
APPS = \
@ -73,6 +74,7 @@ APPS = \
env \
stat \
ping \
uc \
rm
ARCH_FLAGS =
@ -203,6 +205,10 @@ stat: stat.o
ping: ping.o
$(LD) -o $@ $(LDFLAGS) $< -lc
uc: uc.o
$(LD) -o $@ $(LDFLAGS) $< -lc
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

53
Userland/uc.cpp Normal file
View file

@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
struct timeval timeout { 5, 0 };
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if (rc < 0) {
perror("setsockopt");
return 1;
}
struct sockaddr_in dst_addr;
memset(&dst_addr, 0, sizeof(dst_addr));
dst_addr.sin_family = AF_INET;
dst_addr.sin_port = htons(8080);
dst_addr.sin_addr.s_addr = INADDR_ANY;
char buffer[BUFSIZ];
const char* msg = "Test message";
sendto(fd, (const char *)msg, strlen(msg), 0,(const struct sockaddr *)&dst_addr, sizeof(dst_addr));
printf("Message sent.\n");
struct sockaddr_in src_addr;
socklen_t src_addr_len = sizeof(src_addr);
ssize_t nrecv = recvfrom(fd, (char *)buffer, sizeof(buffer), 0, (struct sockaddr*)&src_addr, &src_addr_len);
if (nrecv < 0) {
perror("recvfrom");
return 1;
}
buffer[nrecv] = '\0';
printf("Server: %s\n", buffer);
close(fd);
return 0;
}