文章目录
- 1.引入Sockets
- 2.定义TcpClient
- 3.连接网口
- 4.发送数据
- 5.关闭连接
1.引入Sockets
using System.Net.Sockets;
2.定义TcpClient
private TcpClient tcpClient; // TcpClient实例
private NetworkStream stream; // 网络流,用于与服务器通信
3.连接网口
tcpClient = new TcpClient(); // TcpClient实例
{
try
{
tcpClient.Connect("IP地址", "端口号");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
4.发送数据
byte[] b = Encoding.ASCII.GetBytes(liens);
stream = tcpClient.GetStream();
BinaryWriter writer = new BinaryWriter(stream);//写入流
writer.Write(b, 0, b.Length);
5.关闭连接
tcpClient.Close();












![[JAVA]介绍怎样在Java中通过字节字符流实现文件读取与写入](https://i-blog.csdnimg.cn/direct/fec738f4ef6e4843a4332b9e00bb3708.png)





