001.
002.
003.
004.
005.
006.
007.
008.
009.
010.
011.
012.
013.
014.
015.
016.
017.
018.
019.
020.
021.
022.
023.
024.
025.
026.
027.
028.
029.
030.
031.
032.
033.
034.
035.
036.
037.
038.
039.
040.
041.
042.
043.
044.
045.
046.
047.
#region Using Directive
048.
using
System;
049.
using
System.Collections.Generic;
050.
using
System.Linq;
051.
using
System.Text;
052.
using
System.Net;
053.
using
System.Net.Sockets;
054.
using
System.Threading;
055.
using
System.Security;
056.
using
System.Security.Permissions;
057.
#endregion
058.
059.
060.
public
class
Program
061.
{
062.
static
void
Main(
string
[] args)
063.
{
064.
065.
SocketPermission permission =
new
SocketPermission(
066.
NetworkAccess.Accept,
067.
TransportType.Tcp,
068.
""
,
069.
SocketPermission.AllPorts
070.
);
071.
072.
073.
Socket sListener =
null
;
074.
075.
try
076.
{
077.
078.
permission.Demand();
079.
080.
081.
IPHostEntry ipHost = Dns.GetHostEntry(
""
);
082.
083.
///Get First IPV4 Address
084.
///아직까지는 IPV4로 만들지 않으면 외부에서 접근을 못하는 경우가 많다.;
085.
IPAddress ipAddr = ipHost.AddressList.Where(a => a.AddressFamily != AddressFamily.InterNetworkV6).First();
086.
087.
088.
IPEndPoint ipEndPoint =
new
IPEndPoint(ipAddr, 6000);
089.
090.
091.
sListener =
new
Socket(
092.
ipAddr.AddressFamily,
093.
SocketType.Stream,
094.
ProtocolType.Tcp
095.
);
096.
097.
098.
sListener.Bind(ipEndPoint);
099.
100.
101.
102.
sListener.Listen(10);
103.
104.
Console.WriteLine(
"Waiting for a connection on port {0}"
,
105.
ipEndPoint);
106.
107.
108.
AsyncCallback aCallback =
new
AsyncCallback(AcceptCallback);
109.
sListener.BeginAccept(aCallback, sListener);
110.
}
111.
catch
(Exception ex)
112.
{
113.
Console.WriteLine(
"Exception: {0}"
, ex.ToString());
114.
return
;
115.
}
116.
117.
Console.WriteLine(
"Press the Enter key to exit ..."
);
118.
Console.ReadLine();
119.
120.
if
(sListener.Connected)
121.
{
122.
sListener.Shutdown(SocketShutdown.Receive);
123.
sListener.Close();
124.
}
125.
}
126.
127.
128.
/// <summary>
129.
/// Asynchronously accepts an incoming connection attempt and creates
130.
/// a new Socket to handle remote host communication.
131.
/// </summary>
132.
/// <param name="ar">the status of an asynchronous operation
133.
///
134.
public
static
void
AcceptCallback(IAsyncResult ar)
135.
{
136.
Socket listener =
null
;
137.
138.
139.
Socket handler =
null
;
140.
try
141.
{
142.
143.
byte
[] buffer =
new
byte
[1024];
144.
145.
listener = (Socket)ar.AsyncState;
146.
147.
handler = listener.EndAccept(ar);
148.
149.
150.
handler.NoDelay =
true
;
151.
152.
153.
object
[] obj =
new
object
[2];
154.
obj[0] = buffer;
155.
obj[1] = handler;
156.
157.
158.
handler.BeginReceive(
159.
buffer,
160.
0,
161.
buffer.Length,
162.
SocketFlags.None,
163.
new
AsyncCallback(ReceiveCallback),
164.
obj
165.
);
166.
167.
168.
AsyncCallback aCallback =
new
AsyncCallback(AcceptCallback);
169.
listener.BeginAccept(aCallback, listener);
170.
}
171.
catch
(Exception ex)
172.
{
173.
Console.WriteLine(
"Exception: {0}"
, ex.ToString());
174.
}
175.
}
176.
177.
/// <summary>
178.
/// Asynchronously receive data from a connected Socket.
179.
/// </summary>
180.
/// <param name="ar">
181.
/// the status of an asynchronous operation
182.
///
183.
public
static
void
ReceiveCallback(IAsyncResult ar)
184.
{
185.
try
186.
{
187.
188.
object
[] obj =
new
object
[2];
189.
obj = (
object
[])ar.AsyncState;
190.
191.
192.
byte
[] buffer = (
byte
[])obj[0];
193.
194.
195.
Socket handler = (Socket)obj[1];
196.
197.
198.
string
content =
string
.Empty;
199.
200.
201.
int
bytesRead = handler.EndReceive(ar);
202.
203.
if
(bytesRead > 0)
204.
{
205.
content += Encoding.Unicode.GetString(buffer, 0,
206.
bytesRead);
207.
208.
if
(content.IndexOf(
"<client quit="
">"
) > -1)
209.
{
210.
211.
string
str =
212.
content.TrimEnd((
"<client quit="
">"
).ToCharArray());
213.
Console.WriteLine(
214.
"Read {0} bytes from client.\n Data: {1}"
,
215.
str.Length, str);
216.
217.
218.
byte
[] byteData =
219.
Encoding.Unicode.GetBytes(str);
220.
221.
222.
handler.BeginSend(byteData, 0, byteData.Length, 0,
223.
new
AsyncCallback(SendCallback), handler);
224.
}
225.
else
226.
{
227.
228.
byte
[] buffernew =
new
byte
[1024];
229.
obj[0] = buffernew;
230.
obj[1] = handler;
231.
handler.BeginReceive(buffernew, 0, buffernew.Length,
232.
SocketFlags.None,
233.
new
AsyncCallback(ReceiveCallback), obj);
234.
}
235.
}
236.
}
237.
catch
(Exception ex)
238.
{
239.
Console.WriteLine(
"Exception: {0}"
, ex.ToString());
240.
}
241.
}
242.
243.
/// <summary>
244.
/// Sends data asynchronously to a connected Socket.
245.
/// </summary>
246.
/// <param name="ar">
247.
/// The status of an asynchronous operation
248.
///
249.
public
static
void
SendCallback(IAsyncResult ar)
250.
{
251.
try
252.
{
253.
254.
Socket handler = (Socket)ar.AsyncState;
255.
256.
257.
int
bytesSend = handler.EndSend(ar);
258.
Console.WriteLine(
259.
"Sent {0} bytes to Client"
,bytesSend);
260.
}
261.
catch
(Exception ex)
262.
{
263.
Console.WriteLine(
"Exception: {0}"
, ex.ToString());
264.
}
265.
}
266.
}
267.
</client></client></client>