TcpClient 读写流

时间:2021-08-16 02:23:19

TcpClient 读写流

           TcpClient tcp = new TcpClient();
tcp.Connect(IPAddress.Parse("192.168.1.161"), 8885);//根据服务器的IP地址和侦听的端口连接
if (tcp.Connected)
{ //获取网络流
NetworkStream networkStream = tcp.GetStream();
//将网络流作为二进制读写对象
br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream); bw.Write(str);//发送字符串命令
string result = br.ReadString();
if (result == "OK")
{
MessageBox.Show("取消成功!");
}
else
{
MessageBox.Show("取消失败!");
} } else
{
MessageBox.Show("无法连接服务器!");
return;
}
}
catch
{ MessageBox.Show("无法连接服务器!");
return;
}