DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
UdpClient.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Net/Sockets/UdpClient.h
"
3
#include "
System/Net/Sockets/SocketException.h
"
4
5
namespace
DotNetDupe
{
6
namespace
System
{
7
namespace
Net
{
8
namespace
Sockets
{
9
10
UdpClient::UdpClient
()
11
: m_pClientSocket(new
Socket
(
AddressFamily
::
InterNetwork
,
SocketType
::
Dgram
,
ProtocolType
::
Udp
), true) {
12
}
13
14
UdpClient::UdpClient
(
int
port)
15
: m_pClientSocket(new
Socket
(
AddressFamily
::
InterNetwork
,
SocketType
::
Dgram
,
ProtocolType
::
Udp
), true) {
16
m_pClientSocket->Bind(
"0.0.0.0"
, port);
17
}
18
19
UdpClient::UdpClient
(
const
String
& ip,
int
port)
20
: m_pClientSocket(new
Socket
(
AddressFamily
::
InterNetwork
,
SocketType
::
Dgram
,
ProtocolType
::
Udp
), true) {
21
m_pClientSocket->Bind(ip, port);
22
}
23
24
UdpClient::~UdpClient
() {
26
Close
();
27
}
28
29
UdpClient::UdpClient
(
UdpClient
&& other) noexcept
30
: m_pClientSocket(std::move(other.m_pClientSocket)) {
31
}
32
33
UdpClient
&
UdpClient::operator=
(
UdpClient
&& other)
noexcept
{
35
if
(
this
!= &other) {
36
Close
();
37
m_pClientSocket = std::move(other.m_pClientSocket);
38
}
39
return
*
this
;
40
}
41
42
void
UdpClient::Connect
(
const
String
& ip,
int
port) {
44
if
(m_pClientSocket.IsNull()) {
45
m_pClientSocket.Attach(
new
Socket
(
AddressFamily::InterNetwork
,
SocketType::Dgram
,
ProtocolType::Udp
),
true
);
46
}
47
49
m_pClientSocket->
Connect
(ip, port);
50
}
51
52
int
UdpClient::Send
(
const
char
* buffer,
int
offset,
int
size) {
54
if
(m_pClientSocket.IsNull()) {
55
throw
SocketException
(-1,
String
(
"UdpClient is closed."
));
56
}
57
59
return
m_pClientSocket->Send(buffer, offset, size);
60
}
61
62
int
UdpClient::Send
(
const
char
* buffer,
int
offset,
int
size,
const
String
& ip,
int
port) {
64
if
(m_pClientSocket.IsNull()) {
65
throw
SocketException
(-1,
String
(
"UdpClient is closed."
));
66
}
67
69
return
m_pClientSocket->SendTo(buffer, offset, size, ip, port);
70
}
71
72
int
UdpClient::Receive
(
char
* buffer,
int
offset,
int
size,
String
& ip,
int
& port) {
74
if
(m_pClientSocket.IsNull()) {
75
throw
SocketException
(-1,
String
(
"UdpClient is closed."
));
76
}
77
79
return
m_pClientSocket->ReceiveFrom(buffer, offset, size, ip, port);
80
}
81
82
void
UdpClient::Close
() {
84
if
(!m_pClientSocket.IsNull()) {
85
m_pClientSocket->Close();
86
m_pClientSocket =
nullptr
;
87
}
88
}
89
90
SmartPointer<Socket>
UdpClient::GetClient
()
const
{
91
return
m_pClientSocket;
92
}
93
94
}
95
}
96
}
97
}
SocketException.h
Exception thrown when a network socket error occurs per RFC 793 / RFC 768.
UdpClient.h
Provides User Datagram Protocol (UDP) network services.
DotNetDupe::System::Net::Sockets::SocketException::SocketException
SocketException(int errorCode, const String &message)
Initializes a new instance of the SocketException class with the specified error code and message.
Definition
SocketException.cpp:8
DotNetDupe::System::Net::Sockets::Socket
Implements the Berkeley sockets interface.
Definition
Socket.h:69
DotNetDupe::System::Net::Sockets::Socket::Socket
Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
Initializes a new instance of the Socket class using the specified address family,...
Definition
Socket.cpp:113
DotNetDupe::System::Net::Sockets::UdpClient::Receive
int Receive(char *buffer, int offset, int size, String &ip, int &port)
Returns a UDP datagram that was sent by a remote host.
Definition
UdpClient.cpp:72
DotNetDupe::System::Net::Sockets::UdpClient::~UdpClient
~UdpClient()
Destructor closing client socket.
Definition
UdpClient.cpp:24
DotNetDupe::System::Net::Sockets::UdpClient::Connect
void Connect(const String &ip, int port)
Establishes a default remote host using the specified IP address and port number.
Definition
UdpClient.cpp:42
DotNetDupe::System::Net::Sockets::UdpClient::operator=
UdpClient & operator=(const UdpClient &)=delete
DotNetDupe::System::Net::Sockets::UdpClient::UdpClient
UdpClient()
Initializes a new instance of the UdpClient class.
Definition
UdpClient.cpp:10
DotNetDupe::System::Net::Sockets::UdpClient::Send
int Send(const char *buffer, int offset, int size)
Sends a UDP datagram to a remote host (used when Connect has established default host).
Definition
UdpClient.cpp:52
DotNetDupe::System::Net::Sockets::UdpClient::GetClient
SmartPointer< Socket > GetClient() const
Gets the underlying network Socket.
Definition
UdpClient.cpp:90
DotNetDupe::System::Net::Sockets::UdpClient::Close
void Close()
Closes the UDP connection.
Definition
UdpClient.cpp:82
DotNetDupe::System::SmartPointer
A unified smart pointer that supports both unique and shared ownership semantics.
Definition
SmartPointer.h:72
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::Net::Sockets
Definition
HttpClient.h:20
DotNetDupe::System::Net::Sockets::AddressFamily
AddressFamily
Specifies the addressing scheme that an instance of the Socket class can use.
Definition
Socket.h:20
DotNetDupe::System::Net::Sockets::AddressFamily::InterNetwork
@ InterNetwork
Definition
Socket.h:23
DotNetDupe::System::Net::Sockets::ProtocolType
ProtocolType
Specifies the protocols that the Socket class supports.
Definition
Socket.h:38
DotNetDupe::System::Net::Sockets::ProtocolType::Udp
@ Udp
Definition
Socket.h:41
DotNetDupe::System::Net::Sockets::SocketType
SocketType
Specifies the type of socket that an instance of the Socket class represents.
Definition
Socket.h:29
DotNetDupe::System::Net::Sockets::SocketType::Dgram
@ Dgram
Definition
Socket.h:32
DotNetDupe::System::Net
Definition
Dns.h:13
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
UdpClient.cpp
Generated by
1.18.0