序列化数据集并通过套接字发送 [英] serializing dataset and sending it through socket

查看:87
本文介绍了序列化数据集并通过套接字发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在vb.net中创建一个Web应用程序。我想从服务器向客户端发送200多条记录,因此我将所有记录存储在数据集对象中并通过套接字发送该对象。现在我知道我应该序列化对象&通过套接字发送,但我遇到问题。

这是我的代码:

 sql =  选择Transaction_ID,Log_Date,Log_Time,Client_Action,来自ClientLogs的备注,其中Account_No =& ClientAccountNumber&   
dataadapter = OleDbDataAdapter(sql,connection)
dataadapter.Fill(dataset, ClientLogs
connection.Close()
dataadapter.Dispose()

Dim send_to_client 作为 字节()
send_to_client = Encoding.ASCII.GetBytes(dataset& Chr( 13 ))
networkStream.Write(send_to_client, 0 ,send_to_client.Length)
networkStream.Flush()





我对二进制serializa一无所知(我不想要xml序列化)。

我试图收集一些关于< serializable()>的信息。但我没有得到太多。有没有办法通过套接字使用可序列化的类发送数据集对象,并在服务器端反序列化数据集对象。

在我的项目中,数据库在服务器端。



欢迎任何建设性的建议。

提前感谢你

解决方案

< blockquote>如果您要使用二进制序列化,那么在将序列化对象发送到另一个应用程序时遇到的一个大问题是使用二进制格式化程序序列化的对象不仅记录了类名,还记录了命名空间和程序集名称。因此,即使您通过tcp成功序列化然后传输对象,当您尝试在远程应用程序中对其进行反序列化时,它也会失败。



处理方式这是使用 SerializationBinder.BindToType



使用BindToType,您可以在反序列化之前查看对象,并指示BinaryFormatter从当前程序集中获取目标对象类型。



我在codeproject上有一个TCP库,允许您在客户端和服务器之间以及连接到服务器的客户端之间发送序列化对象。该代码可在下载文章中找到:



https://www.codeproject.com/Articles/1192106/Abstracting-TCP-Communications-and-adding-what-sho



我会下载图书馆看看 - 它应该做你需要的。如果您仍然有兴趣自己动手,请看一下我如何设置BindToType(在c#中):



 < span class =code-keyword> public   class  TypeConvertor:System.Runtime.Serialization.SerializationBinder 
{
private 程序集targetAssembly;
public TypeConvertor(Assembly targetAssembly)
{
this .targetAssembly = targetAssembly;
}

public 覆盖类型BindToType( string assemblyName, string typeName)
{
类型returntype = ;
assemblyName = targetAssembly.FullName;
Assembly.Load(assemblyName);

returntype = Type.GetType( String .Format( {0},{1},typeName,assemblyName), false );

// 在VB.NET中,typeName也可能包含默认命名空间。所以我们的远程应用程序
// 是一个VB.NET应用程序,我们需要删除:
if (returntype == null && typeName .Contains( ))
{
// 在此删除默认命名空间:
String vbAssemName = Regex.Split(typeName, @ \。)[ 0 ] + ;
typeName = typeName.Replace(vbAssemName, );

// 并尝试获取返回类型(我们的targetAssembly可能是C#应用程序)
returntype = Type.GetType( String .Format( {0},{1},typeName,assemblyName), false );

// 如果targetAssembly是VB.NET应用程序,我们需要获取当前默认值命名空间:
if (returntype == null
{
尝试
{
// 目标程序集中的所有类型都应该相同。我们只需要一个:
类型[] types = targetAssembly.GetTypes();
if (types.Length > 0
{
// 并将其包含在我们的typename中:
returntype = Type.GetType( String .Format( {0},{1},Regex.Split(类型[ 0 ]。FullName, @ \。)[ 0 ] + + typeName,assemblyName), false );
}
}
catch (例外){}
}
}

return returntype;
}
}





您需要查看的代码,以查看序列化和反序列化对象的示例在该项目中可以在第399和465行之间的Core.cs中找到。



- Pete


I am creating a web application in vb.net. I want to send more than 200 records from server to client, so I have stored all the records in the dataset object and sending that object through socket. Now I know that I should serialize the object & send it through socket, but I am facing problem.
Here is my code:

sql = "Select Transaction_ID,Log_Date,Log_Time,Client_Action,Remarks from ClientLogs  where Account_No=" & ClientAccountNumber & ""
                          dataadapter = New OleDbDataAdapter(sql, connection)
                          dataadapter.Fill(dataset, "ClientLogs")
                          connection.Close()
                          dataadapter.Dispose()
                         
                          Dim send_to_client As Byte()
                          send_to_client = Encoding.ASCII.GetBytes(dataset & Chr(13))
                          networkStream.Write(send_to_client, 0, send_to_client.Length)
                          networkStream.Flush()



I have very little idea about binary serialization(I don't want xml serialization).
I tried to collect some info about <serializable()> but I didn't get much.Is there any way to send the data set object through socket using serializable class and deserialize the dataset object oat the server side.
In my project, database is at server side .

Any constructive suggestions are welcome.
Thanking you in advance

解决方案

If you're going to use binary serialization, the big problem you'll run into when sending serialized objects to another application is that objects serialized using binary formatter record not only the class name, but the namespace and the assembly name as well. So even if you successfully serialize and then transfer an object via tcp, when you attempt to deserialize it in the remote application, it will fail.

The way to deal with this is to use SerializationBinder.BindToType.

Using BindToType, you can peek into the object before it is deserialized, and instruct BinaryFormatter to get the target Object Type from current assembly.

I have a TCP library here on codeproject that allows you to send serialized objects between client and server, and also between clients connected to your server. The code is available in the download for the article:

https://www.codeproject.com/Articles/1192106/Abstracting-TCP-Communications-and-adding-what-sho

I would download the library and have a look - it should do what you need. If you're still interested in doing it yourself, here's a look at how I set up BindToType (in c#):

public class TypeConvertor : System.Runtime.Serialization.SerializationBinder
            {
                private Assembly targetAssembly;
                public TypeConvertor(Assembly targetAssembly)
                {
                    this.targetAssembly = targetAssembly;
                }

                public override Type BindToType(string assemblyName, string typeName)
                {
                    Type returntype = null;
                    assemblyName = targetAssembly.FullName;
                    Assembly.Load(assemblyName);

                    returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);

                    // In VB.NET, the typeName may contain the default namespace also. So our if our remote application
                    // is a VB.NET application, we need to remove that:
                    if (returntype == null && typeName.Contains("."))
                    {
                        // Remove the default namespace here:
                        String vbAssemName = Regex.Split(typeName, @"\.")[0] + ".";
                        typeName = typeName.Replace(vbAssemName, "");

                        // And attempt to get the returntype (our targetAssembly may be a C# application)
                        returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);

                        // If targetAssembly is a VB.NET application, we need to get the current default namespace:
                        if (returntype == null)
                        {
                            try
                            {
                                // All the types in our target assembly should be the same. We only need one:
                                Type[] types = targetAssembly.GetTypes();
                                if (types.Length > 0)
                                {
                                    // And include it in our typename:
                                    returntype = Type.GetType(String.Format("{0}, {1}", Regex.Split(types[0].FullName, @"\.")[0] + "." + typeName, assemblyName), false, true);
                                }
                            }
                            catch (Exception) { }
                        }
                    }

                    return returntype;
                }
            }



The code you will want to look at to see an example of serializing and deserializing objects in that project can be found in Core.cs between lines 399 and 465.

- Pete


这篇关于序列化数据集并通过套接字发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆