c# tcp协议发送数据

private void tcp_send(string data)//tcp协议转发数据
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 8000);//发送的主机地址及端口
NetworkStream ntwStream = tcpClient.GetStream();
if (ntwStream.CanWrite)
{
Byte[] bytSend = Encoding.UTF8.GetBytes(data);
ntwStream.Write(bytSend, 0, bytSend.Length);
}
else
{
ntwStream.Close();
tcpClient.Close();

return;
}

ntwStream.Close();
tcpClient.Close();
}

posted on 2018-05-11 13:03  七七2020  阅读(1093)  评论(0编辑  收藏  举报

导航