Unix TCP服务器和UDP服务器 [英] Unix TCP servers and UDP Servers

查看:196
本文介绍了Unix TCP服务器和UDP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么TCP服务器的设计主要是这样的,每当它接受一个连接,一个新的进程被调用来处理它。但是,为什么在UDP服务器的情况下,大多数只有一个进程来处理所有客户端请求?

Why is the design of TCP servers mostly such that whenever it accepts a connection, a new process is invoked to handle it . But, why in the case of UDP servers, mostly there is only a single process that handles all client requests ?

推荐答案

如前所述,TCP和UDP之间的区别在于UDP是无连接的。

The main difference between TCP and UDP is, as stated before, that UDP is connectionless.

使用UDP的程序只有一个套接字,它接收消息。

A program using UDP has only one socket where it receives messages. So there's no problem if you just block and wait for a message.

如果使用TCP,你为每个连接的客户端提供一个套接字。然后你不能阻塞并等待一个套接字接收一些东西,因为有其他套接字必须同时处理。

所以你有两个选项,使用非阻塞方法或使用线程。当没有一个while循环必须处理每个客户端时,代码通常要简单得多,因此通常优先使用线程。如果使用阻止方法,您还可以节省一些CPU时间。

If using TCP you get one socket for every client which connects. Then you can't just block and wait for ONE socket to receive something, because there are other sockets which must be processed at the same time.
So you got two options, either use nonblocking methods or use threads. Code is usually much simpler when you don't have one while loop which has to handle every client, so threading is often prefered. You can also save some CPU time if using blocking methods.

这篇关于Unix TCP服务器和UDP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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