linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* connecting to a hoast
@ 2004-09-12 21:04 Ameer Armaly
  2004-09-13  7:16 ` John T. Williams
  2004-09-13  8:03 ` Ron Michael Khu
  0 siblings, 2 replies; 5+ messages in thread
From: Ameer Armaly @ 2004-09-12 21:04 UTC (permalink / raw)
  To: linux-c-programming

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: connecting to a hoast
  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
  2004-09-13  8:03 ` Ron Michael Khu
  1 sibling, 1 reply; 5+ messages in thread
From: John T. Williams @ 2004-09-13  7:16 UTC (permalink / raw)
  To: John T Williams; +Cc: linux-c-programming

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: connecting to a hoast
  2004-09-12 21:04 connecting to a hoast Ameer Armaly
  2004-09-13  7:16 ` John T. Williams
@ 2004-09-13  8:03 ` Ron Michael Khu
  1 sibling, 0 replies; 5+ messages in thread
From: Ron Michael Khu @ 2004-09-13  8:03 UTC (permalink / raw)
  To: Ameer Armaly; +Cc: linux-c-programming

you can try reading the "beej guide" at 
www.ecst.csuchico.edu/~beej/guide/net
it covers some of the basics of C sockets programming
=)

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
>
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: connecting to a hoast
  2004-09-13  7:16 ` John T. Williams
@ 2004-09-13  8:35   ` John T. Williams
  0 siblings, 0 replies; 5+ messages in thread
From: John T. Williams @ 2004-09-13  8:35 UTC (permalink / raw)
  To: jtwilliams; +Cc: John T Williams, linux-c-programming

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: connecting to a hoast
@ 2004-09-13 16:09 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-13 16:09 UTC (permalink / raw)
  To: linux-c-programming


>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.

There are several online tutorial for network programming (beej for
example),
however if you intend to do a lot of network programming, I would recommend
reading one or more of the following books:

1. Unix Network Programming vol 1 and 2 by W. Richard Stevens

   N.B. - volume one was recently updated by Bill Fenner and Andrew M Rudoff

          after Stevens death.

   volumn 1 covers Sockets and XTI programming.  Lots of examples and
details 
   volume 2 covers interprocess communications

2. Internetworking with TCP/IP vol 1 -3 by Douglas Comer and David Stevens

   volume 1 covers the protocols and architecture
   volume 2 covers the implementation of the network stack and protocols
   volume 3 covers client-server programming.  There are seperate volumes 
            for most of the popular operating systems.

3. TCP/IP Illustrated, vol 1 - 3 by W. Richard Stevens
   volume 1 is a detailed coverage of the protocols and how they work
   volume 2 is a line by line code walkthrough for the TCP stack of 4.4BSD
   volume 3 covers transaction based communications and UNIX domain
protocols

3. Linux Socket Programming by Sean Walton

4. Effective TCP/IP Programming: 44 Tips to Improve Your Programming,
   Jon C. Snader

   Lots of useful tips to improve you code.

George


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-13 16:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).