From: "John T. Williams" <jowillia@vt.edu>
To: jtwilliams@vt.edu
Cc: John T Williams <jtwilliams@contextweb.com>,
linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: Re: connecting to a hoast
Date: Mon, 13 Sep 2004 03:35:40 -0500 [thread overview]
Message-ID: <1095064539.2866.12.camel@client001> (raw)
In-Reply-To: <1095059805.2813.0.camel@client001>
small correction to the code + this actually fetches a webpage
------------------------
#include <sys/types.h>
#include <netdb.h>
int main() {
char* host = "www.vt.edu";
int port = 80;
struct sockaddr_in passive;
struct protoent* proto;
struct hostent hostent;
char buff[512];
int skt;
int sz;
proto = getprotobyname("tcp");
skt = socket(PF_INET, SOCK_STREAM, proto->p_proto );
hostent = *gethostbyname(host);
passive.sin_family = AF_INET;
memcpy(&passive.sin_addr, hostent.h_addr, hostent.h_length);
passive.sin_port = htons(port);
connect(skt, (struct sockaddr*) &passive, sizeof(passive));
sz = sprintf(buff, "GET / HTTP/1.1\r\nHOST:www.vt.edu\r\n\r\n");
write(skt, buff, sz);
while( (sz = read(skt,buff, 512) ) > 0 ) {
printf(buff);
}
printf("\n");
close(skt);
return 0;
}
On Mon, 2004-09-13 at 02:16, John T. Williams wrote:
> Ok
>
> -------- Start Code ---------------------------------
> #include <sys/socket.h> // for connection api
> #include <sys/types.h> // for predefined values
> #include <netdb.h> // for hostname resolution api
>
> char* host = "ftp.domain.com¡;
> int port = 25;
> struct sockaddr_in passive;
> struct protoent * proto;
> int skt;
>
> proto = getprotobyname("tcp")
> // gets the prototype number for tcp/ip
>
> skt = socket(PF_INET, SOCK_STREAM, proto->p_proto );
> //creates a socket
>
> passive.sin_family = AF_INET;
> // IPv4 connection type
> passive.sin_addr = gethostbye(host);
> // resolves the hostname and returns the correct in_addr
> passive.sin_port = htons(port);
> // htons changes the bit order so that it is in network format
>
> connect(skt, &passive, sizeof(passive));
> // connect to host
>
> close(skt);
> // close connection
> -------- End Code -----------------------------------
> this code does not check for errors and will probably crash if anything
> goes wrong. How ever it does show how to connect to a server.
>
> You should read:
> http://www.gnu.org/software/libc/manual/html_node/Connections.html#Connections
>
> - John
>
>
> On Sun, 2004-09-12 at 16:04, Ameer Armaly wrote:
> > Hi all.
> > I was wondering, where is a step by step instructions to connect to a
> > hoast?
> > The documentation seems very cryptic, and doesn't give directions.
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2004-09-13 8:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-12 21:04 connecting to a hoast Ameer Armaly
2004-09-13 7:16 ` John T. Williams
2004-09-13 8:35 ` John T. Williams [this message]
2004-09-13 8:03 ` Ron Michael Khu
-- strict thread matches above, loose matches on Subject: below --
2004-09-13 16:09 Huber, George K RDECOM CERDEC STCD SRI
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1095064539.2866.12.camel@client001 \
--to=jowillia@vt.edu \
--cc=jtwilliams@contextweb.com \
--cc=jtwilliams@vt.edu \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).