diff options
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/.gitignore | 1 | ||||
-rw-r--r-- | Userland/Makefile | 6 | ||||
-rw-r--r-- | Userland/uc.cpp | 53 |
3 files changed, 60 insertions, 0 deletions
diff --git a/Userland/.gitignore b/Userland/.gitignore index 17a5d60640..3f492418c4 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -38,3 +38,4 @@ env chown stat ping +uc diff --git a/Userland/Makefile b/Userland/Makefile index 6acf59947b..2af1bdaa27 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -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 $< diff --git a/Userland/uc.cpp b/Userland/uc.cpp new file mode 100644 index 0000000000..cb28aa53f9 --- /dev/null +++ b/Userland/uc.cpp @@ -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; +} |