在C UDP聊天服务器 [英] udp chat server in c
问题描述
我试图写在C语言中的UDP的聊天服务器。我以前写的TCP聊天服务器,这是确定的。
i am trying to write udp chat server in c language. i have written tcp chat server before and it is ok.
在TCP服务器,接受函数返回一个FD号码,服务器会根据这个数字的fd与特定客户端通信。
in tcp server, accept function returns an fd number and server communicate with a specific client according to this fd number.
但在UDP的,我怎么能听一个特定的客户端?
but in udp, how can i listen a specific client?
在我的TCP服务器,客户端后,连接到服务器,创建了一个线程,它听此客户端。因此,对于每个客户端,则存在听根据是从接受函数返回的fd数目的线程。所以任何消息可以根据这个数字FD对特定客户端发送。
In my tcp server, after a client connect to server, a thread is created and it listen this client. So for each client, there is a thread that listen to according to fd number that is returned from accept function. so any message can send according to this fd number to specific client.
如何在UDP服务器实现这一目标?
How can i achieve this in udp server?
感谢您的答案。
推荐答案
您使用 recvfrom的
来找出源IP /端口,然后用 SENDTO
。你只需要绑定
来选择一个服务器端口。你不接受
。所有连接
确实为UDP设置默认目标(你打算使用覆盖SENDTO
)。
You use recvfrom
to find out the source IP/port and then reply with sendto
. You only need to bind
to select a server port. You don't accept
. All connect
does for UDP is set the default destination (which you plan to override with sendto
).
这篇关于在C UDP聊天服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!