목표


이벤트 전달용 <데이터 클래스> 자료형 구현

: 이전 포스트 서버 구현 참고 및 비교

더보기
// [이벤트 전달용 데이터 클래스]
public class TextReceivedEventArgs : EventArgs
{
    public string ClientInfo { get; set; }
    public string TextReceived { get; set; }

    public TextReceivedEventArgs(string _clientInfo, string _textReceived)
    {
        ClientInfo = _clientInfo;
        TextReceived = _textReceived;
    }
}

 

이벤트 정의 구현

: 이전 포스트 서버 구현 참고 및 비교

더보기
// 텍스트 수신 이벤트 정의
public EventHandler<CustomEventArgs>? TextReceivedEvent;
// 텍스트 수신 이벤트 발생 메서드
public void OnRaiseTextReceivedEvent(CustomEventArgs e)
{
    TextReceivedEvent?.Invoke(this, e);
}


이벤트 실행 구현

: 이전 포스트 서버 구현 참고 및 비교

더보기
// [F.4] 텍스트 수신 이벤트 발생 메서드 호출
OnRaiseTextReceivedEvent(new CustomEventArgs(new string(buffer, 0, readByteCount)));


메인 로직에 적용하기

: 이전 포스트 서버 구현 참고 및 비교

더보기
private static void Client_TextReceivedEvent(object? sender, CustomEventArgs e)
{
    // 수신된 텍스트 출력
    Console.WriteLine($"[서버로부터 수신된 메시지]: {e.NewClientInfo}");
}
// 텍스트 수신 이벤트 핸들러 등록
client.TextReceivedEvent += Client_TextReceivedEvent;

 

실행 테스트