DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
UdpClient.cpp
Go to the documentation of this file.
1#include "pch.h"
4
5namespace DotNetDupe {
6 namespace System {
7 namespace Net {
8 namespace Sockets {
9
11 : m_pClientSocket(new Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp), true) {
12 }
13
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
28
30 : m_pClientSocket(std::move(other.m_pClientSocket)) {
31 }
32
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
84 if (!m_pClientSocket.IsNull()) {
85 m_pClientSocket->Close();
86 m_pClientSocket = nullptr;
87 }
88 }
89
91 return m_pClientSocket;
92 }
93
94 }
95 }
96 }
97}
Exception thrown when a network socket error occurs per RFC 793 / RFC 768.
Provides User Datagram Protocol (UDP) network services.
SocketException(int errorCode, const String &message)
Initializes a new instance of the SocketException class with the specified error code and message.
Implements the Berkeley sockets interface.
Definition Socket.h:69
Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
Initializes a new instance of the Socket class using the specified address family,...
Definition Socket.cpp:113
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
~UdpClient()
Destructor closing client socket.
Definition UdpClient.cpp:24
void Connect(const String &ip, int port)
Establishes a default remote host using the specified IP address and port number.
Definition UdpClient.cpp:42
UdpClient & operator=(const UdpClient &)=delete
UdpClient()
Initializes a new instance of the UdpClient class.
Definition UdpClient.cpp:10
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
SmartPointer< Socket > GetClient() const
Gets the underlying network Socket.
Definition UdpClient.cpp:90
void Close()
Closes the UDP connection.
Definition UdpClient.cpp:82
A unified smart pointer that supports both unique and shared ownership semantics.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
AddressFamily
Specifies the addressing scheme that an instance of the Socket class can use.
Definition Socket.h:20
ProtocolType
Specifies the protocols that the Socket class supports.
Definition Socket.h:38
SocketType
Specifies the type of socket that an instance of the Socket class represents.
Definition Socket.h:29