.NET平台上 利用socket传输自定义类的实体 C#, 以及把自定义的类生成DLL文件

利用udp协议进行类实体的传送,首先需要把传送的类 制作成DLL链接库,然后服务器端和客户端都要进行该DLL库的引用。在服务器端需要对自定义的类实体进行序列化,传送给客户端后,客户端需要对接收的类实体进行发序列化,恢复出原来的信息。以下是步骤:

1,建立被传送类的DLL文件

在VS2010上创建新项目,类库,然后把自定义类的代码写进该文档,这里定义了个人GPS跟踪的位置信息类,定义如下:

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GPSPointInfo {    

       [Serializable]    //因为该类要被序列化传输,所以要在类前添加该标记

        public class TimeDataObjector   

       {        

           public string stuffid;         //个人的名字或者工号

           public long x;         //所在位置的经度

           public long y;         //所在位置的纬度

           public long Time;        //现在的时间

           public string Place;     //所在地方的名字

}

}

 

然后点击“生成”-》”生成解决方案“,然后去工程的debug文件夹下就可以找到该类的DLL库,

 

2,

编写客户端,这是需要引用书面的DLL库,点击“项目”-》“添加引用”,然后找到刚才的DLL库添加进来,再在客户端using  名字;名字是刚才类的空间域名,这里PointObjector。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization.Formatters;

using System.Runtime.Serialization;

using System.Threading;

using System.IO;

using PointObjector;

namespace UDPClient {    

class Program     {

        public static void StartListener()         {            

                 Console.WriteLine("开始接收数据:");            

              Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);           

           IPEndPoint iep = new IPEndPoint(IPAddress.Any, 3222);           

               EndPoint ep = (EndPoint)iep;             s.Bind(iep);          

              s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.0.0.1")));             byte[] b = new byte[1024];           

           while (true)           

         {                

          int length = s.ReceiveFrom(b, ref ep);

                MemoryStream MStream = new MemoryStream();               

               MStream.Position = 0;                

                MStream.Write(b, 0, length);               

                MStream.Flush();               

               MStream.Position = 0;                

                BinaryFormatter BFormatter = new BinaryFormatter();               

               defineclass TDO = new defineclass();               

               TDO = BFormatter.Deserialize(MStream) as defineclass;

                Console.WriteLine("name:{0}" + "\r\n" + "age:{1}" + "\r\n" + "gender:{2}" + "\r\n" + "x:{3}" + "\r\n" + "y:{4}" + "\r\n", TDO.name, TDO.age, TDO.gender, TDO.x, TDO.y);            

}

 

        }

        static void Main(string[] args)  

       {            

   Thread thread = new Thread(new ThreadStart(StartListener));           

  thread.Start();        

}    

}

}

 

3,编写服务器端,和客户端一样也要调用刚才DLL库

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization.Formatters;

using System.Runtime.Serialization;

using PointObjector;

namespace UDPServer {    

class Program     {

        public static Stream serializeStream(object param)       

      {            

         DataContractSerializer serializer = new DataContractSerializer(param.GetType());     

        MemoryStream stream = new MemoryStream();

            serializer.WriteObject(stream, param);

            return stream;       

  }

        public static void send()      

   {                  

while (true)          

   {               

          Console.WriteLine("请输入内容:" + "\r\n");              

           defineclass TempObjector = new defineclass();                

         TempObjector.name = Console.ReadLine();        

         TempObjector.age = Int32.Parse(Console.ReadLine());       

          TempObjector.gender = Console.ReadLine();

                TempObjector.x = Int32.Parse(Console.ReadLine());

                TempObjector.y = Int32.Parse(Console.ReadLine());

                Socket groupsocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.0.0.1"), 3222);

                EndPoint ep = (EndPoint)iep;

 

               MemoryStream mStream = new MemoryStream();  

               BinaryFormatter bformatter = new BinaryFormatter();

                bformatter.Serialize(mStream, TempObjector); //序列化

                mStream.Flush();

                byte[] buffer = new byte[500];

                mStream.Position = 0;

                //mStream.Read(buffer, 0, buffer.Length);  

               int i = mStream.Read(buffer, 0, buffer.Length);   

              Console.WriteLine("i:{0}",i);

                groupsocket.SendTo(buffer, ep);

                groupsocket.Close();

            }

        }  

       static void Main(string[] args)

        {  

           send();

            return;

        }

    }

}

 

posted on 2013-03-04 18:20  阿甘正传  阅读(406)  评论(0)    收藏  举报