关于服务器套接字编程模型的问题 [英] Question about server socket programming model

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

问题描述

在过去的几个月中,我一直在C ++和Java中实现套接字服务器。我在Java中编写了一个可以处理&从网站上托管的Flash应用程序的进程输入,我成功地编写了一个服务器来处理来自2D游戏客户端的输入,C ++中有多个玩家。我在一个项目& UDP在另一个。现在我有一些问题,我真的不能在网上找到,希望有些专家可以帮助我。 :)

Over the last couple of months I've been working on some implementations of sockets servers in C++ and Java. I wrote a small server in Java that would handle & process input from a flash application hosted on a website and I managed to successfully write a server that handles input from a 2D game client with multiple players in C++. I used TCP in one project & UDP in the other one. Now, I do have some questions that I couldn't really find on the net and I hope that some of the experts could help me. :)

我想用C ++构建一个可以处理来自数千个独立和/或Web应用程序的输入的服务器,我应该如何设计我的服务器?到目前为止,我通常创造一个新的&连接每个用户的唯一线程,但我怀疑这是要走的路。

Let's say I would like to build a server in C++ that would handle the input from thousands of standalone and/or web applications, how should I design my server then? So far, I usually create a new & unique thread for each user that connects, but I doubt this is the way to go.

另外,如何确定通过网络发送的数据包的布局?数据通常是通过网络以二进制还是文本状态发送?当您将数据发送到不同的媒体(例如C ++服务器到Flash应用程序)时,如何处理串行化的对象?

Also, How does one determine the layout of packets sent over the network; is data usually sent over the network in a binary or text state? How do you handle serializated objects when you send data to different media (eg C++ server to flash application)?

最后,是否有任何易于使用的库,通常是用于支持可移植性(例如在Windows机器上开发和在linux机箱上部署),而不是boost asio。

And last, is there any easy to use library which is commonly used that supports portability (eg development on a windows machine & deployment on a linux box) other than boost asio.

谢谢。

推荐答案

听起来你在这里有几个问题。我会尽力回答我能看到的内容。

Sounds like you have a couple of questions here. I'll do my best to answer what I can see.

1。我应该如何处理网络服务器中的线程?

我会很好的看​​看你正在做什么工作的工作线程是由您的服务器产生。为每个请求产生一个新的线程不是一个好主意...但是如果并行请求的数量很小,并且每个线程上执行的任务正在快速运行,它可能不会受到任何影响。

I would take a good look at what kind of work you're doing on the worker threads that are being spawned by your server. Spawning a new thread for each request isn't a good idea...but it might not hurt anything if the number of parallel requests is small and and tasks performed on each thread are fast running.

如果你真的想以正确的方式做事情,你可以有一个可配置/动态的线程池,可以在工作线程自由时回收工作线程。这样你可以设置一个最大线程池大小。然后,您的服务器将处理池大小...然后再进一步请求等待工作线程可用。

If you really want to do things the right way, you could have a configurable/dynamic thread pool that would recycle the worker threads as they became free. That way you could set a max thread pool size. Your server would then work up to the pool size...and then make further requests wait until a worker thread was available.

2。如何格式化数据包中的数据?

除非你正在开发一个全新的协议...这不是你真正需要的担心除非您正在处理流媒体(或可以接受数据包丢失/损坏的其他应用程序),否则您可能不会在此应用程序中使用UDP。 TCP / IP可能是您最好的选择,这将决定您的数据包设计。

Unless you're developing an entirely new protocol...this isn't something you really need to worry about. Unless you're dealing with streaming media (or another application where packet loss/corruption is acceptable), you probably won't be using UDP for this application. TCP/IP is probably going to be your best bet...and that will dictate the packet design for you.

3。我使用哪种格式进行序列化?

通过电子邮件序列化数据的方式取决于哪种应用程序将耗用您的服务。二进制序列化通常更快,导致需要通过网络传输的数据量较少。使用二进制序列化的缺点是,一种语言的二进制序列化可能在另一种语言中不起作用。因此,连接到您的服务器的客户端很可能必须使用您使用的相同语言编写。

The way you serialize your data over the wire depends on what kind of applications are going to be consuming your service. Binary serialization is usually faster and results in a smaller amount of data that needs to be transfered over the network. The downside to using binary serialization is that the binary serialization in one language may not work in another. Therefore the clients connecting to your server are, most likely, going to have to be written in the same language you are using.

XML序列化是另一种选择。这将需要更长时间,并且有更大量的数据通过网络传输。使用像XML序列化这样的东西的优势在于,您不会受限于可以连接到服务器并使用您的服务的客户端类型。

XML Serialization is another option. It will take longer and have a larger amount of data to be transmitted over the network. The upside to using something like XML serialization is that you won't be limited to the types of clients that can connect to your server and consume your service.

您必须选择最适合您需要的东西。

You have to choose what fits your needs the best.

...玩不同的选项,找出最适合您的内容。希望你会发现比我在这里提到的任何东西都能更快更可靠地执行。

...play around with the different options and figure out what works best for you. Hopefully you'll find something that can perform faster and more reliably than anything I've mentioned here.

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

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