在C服务器接受UDP和TCP连接 [英] Server in C accepting UDP and TCP connections

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

问题描述

我有一个问题。我一直在开发一个在C服务器,可以接受UDP和TCP客户端的连接。所以我需要知道当一个连接的UDP,当它是一个TCP。
我已经开发了两种情况下分开,而他们的工作正常。

I have a problem. I've been developing a Server in C that can accept UDP and TCP clients connections. So i need to know when a connection is UDP and when its a TCP. I've already developed both cases separately, and they work ok.

我一直在寻找如何做到这一点,一切指向的选择()的功能,但我不知道如何使用它这件事。
谁能告诉我一个例子或东西接受UDP和TCP连接......我真的失去了那里。

I've been searching about how to do this, and everything points to the select() function, but i dont know how to use it for this matter. Can anyone show me an example or something accepting udp and tcp connections... I'm really lost there.

非常感谢你...

推荐答案

首先,打开每个服务器端套接字。 BSD套接字需要您单独打开的TCP和UDP。每个插座,因为我相信你已经知道,有一个文件描述符,这是一个小的整数。

First, open each server-side socket. BSD sockets require you to open TCP and UDP separately. Each socket, as I assume you already know, has a file descriptor, which is a small integer.

在你有准备,创建类型 fdset 的变量,它是一组文件描述符。为 fdset 型的小型API包括:

Once you have that ready, create a variable of type fdset, which is a set of file descriptors. The mini-API for the fdset type includes:


  • FD_ZERO(套),这将清除组。

  • FD_SET(插座,设置),它增加了套接字的描述符集合(转位的整数)。

  • FD_CLR(插座,设置),它会删除的插口。

  • FD_ISSET(插座,设置),返回非零如果套接字在集合。

  • FD_ZERO(set), which clears the set.
  • FD_SET(socket,set), which adds the socket's descriptor to the set (turns the bit on the integer).
  • FD_CLR(socket,set), which removes the socket from the set.
  • FD_ISSET(socket,set), which returns non-zero if the socket is in the set.

所以,你要做的是:


  • 创建四个 fdset 变量。一牵你的插座完整列表,测试阅读,写作和异常三层防刮空间。清除他们与 FD_ZERO()

  • FD_SET(),添加套接字的完整列表。

  • 在您的服务器循环,使用 FD_ISSET() FD_SET()复制从套接字的列表完整列表读取列表(和其他人,如果需要的话)。

  • 呼叫选择()。它会看起来像选择(biggest_socket_fd,readSockets,writeSockets,exceptionSockets,超时)

  • 循环遍历可能的套接字号,要求 FD_ISSET(readSockets,I)。希望你保持跟踪哪个插座用的是什么协议,可以路由执行为接受() recvfrom的()

  • Create four fdset variables. One to hold your full list of sockets, and three scratch spaces for testing reading, writing, and exceptions. Clear them out with FD_ZERO().
  • With FD_SET(), add your sockets to the full list.
  • In your server loop, use FD_ISSET() and FD_SET() to copy the list of sockets from the full list to the read list (and others, if you need them).
  • Call select(). It'll look something like select(biggest_socket_fd, readSockets, writeSockets, exceptionSockets, timeout).
  • Loop through possible socket numbers, asking FD_ISSET(readSockets,i). Hopefully, you kept track of which socket is using what protocol and can route execution to either accept() or recvfrom().

请注意,为了避免饥饿,你要确保你的内环跟踪它离开的最后的地方。如果你有一千个插座和你做类似(i = 0; I< 1000;我++){} ,低数量的插座将不得不在优势服务。

Note that, to avoid starvation, you want to make sure your inner loop keeps track of where it left off last. If you have a thousand sockets and you do something like for (i=0;i<1000;i++){}, lower-number sockets are going to have an advantage in service.

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

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