使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
2016-04-09 11:11
1521 查看
C#中使用TCP通信
TCP通信需要通信双方都在线,所以需要先启动服务端进行监听,客户端才能获得连接,服务端代码:
static void Main(string[] args) { TcpClient client = null; NetworkStream stream = null; byte[] buffer = null; string receiveString = null; IPAddress localIP = IPAddress.Parse("127.0.0.1"); int localPort = 11000; TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口实例化Listener listener.Start();//开始监听 while (true) { client = listener.AcceptTcpClient();//接受一个Client buffer = new byte[client.ReceiveBufferSize]; stream = client.GetStream();//获取网络流 stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据 stream.Close();//关闭流 client.Close();//关闭Client receiveString = Encoding.Default.GetString(buffer).Trim('\0');//转换成字符串 Console.WriteLine(receiveString); } }
只有服务端开启监听后,客户端才能正确连接,所以服务端要一直开启监听,客户端每次发送数据,都要首先与服务端建立连接,连接建立完成后才进行数据发送。客户端代码:
static void Main(string[] args) { string sendString = null;//要发送的字符串 byte[] sendData = null;//要发送的字节数组 TcpClient client = null;//TcpClient实例 NetworkStream stream = null;//网络流 IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//远程主机IP int remotePort = 11000;//远程主机端口 while (true)//死循环 { sendString = Console.ReadLine();//获取要发送的字符串 sendData = Encoding.Default.GetBytes(sendString);//获取要发送的字节数组 client = new TcpClient();//实例化TcpClient try { client.Connect(remoteIP, remotePort);//连接远程主机 } catch (System.Exception ex) { Console.WriteLine("连接超时,服务器没有响应!");//连接失败 Console.ReadKey(); return; } stream = client.GetStream();//获取网络流 stream.Write(sendData, 0, sendData.Length);//将数据写入网络流 stream.Close();//关闭网络流 client.Close();//关闭客户端 } }
C#中使用UDP通信
UDP通信是无连接通信,客户端在发送数据前无需与服务器端建立连接,即使服务器端不在线也可以发送,但是不能保证服务器端可以收到数据。
服务器端代码:
static void Main(string[] args) { UdpClient client = null; string receiveString = null; byte[] receiveData = null; //实例化一个远程端点,IP和端口可以随意指定,等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点 IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0); while (true) { client = new UdpClient(11000); receiveData = client.Receive(ref remotePoint);//接收数据 receiveString = Encoding.Default.GetString(receiveData); Console.WriteLine(receiveString); client.Close();//关闭连接 } }
客户端代码:
static void Main(string[] args) { string sendString = null;//要发送的字符串 byte[] sendData = null;//要发送的字节数组 UdpClient client = null; IPAddress remoteIP = IPAddress.Parse("127.0.0.1"); int remotePort = 11000; IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//实例化一个远程端点 while (true) { sendString = Console.ReadLine(); sendData = Encoding.Default.GetBytes(sendString); client = new UdpClient(); client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点 client.Close();//关闭连接 } }
您可能感兴趣的文章:
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法