发送套接字用于不同目的 [英] Send sockets for different purposes

查看:76
本文介绍了发送套接字用于不同目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如果我有一个客户端和一个服务器程序,并且想从服务器调用不同的任务,我该怎么做?例如,如果我想将数据发送到服务器并使服务器将此数据存储在数据库中,或者我想从服务器上的数据库接收数据到客户端?



我应该写一个带有标题的数据包告诉服务器如何处理数据?或者还有其他方法吗?

Hi,

If I have a client and a server program and want to call different tasks from the server how would I do this? For example if I want to send data to the server and make the server store this data in a database or if I want to receive data to the client from the database on the server?

Should I write a packet with a header telling the server what to do with the data? or is there other ways of doing this?

推荐答案

有很多方法可以实现这一点,但有一些标题是个好主意。通常,所有通信都以前缀形式更好。比如,你有一个可变大小的消息。当然,最好先说出尺寸,读者会读到足够的字节数。同样的事情是可变内容的消息。您可能有一些读取器首先接收的字段,然后,根据 descriptor 上的此信息,读者知道此消息其余部分的预期结构,并且可以按层次结构完成。



你的问题是模糊地讨论它的更详细的方法,但我希望你明白这个想法。



但是,所有这些考虑因素对于应用程序而言可能太低。问题是:这个问题的整个复杂性已经为你解决了。如果您真的想使用原始套接字,而不是WCF,而不是.NET远程处理,则可以将套接字流与预序列化数据一起使用。在其他形式中,使用序列化将让您交换编程对象而不是原始数据,这将在读取端自动反序列化。



请参阅:

http://en.wikipedia.org / wiki /序列化 [ ^ ],
http://msdn.microsoft.com/en -us / library / 7ay27kt9%28v = vs.110%29.aspx [ ^ ]。



在很多情况下,我建议使用最好的.NET FCL中提供的序列化(至少在某些方面) - 数据合同

http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



请查看我过去的答案:

如何在表单应用程序中使用XML文件源代码和阅读器? [ ^ ],

创建属性文件...... [ ^ ],

解析json字符串数组 [ ^ ]。



-SA
There are many ways to achieve that, but having some header is a good idea. Generally, all communications are better in prefixed form. Say, you have a message with variable size. Naturally, it's the best to tell the size first, the the reader would read just enough bytes. Same thing is with the messages of variable content. You may have some field which the reader receives first, and then, based on this information as on descriptor, the reader knows the expected structure of the rest of this message, and it can be done hierarchically.

Your question is way to vague to discuss it in further detail, but I hope you get the idea.

However, all such considerations might be too low-level for the application. The thing is: the whole complex of such problem is already solved for you. If you really want to use raw sockets, not WCF, not .NET remoting, you can use your socket streams with pre-serialized data. In other forms, using of serialization will have you to exchange your programming objects instead of raw data, which will be automatically deserialized on the reading end.

Please see:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^].

In many cases, I would recommend to use the best serialization (at least in some aspects) offered in .NET FCL — Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


我认为使用Web服务最好和最简单的想法。我认为使用任何类型的套接字都没有任何意义。如果您能够编写Web服务,您的生活将变得更加轻松。关于如何在dotnet中编写web服务的谷歌,你可以选择ASMX或WCF网络服务。



任何帮助,在这里发布



谢谢

Ganesh
I think the best and simplest idea for this using a web service. I think there is nothing involved in working with any type of sockets. If you can able to write a web services your life becomes easier. Google it on how to write a webservice in dotnet, you can choose between ASMX or WCF web services.

Any help, post it here

Thanks
Ganesh


这篇关于发送套接字用于不同目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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