如何通过socket发送和接受dataset数据集(C#)

//以下是属于客户端的代码



using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Data;

using System.Net.Sockets;

using System.Data.SqlClient;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Net;





namespace ConsoleApplication1

{

     class Program

     {  

         public static byte[] GetBinaryFormatDataSet(DataSet ds)

         {

             //创建内存流

             MemoryStream memStream = new MemoryStream();

             //产生二进制序列化格式

             IFormatter formatter = new BinaryFormatter();

             //指定DataSet串行化格式是二进制

             ds.RemotingFormat = SerializationFormat.Binary;

             //串行化到内存中

             formatter.Serialize(memStream, ds);

             //将DataSet转化成byte[]

             byte[] binaryResult = memStream.ToArray();

             //清空和释放内存流

             memStream.Close();

             memStream.Dispose();

             return binaryResult;

         }



         static void Main(string[] args)

         {

             //

             // TODO: 在此处添加代码以启动应用程序

             //

             SqlConnection conn = new SqlConnection("server=.;database=news;uid=sa;pwd=sa;");

            

             conn.Open();

             String selstr = "select * from newsType";

             SqlDataAdapter adapter = new SqlDataAdapter(selstr, conn);

             DataSet ds = new DataSet();

             adapter.Fill(ds, "phonetab");

             conn.Close();

             byte[] input = GetBinaryFormatDataSet(ds);





             try

             {

                 //远程的ip地址和端口号

                 IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");

                 IPEndPoint ipep = new IPEndPoint(ip, 3434);

                 Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);



                 try

                 {

                     server.Connect(ipep);

                 }

                 catch (Exception e)

                 {

                     Console.WriteLine(e.ToString());

                 }

                 server.Send(input);

                 byte[] response = new byte[1024];

                 int bytesRead = server.Receive(response);

                 Console.WriteLine(Encoding.ASCII.GetString(response, 0, bytesRead));

                 server.Shutdown(SocketShutdown.Both);

                 server.Close();

             }

             catch (Exception e)

             {

                 Console.WriteLine(e.ToString());

             }

             Console.ReadLine();         

         }  

     }

}





//一下是属于服务器端的代码



using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Data;

using System.Net.Sockets;

using System.Data.SqlClient;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Net;

namespace ConsoleApplication2

{

     class Program

     {

         public static DataSet ds;

         public static DataSet RetrieveDataSet(byte[] binaryData)

         {

             //创建内存流

             MemoryStream memStream = new MemoryStream(binaryData);

             memStream.Seek(0, SeekOrigin.Begin);

             //产生二进制序列化格式

             IFormatter formatter = new BinaryFormatter();

             //反串行化到内存中

             object obj = formatter.Deserialize(memStream);

             //类型检验

             if (obj is DataSet)

             {

                 DataSet dataSetResult = (DataSet)obj;

                 return dataSetResult;

             }

             else

             {

                 return null;

             }

         }

         static void Main(string[] args)

         {



             IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");

             IPEndPoint ipep = new IPEndPoint(ip, 3434);

             Socket lst = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

             lst.Bind(ipep);

             lst.Listen(20);



             while (true)

             {

                 Console.Write("等待连接.......");

                 Socket client = lst.Accept();

                 Console.WriteLine("客户已连接");



                 byte[] request = new byte[512];

                 int bytesRead = client.Receive(request);

                 //ds = RetrieveDataSet(request);

                 string input = Encoding.ASCII.GetString(request, 0, bytesRead);



                 Console.WriteLine("客户请求:{0}", input);



                 string output = "hello, " + input + "!";

                 byte[] hello = Encoding.ASCII.GetBytes(output);



                 try

                 {

                     client.Send(hello);

                     client.Shutdown(SocketShutdown.Both);

                     client.Close();

                 }

                 catch (Exception e)

                 {

                     Console.WriteLine(e.ToString());

                 }

             }



         }



     }

}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值