1- using System . Net ;
1+ using System ;
2+ using System . Net ;
23using System . Net . Sockets ;
4+ using System . Text ;
35
46namespace GodSharp . Sockets
57{
68 public static class SocketExtensions
79 {
810 /// <summary>
9- /// Gets the TCP connection information .
11+ /// Sends data to a connected <see cref="Socket"/> .
1012 /// </summary>
1113 /// <param name="socket">The socket.</param>
14+ /// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
15+ /// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
16+ /// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
17+ public static int Send ( this Socket socket , string data , Encoding encoding = null )
18+ {
19+ if ( encoding == null ) encoding = Encoding . UTF8 ;
20+
21+ return socket . Send ( encoding . GetBytes ( data ) ) ;
22+ }
23+
24+ /// <summary>
25+ /// Begins the send.
26+ /// </summary>
27+ /// <param name="socket">The socket.</param>
28+ /// <param name="data">The data.</param>
29+ /// <param name="callback">The callback.</param>
30+ /// <param name="state">The state.</param>
31+ /// <param name="encoding">The encoding.</param>
32+ /// <returns></returns>
33+ public static IAsyncResult BeginSend ( this Socket socket , string data , AsyncCallback callback , object state , Encoding encoding = null )
34+ {
35+ if ( encoding == null ) encoding = Encoding . UTF8 ;
36+ byte [ ] buffers = encoding . GetBytes ( data ) ;
37+ return socket . BeginSend ( buffers , 0 , buffers . Length , SocketFlags . None , callback , state ) ;
38+ }
39+
40+ /// <summary>
41+ /// Sends data to a connected <see cref="Socket" /> using the specified <see cref="SocketFlags" />.
42+ /// </summary>
43+ /// <param name="socket">The socket.</param>
44+ /// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
45+ /// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
46+ /// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
47+ /// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
48+ public static int Send ( this Socket socket , string data , SocketFlags socketFlags , Encoding encoding = null )
49+ {
50+ if ( encoding == null ) encoding = Encoding . UTF8 ;
51+
52+ return socket . Send ( encoding . GetBytes ( data ) , socketFlags ) ;
53+ }
54+
55+ /// <summary>
56+ /// Sends the specified number of bytes of data to a connected <see cref="Socket" />, starting at the specified offset, and using the specified <see cref="SocketFlags" />.
57+ /// </summary>
58+ /// <param name="socket">The socket.</param>
59+ /// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
60+ /// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
61+ /// <param name="socketError">A <see cref="SocketError" /> object that stores the socket error.</param>
62+ /// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
63+ /// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
64+ public static int Send ( this Socket socket , string data , SocketFlags socketFlags , out SocketError socketError , Encoding encoding = null )
65+ {
66+ if ( encoding == null ) encoding = Encoding . UTF8 ;
67+ byte [ ] buffers = encoding . GetBytes ( data ) ;
68+ return socket . Send ( buffers , 0 , buffers . Length , socketFlags , out socketError ) ;
69+ }
70+
71+ /// <summary>
72+ /// Sends data to a <see cref="Socket"/>.
73+ /// </summary>
74+ /// <param name="socket">The socket.</param>
75+ /// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
76+ /// <param name="endPoint">The end point.</param>
77+ /// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
78+ /// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
79+ public static int SendTo ( this Socket socket , string data , EndPoint endPoint , Encoding encoding = null )
80+ {
81+ if ( encoding == null ) encoding = Encoding . UTF8 ;
82+
83+ return socket . SendTo ( encoding . GetBytes ( data ) , endPoint ) ;
84+ }
85+
86+ /// <summary>
87+ /// Sends the specified number of bytes of data to a <see cref="Socket" />, using the specified <see cref="SocketFlags" />.
88+ /// </summary>
89+ /// <param name="socket">The socket.</param>
90+ /// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
91+ /// <param name="socketFlags">The socket flags.</param>
92+ /// <param name="endPoint">The end point.</param>
93+ /// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
94+ /// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
95+ public static int SendTo ( this Socket socket , string data , SocketFlags socketFlags , EndPoint endPoint , Encoding encoding = null )
96+ {
97+ if ( encoding == null ) encoding = Encoding . UTF8 ;
98+
99+ return socket . SendTo ( encoding . GetBytes ( data ) , socketFlags , endPoint ) ;
100+ }
101+
102+ /// <summary>
103+ /// Begins the send to.
104+ /// </summary>
105+ /// <param name="socket">The socket.</param>
106+ /// <param name="buffers">The buffers.</param>
107+ /// <param name="remoteEP">The remote ep.</param>
108+ /// <param name="callback">The callback.</param>
109+ /// <param name="state">The state.</param>
12110 /// <returns></returns>
13- //public static ConnectionInformation GetConnectionInformation(this Socket socket)
14- //{
15- // IPEndPoint remote = socket.RemoteEndPoint as IPEndPoint;
16- // IPEndPoint local = socket.LocalEndPoint as IPEndPoint;
111+ public static IAsyncResult BeginSendTo ( this Socket socket , byte [ ] buffers , EndPoint remoteEP , AsyncCallback callback , object state ) => socket . BeginSendTo ( buffers , 0 , buffers . Length , SocketFlags . None , remoteEP , callback , state ) ;
17112
18- // return NetworkHelper.GetTcpConnectionInformation(local?.Port ?? 0, remote?.Port ?? 0) as ConnectionInformation;
19- //}
113+ /// <summary>
114+ /// Begins the send to.
115+ /// </summary>
116+ /// <param name="socket">The socket.</param>
117+ /// <param name="data">The data.</param>
118+ /// <param name="remoteEP">The remote ep.</param>
119+ /// <param name="callback">The callback.</param>
120+ /// <param name="state">The state.</param>
121+ /// <param name="encoding">The encoding.</param>
122+ /// <returns></returns>
123+ public static IAsyncResult BeginSendTo ( this Socket socket , string data , EndPoint remoteEP , AsyncCallback callback , object state , Encoding encoding = null )
124+ {
125+ if ( encoding == null ) encoding = Encoding . UTF8 ;
126+ byte [ ] buffers = encoding . GetBytes ( data ) ;
127+ return socket . BeginSendTo ( buffers , 0 , buffers . Length , SocketFlags . None , remoteEP , callback , state ) ;
128+ }
20129 }
21130}
0 commit comments