客户端和服务端之间的通信(UDP)

实现一个小的通信功能:客户端向服务端提交数据,然后服务端回执数据。

一、新建一个控制台应用程序:Client。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Socket对象
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
            Console.Write("向服务端提交当前时间!\n");

            string dt = DateTime.Now.ToString();//获取当前时间。
            byte[] send = Encoding.ASCII.GetBytes(dt);

            //string message = Console.ReadLine();//发送客户端输入信息。
            //byte[] send = Encoding.ASCII.GetBytes(message);
            s.SendTo(send, (EndPoint)ipep);//向服务器发送

            int length;
            byte[] data = new byte[1024];
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)(sender);
            length = s.ReceiveFrom(data, ref Remote);//通过ReceiveFrom接受来自服务端的数据
            Console.Write("" + Remote.ToString() + "接收信息:\n服务器已接收到客户端发送时间");
            string receive = Encoding.ASCII.GetString(data, 0, length);
            Console.WriteLine(receive);
            Console.ReadLine();
        }
    }
}

 

二、新建一个控制台应用程序:Server。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
            s.Bind(ipep);//使用Bind方法绑定所指定的接口使Socket与一个本地终结点相联
            Console.WriteLine("等待客户端发送信息");

            int length;
            byte[] data = new byte[1024];
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)(sender);
            length = s.ReceiveFrom(data, ref Remote);
            Console.Write("" + Remote.ToString() + "接收信息:");
            string receive = Encoding.ASCII.GetString(data, 0, length);
            Console.WriteLine(receive);

            //string sendString = "SerderTime from Cliend:"+receive;
            byte[] send = Encoding.ASCII.GetBytes(receive);
            s.SendTo(send, Remote);

            //s.Close();
            Console.ReadLine();

        }
    }
}

先后运行服务端和客户端程序。执行结果如下:

三、可以进一步的完善功能:如可以让客户端每5秒钟向客户端提交一次数据。

这样只需要对程序进行一些修改:在客户端增加一个定时器,在程序运行后,每5秒钟向服务端提交一次当前时间。(在Client运行期间,Server端必须保持运行状态)

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    public class Program
    {
        //创建一个Socket对象
        static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        static IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
        static void Main(string[] args)
        {
            Console.Write("向服务端提交当前时间!\n");
            //创建一个定时器对象
            System.Timers.Timer myTimer = new System.Timers.Timer();
            myTimer.AutoReset = true;
            myTimer.Interval = 5000;
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Enabled = true;
            Console.ReadLine();
        }

        public static void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string dt = DateTime.Now.ToString();
            byte[] send = Encoding.ASCII.GetBytes(dt);
            //string message = Console.ReadLine();
            //byte[] send = Encoding.ASCII.GetBytes(message);
            s.SendTo(send, (EndPoint)ipep);//向服务器发送当前时间

            int length;
            byte[] data = new byte[1024];
            IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)(senderEP);
            length = s.ReceiveFrom(data, ref Remote);//通过ReceiveFrom接受来自服务端的数据
            Console.Write("" + Remote.ToString() + "接收信息:\n服务器已接收到客户端发送时间");
            string receive = Encoding.ASCII.GetString(data, 0, length);
            Console.WriteLine(receive);
            Console.ReadLine();
        }
    }
}

在服务端接收和回执部分代码上添加一个while循环语句:

四、再次先后运行Server和Client。运行结果如下:

posted @ 2012-07-29 18:00  Shang0109  阅读(4960)  评论(2编辑  收藏  举报