"TCPClient 클라이언트 1단계" 구현 목표

 

서버로 메시지 전송 구현

더보기
if (userInput != null && userInput.Trim() != "<EXIT>" && client.Client != null && client.Client.Connected)
{
    // [F] 서버로 메시지 전송
    _ = client.SendData(userInput);
}

 

 

if(string.IsNullOrEmpty(userInput) || _client == null || !_client.Connected)
{
    Console.WriteLine(" - 서버에 연결되어 있지 않거나, 전송할 데이터가 없습니다.");
    return;
}

 

 

else
{
    try
    {
        // [G.1] 서버로 데이터를 전송하기 위한 준비 (StreamWriter 등)
        StreamWriter clntStreamWriter = new StreamWriter(_client.GetStream());
        clntStreamWriter.AutoFlush = true;
        // [G.2] 사용자 입력 데이터를 서버로 전송
        await clntStreamWriter.WriteAsync(userInput);
        Console.WriteLine($"서버로 전송한 메시지: {userInput}");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        throw;
    }
}

 

 

 

실행 테스트