Article View: borland.public.cppbuilder.nativeapi
Article #9683Re: Imitating Telnet
From: Remy Lebeau (TeamB)
Date: Wed, 16 Jun 2004 16:56
Date: Wed, 16 Jun 2004 16:56
87 lines
3910 bytes
3910 bytes
"Feico" <feicorem0veth1s@gmx.net> wrote in message
news:a7k0d0lm7htcsem1ve3179ld2bcpsj8hbs@4ax.com...
> inaddr.s_addr = ::inet_addr(domain);
> if (inaddr.s_addr == INADDR_NONE)
> {
> host = ::gethostbyname(domain);
> }
> else
> {
> host = ::gethostbyaddr((const char *)&inaddr, sizeof(inaddr),
AF_INET);
> }
You don't need gethostbyaddr(). You already have the IP address converted
to a u_long via inet_addr(), so there is no need to convert it a second
time.
> sockaddr_in sa;
You need to zero out the sin_zero member. You are not doing that. In fact,
you should always zero out Win32 API structures before then filling them in
with the desired values. The easiest way to do that is with the following
shorthand:
sockaddr_in sa = {0};
> sa.sin_port = (unsigned short) /*sp->s_*/port;
Why not have your function accept an unsigned short to begin with? Then you
don't need the cast.
> if (::connect(socket, (sockaddr *)&sa, sizeof(sa)) < 0)
> {
> ReportError("connection to host failed"); //THIS is executed
> }
What is the actual return value of WSAGetLastError()?
I suggest you use the following code instead:
SOCKET Opensocket(char *domain, unsigned short port)
{
unsigned long addr = ::inet_addr(domain);
if( addr == INADDR_NONE )
{
hostent *host = ::gethostbyname(domain);
if( host != NULL )
addr = *(unsigned long*)(host->h_addr);
else
ReportError("error");
}
SOCKET socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if( socket == INVALID_SOCKET )
ReportError ("socket invalid");
sockaddr_in sa = {0};
sa.sin_family = AF_INET;
sa.sin_port = port;
sa.sin_addr.s_addr = addr;
if( ::connect(socket, (sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR )
ReportError("connection to host failed");
return socket;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
WSADATA WSAData = {0};
if( WSAStartup(MAKEWORD(2, 0), &WSAData) == 0 )
{
SOCKET socket = Opensocket("mail.home.nl", 110);
if( socket != INVALID_SOCKET )
{
// use socket as needed...
::shutdown(socket, SD_SEND);
::closesocket(socket);
}
WSACleanup();
}
}
Gambit
Technical Details
Message-ID:
<40d07cbe$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!nntp5.savvis.net!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<mk4kc0lf2ku8qbgk2n98eav00l9b0552u5@4ax.com> <40ca1c3f$1@newsgroups.borland.com> <ca9kc0pg43rmc29o9t346a4atb3ko8s7n8@4ax.com> <40ca2dc2$1@newsgroups.borland.com> <njuqc0p935q7en7t7hhqekd8lsllierpmp@4ax.com> <t5rrc0llo0s8nkh70nehnltbcf3aainb6r@4ax.com> <2c4tc09vc8jgm32ahuufv9lt8a4ld4j960@4ax.com> <50auc0ljtkbensa9erhvf8hr4rbkjfj5ka@4ax.com> <a7k0d0lm7htcsem1ve3179ld2bcpsj8hbs@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> <40ca1c3f$1@newsgroups.borland.com> <ca9kc0pg43rmc29o9t346a4atb3ko8s7n8@4ax.com> <40ca2dc2$1@newsgroups.borland.com> <njuqc0p935q7en7t7hhqekd8lsllierpmp@4ax.com> <t5rrc0llo0s8nkh70nehnltbcf3aainb6r@4ax.com> <2c4tc09vc8jgm32ahuufv9lt8a4ld4j960@4ax.com> <50auc0ljtkbensa9erhvf8hr4rbkjfj5ka@4ax.com> <a7k0d0lm7htcsem1ve3179ld2bcpsj8hbs@4ax.com> Subject: Re: Imitating Telnet Date: Wed, 16 Jun 2004 09:56:36 -0700 Lines: 89 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: <40d07cbe$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1087405246 68.169.168.124 (16 Jun 2004 10:00:46 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!nntp5.savvis.net!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail