Article View: borland.public.cppbuilder.nativeapi
Article #9875Re: Imitating Telnet
From: Remy Lebeau (TeamB)
Date: Fri, 11 Jun 2004 20:51
Date: Fri, 11 Jun 2004 20:51
83 lines
3423 bytes
3423 bytes
"Feico" <feicorem0veth1s@gmx.net> wrote in message
news:mk4kc0lf2ku8qbgk2n98eav00l9b0552u5@4ax.com...
> I'm trying to write a program that can perform functions like Telnet.
> That is, it must open a connection to a server (POP3 or SMTP in
> my case) and exchange commands.
You do not need Telnet for that.
> My biggest success was #include <winsock2.h>. This produced a host of
interesting functions.
If I were you, I would just use a pre-made TCP component instead of trying
to do everything manually. Either Borland-provided or third-party, it
doesn't really matter.
> I tried them in opposite order, since connect appeared not to work
> without a preceding socket, and socket would not work without a
> preceding WSAStartup, etc.
That is correct.
> Still some parameters are enigmatic to me. For example, the first
> parameter of socket is an Address family specification, but nobody
> knows what an Address family specification is.
Use AF_INET.
> But let's be brief. Telnet performs a command like:
> OPEN [domain name] [port]
> for example:
> OPEN pop.smurf.com 110
>
> How is this done in a C program?
Use connect(), for example:
#include <winsock2.h>
char *host = "pop.smurf.com";
WSADATA WSAData = {0};
if( WSAStartup(MAKEWORD(2, 0), &WSAData) == 0 )
{
unsigned long HostAddr = inet_addr(host);
if( HostAddr == INADDR_NONE )
{
hostent *pHost = gethostbyname(host);
if( pHost )
HostAddr = inet_addr(pHost->h_addr);
}
if( HostAddr != INADDR_NONE )
{
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if( sock )
{
unsigned long enabled = 0;
if( ioctlsocket(sock, FIONBIO, &enabled) == 0 )
{
sockaddr_in addr = {0};
addr.sin_addr.s_addr = HostAddr;
addr.sin_port = 110;
if( connect(sock, (sockaddr*)&addr, sizeof(addr)) == 0 )
{
// read/write the socket as needed...
shutdown(sock, SD_SEND);
}
}
closesocket(sock);
}
}
WSACleanup();
}
> It cannot be that complicated.
You are making it more complicated for yourself by trying to do everything
manually without understanding what you are doing to begin with.
Gambit
Technical Details
Message-ID:
<40ca1c3f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<mk4kc0lf2ku8qbgk2n98eav00l9b0552u5@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,6ff98bec32bcdef X-Google-Attributes: gid10cc67,public From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <mk4kc0lf2ku8qbgk2n98eav00l9b0552u5@4ax.com> Subject: Re: Imitating Telnet Date: Fri, 11 Jun 2004 13:51:03 -0700 Lines: 85 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.169.168.124 Message-ID: <40ca1c3f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1086987327 68.169.168.124 (11 Jun 2004 13:55:27 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail