在C / C ++程序中telnet [英] telnet within a C/C++ program

查看:104
本文介绍了在C / C ++程序中telnet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C / C ++程序(使用C ++编译器的C程序)中编写一些代码来使用telnet。我想要做的是使用telnet检查给定的电子邮件地址是否确实存在。我知道执行它的telnet命令,但无法弄清楚如何在程序中执行此操作。基本问题是我可以建立telnet连接,但是它位于程序本身外部等待命令的窗口中。如何让程序发出命令并检索答案?



如果有更好的替代方法来检查电子邮件地址的存在,我并不是特别的锁定到telnet。



谢谢

I need to write a little bit of code within a C/C++ program (C program with a C++ compiler) to use telnet. What I want to do is use telnet to check to see if a given email address actually exists. I know the telnet commands to do it, but can't figure out how to do it from within the program. The basic problem is that I can make the telnet connection, but then it's in a window external to the program itself awaiting commands. How do I have the program issue the commands and retrieve the answers?

And if there's a better alternative to checking the existence of an email address, I'm not particularly locked into telnet.

Thanks

推荐答案

我对邮件协议一无所知但我认为如果您知道如何使用Telnet连接到邮件服务器,并告诉它您想要做什么。



有两种方法可以解决这个问题。第一个是从您的应用程序启动telnet,从telnet客户端重定向 stdin stdout 到您自己的应用程序中的文件句柄。然后,您可以将命令写入输入描述符并从输出描述符中读取它们。这通常是你在shell脚本而不是C程序中所做的事情,并且可能比第二种方式更难:将程序直接连接到邮件服务器。



要直接连接到邮件服务器,您需要使用套接字API。要做到这一点你需要知道:



- 邮件服务器的IP地址或名称

- 它提供服务的端口



你所做的就是创建一个叫套接字的东西。它有点像Windows上的文件句柄(在UNIXy系统上它是文件句柄)。当你完成后,你可以发送命令到服务器并阅读任何回复。



它可以很简单:



- 创建一个套接字(使用 socket()函数)

- 初始化服务器的sockadr结构/你想与之交谈的港口(使用 gethostbyname()和一些小事)

- 连接到你想要交谈的电脑和端口(使用connect()函数)

- 对于你要发送的每个命令

- 将命令写入套接字(使用 send() function)

- 读取你可以回复的所有数据(使用 recv()函数)

- 当你完成清理一切(使用 close()功能)



可以在整个网络上找到这些功能的详细信息。我建议您检查编译器和操作系统的文档,因为Windows与UNIXy平台有一些不同(它还有几个步骤要做)。最有趣的是名字解析guff,但如果你能克服其余的相当简单。



干杯,



Ash



编辑:有一个非常可怕的示例此处。如果你使用套接字API参考它,它应该全部到位。或者不: - )
I don't know anything about mail protocols but I assume you do as you know how to connect to the mail server with Telnet and tell it what you want to do.

There's two ways you could go about this. The first one is launch telnet from your app redirecting stdin and stdout from the telnet client to file handles in your own app. You can then write commands to the input descriptor and read them from the output descriptor. This is usually the sort of thing you do in a shell script rather than a C program and is probably harder than the second way: connecting your program directly to the mail server.

To connect directly to the mail server you need to use the sockets API. To do that you need to know:

- The IP address or name of your mail server
- The port it provides the service on

What you do is create something called a socket. It's a bit like a file handle on windows (and on UNIXy systems it IS a file handle). When you've done that you can send commands to the server and read any responses back.

it can be a simple as:

- create a socket (using the socket() function)
- initialise a sockadr structure for the server/port you want to talk to (using gethostbyname() and some fiddling)
- connect to the computer and port you want to talk to (using the connect() function)
- for each command you want to send
- write the command to the socket (using the send() function)
- read all the data you can for the reply (using the recv() function)
- when you're done clean up everything (using the close() function)

The details of these functions can be found all over the web. I'd suggest you check the docs for your compiler and OS as Windows has some differences from UNIXy platforms (it has another couple of steps to do). The fiddliest bit is the name resolution guff, but if you can get over that the rest is fairly simple.

Cheers,

Ash

There's a pretty fearsome looking example here. If you go over it with a sockets API reference it should all fall into place. Or not :-)


您可以根据 RFC 854 c>客户端自行实现 telnet 客户端(和 RFC 1143 ,参见简单的Telnet客户端 [ ^ ])。

您还可以查看一些可用的实现,例如: telnet.c at Koders.com [ ^ ]。
You may implement yourself the telnet client, based on the RFC 854 (and RFC 1143, see "Simple Telnet Client"[^]).
You may also have a look at some available implementation, like, for instance: telnet.c at Koders.com[^].


请参阅以下命令启动Telnet服务器/客户端功能和服务。



dism / online / enable-feature / featurename:TelnetServer

dism / online / enable-feature / featurename:TelnetClient

net start telnet
See the commands below to start Telnet server/client feature and service.

dism /online /enable-feature /featurename:TelnetServer
dism /online /enable-feature /featurename:TelnetClient
net start telnet


这篇关于在C / C ++程序中telnet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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