套接字编程设计问题 [英] Socket Programming design questions

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

问题描述

我正在(第一次)在 C# 中使用 Socket 编程,我正在制作一个类似 Skype 的应用程序(视频通话、IM、文件共享、屏幕共享),我有几个问题......

I'm messing around (for the first time) with Socket programming in C#, I'm making a Skype like application(Video call, IM, file share, screen share) and i have a couple of questions...

1) 套接字应该如何真正工作?在客户端,我有一个有效地保持套接字打开的 while 循环,这是正确的吗?或者我应该在每次发送/接收之后关闭套接字(我使用 BeginSend() 和 BeginRecieve())并创建一个新的套接字?建立连接后调用interact().我的代码:

1) How should sockets truly work? On the client side I have a while loop which is effictively keeping the socket open, Is this correct? Or should i be closing the socket after each Send/Recieve (Im using BeginSend() and BeginRecieve()) and creating a new socket? interact() is called after the connection has been made. my code:

private static void interact()
    {
        try
        {
            while (true)
            {
                receive(client);
                send(client);
            }
        }
        catch (Exception e)
        {
            Logging.errorDisconnect(client, e);
        }
    }

2) 您将如何通过使用 System.Net.Sockets 中的 BeginSend/BegingRecieve 或创建您自己的 Task 实现来设计一个健壮的客户端/服务器应用程序?

2) How would you design a robust client/server application, by using BeginSend/BegingRecieve from System.Net.Sockets, or creating your own Task implementation?

3) 是否有关于用于健壮/可扩展应用程序的客户端/服务器架构的好教程?我看过 P2P,但我不完全确定它是我需要的.

3) Is there any good tutorials on Client/Server architecture for robust/scaleable applications? I've had a look at P2P but i'm not entirely sure its what i need.

另外,我试图避免使用 3rd 方实现,这样我就可以了解它是如何自己完成的..

Also, i'm trying to avoid 3rd party implementations so i can learn how it's done myself..

提前致谢..

推荐答案

我假设您想要运行持久连接并偶尔向远程端发送命令.这通常意味着双方必须始终运行读取循环以接收命令.读取和写入之间的交替对于您在此处没有的请求/回复模型是有意义的(因为双方都可以发送请求,您可能会将传入的请求误认为是预期的回复).

I assume you want to run a persistent connection and occasionally send commands to the remote side. This usually means that both sides must have a read loop running at all times to receive commands. Alternating between reading and writing makes sense for a request/reply model which you do not have here (because both sides can send requests and you might mistake an incoming request for an expected reply).

循环没有使套接字保持活动状态.如果你告诉我为什么你认为我可以澄清.您可以独立于任何类型的循环"来决定连接的生命周期.

The loop is not keeping the socket alive. If you tell me why you were thinking that I can clarify. You decide the lifetime of the connection independently from any kind of "loop".

您想使用哪种调用方式(同步、APM、TAP)不会影响套接字的行为方式或通过线路传输的内容.您可以自由选择.套接字超级难搞定,从同步 IO 开始.异步 IO 相当困难,在这里可能没有必要.

What kind of call style you want to use (sync, APM, TAP) does not affect how the socket behaves or what goes over the wire. You can choose freely. Sockets are super hard to get right, start with synchronous IO. Async IO is considerably harder and likely unnecessary here.

一般来说,您应该尽量避免使用套接字,因为它们很难且级别较低.尝试使用一些更高级别的 RPC 机制,例如 WCF 或 HTTP.如果你坚持自定义线格式protobuf是一个不错的选择.

In general you should try hard to avoid using sockets because they are difficult and low-level. Try using some higher-level RPC mechanism such as WCF or HTTP. If you insist on a custom wire format protobuf is a good choice.

这篇关于套接字编程设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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