UDPSocket.cpp 713 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/UDPSocket.h>
  7. #include <errno.h>
  8. #include <sys/socket.h>
  9. #ifndef SOCK_NONBLOCK
  10. # include <sys/ioctl.h>
  11. #endif
  12. namespace Core {
  13. UDPSocket::UDPSocket(Object* parent)
  14. : Socket(Socket::Type::UDP, parent)
  15. {
  16. #ifdef SOCK_NONBLOCK
  17. int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
  18. #else
  19. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  20. int option = 1;
  21. ioctl(fd, FIONBIO, &option);
  22. #endif
  23. if (fd < 0) {
  24. set_error(errno);
  25. } else {
  26. set_fd(fd);
  27. set_mode(OpenMode::ReadWrite);
  28. set_error(0);
  29. }
  30. }
  31. UDPSocket::~UDPSocket()
  32. {
  33. }
  34. }