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

本文是小编为大家收集整理的关于关于服务器套接字编程模型的问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在过去的几个月中,我一直在研究C ++和Java中插座服务器的一些实现.我在Java中编写了一台小型服务器,该服务器将从网站上托管的Flash应用程序中处理和处理输入,并成功地编写了一台服务器,该服务器可以处理来自2D游戏客户端的输入,并在C ++中具有多个播放器.我在另一个项目中使用了TCP,另一个项目中使用了TCP.现在,我确实有一些我在网上找不到的问题,希望有些专家可以帮助我. :)

假设我想在C ++中构建一台服务器,该服务器可以处理数千个独立和/或Web应用程序的输入,那么我应该如何设计服务器?到目前为止,我通常为连接的每个用户创建一个新的独特线程,但我怀疑这是要走的方式.

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

和最后,是否有任何易于使用的库,这些库通常是支持可移植性的(例如在Linux框上的Windows Machine和Linux Box上的部署).

谢谢.

推荐答案

听起来您在这里有几个问题.我会尽力回答我可以看到的.

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

我会很好地看一下您在服务器产生的工作线程上所做的工作.为每个请求产生一个新线程不是一个好主意...但是如果并行请求的数量很小,并且在每个线程上执行的任务也很快运行.

如果您真的想以正确的方式进行操作,则可以拥有一个可配置/动态线程池,该池将在工程线变得自由时回收这些工作线程.这样,您可以设置最大线程池大小.然后,您的服务器将工作到池尺寸...然后提出进一步的请求,等到工作线程可用.

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

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

3.我将哪种格式用于序列化?

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

XML序列化是另一个选择.它将花费更长的时间,并且要通过网络传输大量数据.使用XML序列化之类的内容的好处是,您不仅限于可以连接到服务器并消耗服务的客户类型.

您必须选择最适合您的需求.

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

其他推荐答案

就服务器设计关注而言,我会说您是对的:尽管单插入是一种简单简单的方法,但它不是一个方法,因为它不会扩展为以及其他服务器设计模式.

我个人喜欢Communication-threads/Worker-threads方法,其中一个动态数量的工作线程池处理生产者线程生成的所有工作.

在此模型中,您将在池中有许多线程,等待将从另一组线程处理网络I/O.

生成的任务.

我找到了 unix networking以及此类网络编程方法的惊人来源.而且,尽管它的名称,但它在Windows环境中也将非常有用.

关于数据包的布局(我认为这是一个完全不同的问题,您应该在此问题上发布一个不同的问题),选择文本与二进制方法时会有权衡.

文本(即XML)可能更容易解析和文档,而且通常更简单,而二进制协议应该在处理速度和网络数据包的速度方面为您提供更好的性能,但是您必须处理更复杂的问题,例如单词和类似的东西.

希望它有帮助.

其他推荐答案

尽管以前的答案提供了良好的方向,只是为了完整性,我想指出的是,线程并不是出色的插座服务器性能的绝对要求.一些示例是在这里.也有许多可扩展性的方法 - 线程池,预叉进程,服务器池等.

本文地址:https://www.itbaoku.cn/post/627388.html

问题描述

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. :)

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.

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)?

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.

Thank you.

推荐答案

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

1. How should I handle threading in my network server?

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. How do I format the data in my packets?

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. Which format do I use for serialization?

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 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.

其他推荐答案

As far as server design concern, I would say that you are right: although ONE-THREAD-PER-SOCKET is a simple and easy approach, it is not the way to go since it won't scale as well as other server design patterns.

I personally like the COMMUNICATION-THREADS/WORKER-THREADS approach, where a pool of a dynamic number of worker threads handle all the work generated by producer threads.

In this model, you will have a number of threads in a pool waiting for tasks that are going to be generated from another set of threads handling network I/O.

I found UNIX Network Programming by Richard Stevens and amazing source for this kind on network programming approaches. And, despite its name, it will be very useful in windows environments as well.

Regarding the layout of the packets (you should have post a different question for this since it is a totally different question, in my opinion), there are tradeoffs when selecting TEXT vs BINARY approach.

TEXT (i.e. XML) is probably easier to parse and document, and more simple in general, while a BINARY protocol should give you better performance in terms of speed of processing and size of network packets, but you will have to deal with more complicated issues such as ENDIANNES of the words and stuff like that.

Hope it helps.

其他推荐答案

Though previous answers provide good direction, just for completeness, I'd like to point out that threads are not an absolute requirement for great socket server performance. Some examples are here. There are many approaches to scalability too - thread pools, pre-forked processes, server pools, etc.

相关标签/搜索