C#的Socket编程小记

本文提供了一个简单的UDP通信示例,包括服务端与客户端的代码实现。通过这些代码,读者可以了解如何使用C#进行UDP数据包的发送与接收。
UDP协议。

看代码:

服务端:
InBlock.gifusing System;
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Linq;
InBlock.gifusing System.Text;
InBlock.gifusing System.Net;
InBlock.gifusing System.Net.Sockets;
InBlock.gif
namespace UDPServer
InBlock.gif{
InBlock.gif        class Program
InBlock.gif        {
InBlock.gif                static void Main(string[] args)
InBlock.gif                {
InBlock.gif                        int recv;
InBlock.gif                        byte[] data = new byte[1024]; 
InBlock.gif
                        //构建TCP 服务器
InBlock.gif
                        //得到本机IP,设置TCP端口号                 
InBlock.gif                        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.125"), 8001);
InBlock.gif                        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp);
InBlock.gif
                        //绑定网络地址
InBlock.gif                        newsock.Bind(ipep);
InBlock.gif
                        Console.WriteLine("这里是服务器, 主机名是 {0}",Dns.GetHostName());
InBlock.gif
                        //等待客户机连接
InBlock.gif                        Console.WriteLine("等待客户端连接");
InBlock.gif
                        //得到客户机IP
InBlock.gif                        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
InBlock.gif                        EndPoint Remote = (EndPoint)(sender);
InBlock.gif                        recv = newsock.ReceiveFrom(data, ref Remote);//会阻塞
InBlock.gif                        Console .WriteLine ("消息来自 {0}: ", Remote.ToString ());
InBlock.gif                        Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
InBlock.gif
                        //客户机连接成功后,发送欢迎信息
InBlock.gif                        string welcome = "欢迎 ! ";
InBlock.gif
                        //字符串与字节数组相互转换
InBlock.gif                        //data    = Encoding .ASCII .GetBytes(welcome);
InBlock.gif                        data = Encoding.UTF8.GetBytes("服务端>>>"+welcome);
InBlock.gif
                        //发送信息
InBlock.gif                        newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
InBlock.gif                        while (true)
InBlock.gif                        {
InBlock.gif                                data =new byte [1024];
InBlock.gif                                //发送接受信息
InBlock.gif                                recv =newsock.ReceiveFrom(data ,ref Remote);
InBlock.gif                                Console.Write("客户端>>>");
InBlock.gif                                Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
InBlock.gif
                                //向客户端回复信息
InBlock.gif                                newsock.SendTo(data,recv,SocketFlags.None, Remote);
InBlock.gif                        }
InBlock.gif                }
InBlock.gif        }
InBlock.gif}


客户端:
InBlock.gifusing System;
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Linq;
InBlock.gifusing System.Text;
InBlock.gifusing System.Net;
InBlock.gifusing System.Net.Sockets;
InBlock.gif
namespace UdpClient
InBlock.gif{
InBlock.gif        class Program
InBlock.gif        {
InBlock.gif                static void Main(string[] args)
InBlock.gif                {
InBlock.gif                        byte[] data = new byte[1024];
InBlock.gif                        string input, stringData;
InBlock.gif

InBlock.gif                        Console.WriteLine("这里是客户端, 主机名是 {0}", Dns.GetHostName());
InBlock.gif
                        //设置服务端IP,设置TCP端口号
InBlock.gif                        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.125"), 8001);
InBlock.gif
                        //定义网络类型,数据连接类型和网络协议UDP
InBlock.gif                        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
InBlock.gif
                        string welcome = "你好! ";
InBlock.gif                        data = Encoding.UTF8.GetBytes("客户端>>>" + welcome);
InBlock.gif                        server.SendTo(data, data.Length, SocketFlags.None, ipep);//向服务端发送数据
InBlock.gif
                        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
InBlock.gif                        EndPoint Remote = (EndPoint)sender;
InBlock.gif
                        data = new byte[1024];
InBlock.gif                        //对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
InBlock.gif                        //server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
InBlock.gif                        int recv = server.ReceiveFrom(data, ref Remote);//将数据接收放入缓冲区
InBlock.gif                        Console.WriteLine("消息来自服务端 {0}: ", Remote.ToString());
InBlock.gif                        Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
InBlock.gif
                        while (true)
InBlock.gif                        {
InBlock.gif                                input = Console.ReadLine();
InBlock.gif
                                //如果输入exit则退出
InBlock.gif                                if (input == "exit" || input=="quit")
InBlock.gif                                        break;
InBlock.gif
                                //input = "客户端>>>" + input;
InBlock.gif                                server.SendTo(Encoding.UTF8.GetBytes(input), Remote);
InBlock.gif
                                data = new byte[1024];
InBlock.gif                                recv = server.ReceiveFrom(data, ref Remote);
InBlock.gif                                stringData = Encoding.UTF8.GetString(data, 0, recv);
InBlock.gif                                Console.Write("服务端回复>>>");
InBlock.gif                                Console.WriteLine(stringData);
InBlock.gif                        }
InBlock.gif                        Console.WriteLine("客户端关闭.");
InBlock.gif                        server.Close();
InBlock.gif                }
InBlock.gif        }
InBlock.gif}

研究一个教程,只做了些改动。






 本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/318765,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值