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

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

问题描述

我(第一次)用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.

此外,我正在尝试避免第三方的实施,以便我自己学习如何做..

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天全站免登陆