TCPSocket.cpp 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/TCPSocket.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. TCPSocket::TCPSocket(int fd, Object* parent)
  14. : Socket(Socket::Type::TCP, parent)
  15. {
  16. // NOTE: This constructor is used by TCPServer::accept(), so the socket is already connected.
  17. m_connected = true;
  18. set_fd(fd);
  19. set_mode(OpenMode::ReadWrite);
  20. set_error(0);
  21. }
  22. TCPSocket::TCPSocket(Object* parent)
  23. : Socket(Socket::Type::TCP, parent)
  24. {
  25. #ifdef SOCK_NONBLOCK
  26. int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  27. #else
  28. int fd = socket(AF_INET, SOCK_STREAM, 0);
  29. int option = 1;
  30. ioctl(fd, FIONBIO, &option);
  31. #endif
  32. if (fd < 0) {
  33. set_error(errno);
  34. } else {
  35. set_fd(fd);
  36. set_mode(OpenMode::ReadWrite);
  37. set_error(0);
  38. }
  39. }
  40. TCPSocket::~TCPSocket()
  41. {
  42. }
  43. }