68 // ------------------------------------------------------------------------------------------------- |
68 // ------------------------------------------------------------------------------------------------- |
69 // |
69 // |
70 METHOD |
70 METHOD |
71 UDPSocket::bind (unsigned short port) -> bool |
71 UDPSocket::bind (unsigned short port) -> bool |
72 { |
72 { |
73 struct sockaddr_in svaddr; |
73 sockaddr_in svaddr; |
74 memset (&svaddr, 0, sizeof svaddr); |
74 memset (&svaddr, 0, sizeof svaddr); |
75 svaddr.sin_family = AF_INET; |
75 svaddr.sin_family = AF_INET; |
76 svaddr.sin_port = htons (port); |
76 svaddr.sin_port = htons (port); |
77 svaddr.sin_addr.s_addr = htonl (INADDR_ANY); |
77 svaddr.sin_addr.s_addr = htonl (INADDR_ANY); |
78 |
78 |
79 if (::bind (m_socket, reinterpret_cast<struct sockaddr*> (&svaddr), sizeof svaddr) == -1) |
79 if (::bind (m_socket, reinterpret_cast<sockaddr*> (&svaddr), sizeof svaddr) == -1) |
80 { |
80 { |
81 m_error = String ("Couldn't bind to port ") + String::from_number (port); |
81 m_error = String ("Couldn't bind to port ") + String::from_number (port); |
82 return false; |
82 return false; |
83 } |
83 } |
84 |
84 |
91 UDPSocket::read (Datagram& datagram) -> bool |
91 UDPSocket::read (Datagram& datagram) -> bool |
92 { |
92 { |
93 sockaddr_in claddr; |
93 sockaddr_in claddr; |
94 socklen_t socklen = sizeof claddr; |
94 socklen_t socklen = sizeof claddr; |
95 int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0, |
95 int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0, |
96 reinterpret_cast<struct sockaddr*> (&claddr), &socklen); |
96 reinterpret_cast<sockaddr*> (&claddr), &socklen); |
97 |
97 |
98 if (length == -1) |
98 if (length == -1) |
99 { |
99 { |
100 if (errno != EWOULDBLOCK) |
100 if (errno != EWOULDBLOCK) |
101 m_error.sprintf ("recvfrom error: %s", strerror (errno)); |
101 m_error.sprintf ("recvfrom error: %s", strerror (errno)); |