服务器如何处理来自多个客户端的 Web 服务请求 [英] How does a server handle web service requests from multiple clients

查看:33
本文介绍了服务器如何处理来自多个客户端的 Web 服务请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个使用 Web 服务连接到远程数据库的 Android 应用程序.我在本地工作.

I just completed an Android application that uses web services to connect to a remote database. I was working on localhost.

现在,我计划在服务器上托管我的 Web 服务.假设我在任意数量的不同客户端智能手机上安装了我的 Android 应用程序.每个智能手机用户同时调用网络服务.

Now, I plan to host my web services on a server. Let's say I have my Android application installed on any number of different client smartphones. Each smartphone user calls the web service at the same time.

现在服务器如何处理这些请求?每个请求执行一个线程吗?我想详细了解服务器处理.考虑到所有手机都使用GPRS,在这种情况下会不会有任何延迟?

Now how does the server handle these requests? Does it execute one thread per request? I want to know about the server processing in detail. Considering, all phones use GPRS, will there be any sort of delay in such a situation?

顺便说一句,我的 Web 服务都是基于 SOAP 的,我打算稍后使用的服务器将是 SQL Server.我已经使用 .NET 框架来创建 Web 服务.

BTW, my web services are all SOAP based and the server I plan to use later will be an SQL Server. I have used .NET framework for creating web services.

推荐答案

它是通用的概念,不是 Android 特定的

Its for the general concept, not a Android specific

通常,每个用户都会为页面发送一个 HTTP 请求.服务器接收请求并将它们委托给不同的工作者(进程或线程).

Usually, each of the users sends an HTTP request for the page. The server receives the requests and delegates them to different workers (processes or threads).

根据给定的 URL,服务器读取文件并将其发送回用户.如果文件是动态文件,例如 PHP 文件,则在将文件发送回用户之前执行该文件.

Depending on the URL given, the server reads a file and sends it back to the user. If the file is a dynamic file such as a PHP file, the file is executed before it's sent back to the user.

一旦请求的文件被发回,服务器通常会在几秒钟后关闭连接.

Once the requested file has been sent back, the server usually closes the connection after a few seconds.

查看 Web 服务器的工作原理

HTTP 使用 TCP,它是一种基于连接的协议.也就是说,客户端在与服务器通信时建立 TCP 连接.

For HTTP uses TCP which is a connection-based protocol. That is, clients establish a TCP connection while they're communicating with the server.

允许多个客户端同时连接到同一目标机器上的同一目标端口.服务器只是打开了多个同时连接.

Multiple clients are allowed to connect to the same destination port on the same destination machine at the same time. The server just opens up multiple simultaneous connections.

Apache(和大多数其他 HTTP 服务器)有一个多处理模块 (MPM).这负责分配 Apache 线程/进程来处理连接.然后,这些进程或线程可以在它们自己的连接上并行运行,而不会相互阻塞.即使没有连接打开,Apache 的 MPM 也倾向于保持打开的备用"线程或进程,这有助于加快后续请求.

Apache (and most other HTTP servers) have a multi-processing module (MPM). This is responsible for allocating Apache threads/processes to handle connections. These processes or threads can then run in parallel on their own connection, without blocking each other. Apache's MPM also tends to keep open "spare" threads or processes even when no connections are open, which helps speed up subsequent requests.

注意:

多线程最常见的问题之一是竞争条件"--,其中两个请求在做同样的事情(竞相"做同样的事情),如果是单一资源,其中之一将获胜.如果他们都将一条记录插入到数据库中,他们就不能获得相同的 id——其中一个将获胜.因此,在编写代码时需要小心,以意识到其他请求正在同时进行,并且可能会修改您的数据库、写入文件或更改全局变量.

One of the most common issues with multi-threading is "race conditions"-- where you two requests are doing the same thing ("racing" to do the same thing), if it is a single resource, one of them is going to win. If they both insert a record into the database, they can't both get the same id-- one of them will win. So you need to be careful when writing code to realize other requests are going on at the same time and may modify your database, write files or change globals.

这篇关于服务器如何处理来自多个客户端的 Web 服务请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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