简体   繁体   English

使用BSD查找打开Open Pseudo Terminal的可用端口

[英]Find free port to Open Pseudo Terminal Using BSD

I want to create a Pseudo Terminal using the BSD API. 我想使用BSD API创建一个伪终端。 My understanding is that the Unix 98 API will automatically find a free port with posix_openpt() but with BSD API I need to check/find a free port to connect to. 我的理解是,Unix 98 API将使用posix_openpt()自动找到一个空闲端口,但是使用BSD API,我需要检查/查找一个要连接的空闲端口。 Is this correct? 这个对吗?

So I need to do something like this...? 所以我需要做这样的事情...?

int fd, index = 0;
char serial_port[11]; // = "/dev/ptyp0"

while (true) {
    sprintf(serial_port, "/dev/ptyp%d", index); 
    fd = open(serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK);

    if (fd > 0) 
        break;

    index++;
}

Do you know of a Pseudo Terminal tutorial that uses BSD? 您知道使用BSD的伪终端教程吗? There are many using Unix 98 API but not many if any BSD API ones. 使用Unix 98 API的有很多,但使用BSD API的则很少。

Using the actual device path is problematic because the pathnames can differ from system to system. 使用实际的设备路径是有问题的,因为每个系统的路径名可能不同。 Most portable applications which use pseudo terminals long ago were changed to use one of the library functions written to abstract away from the device path. 很久以前使用伪终端的大多数便携式应用程序已更改为使用所编写的库函数之一,以从设备路径中抽象出来。

There is more than one flavor of BSD to consider, eg, FreeBSD, NetBSD, OpenBSD (and others of course, including OSX). 要考虑的BSD有不止一种,例如FreeBSD,NetBSD,OpenBSD(当然还有其他一些,包括OSX)。 xterm's configure script identifies features from each. xterm的configure脚本可识别每个功能。 All have openpty in the "util" library, providing its prototype in util.h 所有人都在“ util”库中具有openpty,并在util.h中提供其原型。

POSIX provides posix_openpt which combines the functionality of openpty with others to provide a simpler interface. POSIX提供posix_openpt,它将openpty的功能与其他功能结合在一起,以提供一个更简单的界面。 However (though a documented interface), it is known to not work properly with OSX (see for example OSX 10.7.5 UTF-8 encoding over ssh as well as xterm's changelog ). 但是(尽管有文档说明的接口),但它不能与OSX一起正常工作(例如,参见ssh上的OSX 10.7.5 UTF-8编码以及xterm的changelog )。

Here are manpage links: 以下是联机帮助页链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM