... | @@ -23,8 +23,6 @@ class CallerInfo |
... | @@ -23,8 +23,6 @@ class CallerInfo |
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
:question: :eyes: :star: :hand_with_index_and_middle_finger_crossed: :muscle: :kr: :heart_eyes_cat: :ok_hand: :speak_no_evil: :bulb: :exclamation:
|
|
|
|
|
|
|
|
## 비동기 호출
|
|
## 비동기 호출
|
|
|
|
|
|
### async/await 예약어
|
|
### async/await 예약어
|
... | @@ -59,5 +57,63 @@ class AsyncAwaitTest |
... | @@ -59,5 +57,63 @@ class AsyncAwaitTest |
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
|
|
```c#
|
|
|
|
|
|
|
|
class BCLAsyncTest //WebClient 비동기 호출 async/await 예제
|
|
|
|
{
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
AwaitDownloadString();
|
|
|
|
|
|
|
|
Console.ReadLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async void AwaitDownloadString()
|
|
|
|
{
|
|
|
|
WebClient wc = new WebClient();
|
|
|
|
wc.Encoding = Encoding.UTF8;
|
|
|
|
|
|
|
|
//DownloadStringAsync보다 더 간편하게 이용 가능.
|
|
|
|
string text = await wc.DownloadStringTaskAsync("http://www.naver.com");
|
|
|
|
Console.WriteLine(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```c#
|
|
|
|
class BCLAsyncTest2 //TCP 서버 비동기 통신 예제
|
|
|
|
{
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
TcpListener listener = new TcpListener(IPAddress.Any, 11200);
|
|
|
|
listener.Start();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
//연결 요청을 받아들인다.
|
|
|
|
var client = listener.AcceptTcpClient();
|
|
|
|
ProcessTcpClient(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//NetworkStream 클래스의 ReadAsync와 WriteAsync를 이용하면 간단하게 비동기 통신 구현 가능
|
|
|
|
private static async void ProcessTcpClient(TcpClient client)
|
|
|
|
{
|
|
|
|
NetworkStream ns = client.GetStream();
|
|
|
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int received = await ns.ReadAsync(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
string txt = Encoding.UTF8.GetString(buffer, 0 ,received);
|
|
|
|
|
|
|
|
byte[] sendBuffer = Encoding.UTF8.GetBytes("Hello : " + txt);
|
|
|
|
await ns.WriteAsync(sendBuffer, 0, sendBuffer.Length);
|
|
|
|
ns.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|