将数据(消息)发送到服务器的最佳(广泛接受)方式是什么
我有一个 C# 应用程序(这是客户端)和一个服务器。现在服务器获取并向客户端发送各种字符串消息,我使用 StreamWriter 来实现此目的,现在客户端和服务器上的发送消息看起来非常相同,我采用字符串将其编码为 UTF-8 然后立即发送
public void SendMessage(String p)
{
if (p != "")
{
string StringMessage = HttpUtility.UrlEncode(p, System.Text.Encoding.UTF8);
try
{
swSender.WriteLine(StringMessage);
swSender.Flush();
}
catch (IOException e)
{
//do some stuff
}
}
}
,我发送的字符串是这样的:
"SUBJECT@@@@SOMEDATA1<><>SOMEDATA2<><>SOMEDATA3
这会导致一些问题,并让我思考。这是大型应用程序发送/接收数据的方式吗?因为它看起来很傻。如果不是,那么有人可以提供一个关于大型应用程序如何发送消息的示例吗?
另外:我发送消息的方式使我进行大嵌套,
例如:
if(Subject="something")
do something
else if(subject="something else")
do something else
我该如何解决这个问题?
I have a C# application (which is the client) and I have a server. Now the server gets and sends all sorts of messages which are strings to the client, I am using StreamWriter
for this, now the sending message on the client and the server looks pretty the same, I take the string encode it to UTF-8 and then send it
public void SendMessage(String p)
{
if (p != "")
{
string StringMessage = HttpUtility.UrlEncode(p, System.Text.Encoding.UTF8);
try
{
swSender.WriteLine(StringMessage);
swSender.Flush();
}
catch (IOException e)
{
//do some stuff
}
}
}
now,the strings I send is something like this:
"SUBJECT@@@@SOMEDATA1<><>SOMEDATA2<><>SOMEDATA3
This causes some problems, and makes me think. Is this the way big applications send/ receive data? Because it looks pretty silly. If no, then can some one provide an example on how big applications send messages?
Also: my way of sending messages makes me make big nested if
For example:
if(Subject="something")
do something
else if(subject="something else")
do something else
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这在很大程度上取决于您的应用程序的需求。
一般来说:不,发明自己的协议不是一个好主意。
有很多方法可以将消息从客户端发送到服务器。
我建议您阅读一些有关 WCF 的文章,或者如果您使用的是 .NET 2.0,而不是 .NET Remoting。
另外,您可能需要考虑发送 HTTP 消息,因为有大量框架可以做到这一点。
It all greatly depends on your application's needs.
Generally speaking: no, inventing your own protocol is not a good idea.
There are quite a few ways to send messages from client to server.
I'd suggest you to do some reading on WCF, or if you are in .NET 2.0 than .NET Remoting.
Also, you might want to consider to send HTTP messages, as there are a shitload of frameworks to do that.
一种方法是使用 XML-RPC。我在 .NET 中使用了这个。我按照说明进行操作,但没有修改它,并在 30 分钟内让客户端/服务器正常工作,并在另外 10 分钟内将其修改为我喜欢的。本质上,您正常调用函数,通过库的魔力,它将阻止服务器执行代码并返回结果。 RPC = 远程过程调用。
如果您使用 asp.net,请使用标记为 IIS 的说明,即使您在 Linux 上使用 fastcgi 或 apache。我忽略了这个错误,因为它被标记为 IIS。有一个可用的 .NET Remoting 选项(如果服务器不是 asp.net,而是另一个应用程序)。
一个不太好的选择是学习 Web 客户端并将 json 字符串发布到服务器。然后将响应读取为 json。 XML-RPC 是非常标准和建议的。
One way is to use XML-RPC. I used this for .NET. I followed the instructions w/o modifying it and got the client/server working within 30mins and another 10 to modify it to my liking. Essentially you call functions normally and through the magic of the library it will block for the server to execute the code and it will return results. RPC = remote procedure call.
If your using asp.net use the instructions labeled IIS even if your on linux using fastcgi or apache. I ignored that which was a mistake because it was labeled IIS. There is a .NET Remoting option (if the server isnt asp.net but another app) thats available.
A not as good option is to learn webclient and post json strings to the server. Then read the response as json. XML-RPC is pretty standard and suggested.
尝试使用 HttpUtility.HtmlEncode 方法
代替 UrlEncode ()
try to use HttpUtility.HtmlEncode Method
instead UrlEncode()