실제로 메시지를 보내려면 연결을 닫아야합니까? send 명령을 사용하든 네트워크 스트림을 사용하든, 연결을 끊을 때까지 메시지가 처리되지 않기 때문입니다. 그게 그것이 있어야 할 방식인가요, 아니면 제가 빠진 것이 아닌가요?TcpClient 보내기/닫기 문제
여기 코드가 있습니다.
private void connectButton_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient();
client.Connect(ip, port);
netStream = client.GetStream();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void disconnectButton_Click(object sender, EventArgs e)
{
if (client != null)
{
if (client.Connected)
{
client.Close();
client = null;
}
}
}
private void sendButton_Click(object sender, EventArgs e)
{
byte[] cmd = ToByteArray("bla bla bla");
netStream.Write(cmd, 0, cmd.Length);
netStream.Flush();
}
나는이 방법과 관련이 없지만 모양을 가지고 있습니다. 당신은 아마 버퍼 스트림을 사용하는
public static byte[] ToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
어떤 수업을 사용하고 있습니까? + 코드 예제를 붙여 넣습니다. –
모든 코드는 우리 소유입니다. – jgauffin
ToByteArray는 어떤 모습입니까? – SwDevMan81