LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2005, 08:54 AM   #1
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Rep: Reputation: 0
Using the gethostbyname command


Hi, i have been trying to create a program that will connect to a server using its DNS name and have found out that i need to use the gethostbyname function. I also require it to read off a txt file that is stored there without the http headers and have not got a clue how to do it. When i compile my code using -gcc -Wall -ansi -pedantic -c client.c i get a load of errors. Would anyone be able to set me on the right path to makeing this work? the code that i am using is an example taken from a clint server program that i have tried to change.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


/* Explicit declaration of an imported function. We could use a .h
for this. */
void readloop (int our_socket);

int main
char address*
{
int our_socket;
struct sockaddr_in our_address;
struct hostent* host_info;

/*Resolve the host*/

host_info = gethostbyname("host address");

if h_name == NULL
printf("/nError Connecting To Host");
else
{
printf("/nConnection Made");
}




printf("Client starting...\n");
if ((our_socket = socket(PF_INET, SOCK_STREAM, 0)) > 0)
printf("The socket has been created\n");
else
{
perror("Could not create socket");
exit(1);
}
our_address.sin_family = AF_INET;
if (argc < 2)
{
printf("Need one argument: the numeric IP address of the server!\n");
exit(3);
}
printf("Hopefully, %s is the numeric IP address of the server...\n",
argv[1]);
/* No error checking on the next line -- hence the previous message! */
inet_pton(AF_INET, argv[1], &our_address.sin_addr);
our_address.sin_port = htons(23456);
if (connect(our_socket,
(struct sockaddr *) & our_address,
sizeof(our_address)) == 0)
printf("Connected to host %s\n",
inet_ntoa(our_address.sin_addr));
else
{
perror("Could not connect");
exit(2);
}
/* Insert read text file here */



if (close(our_socket) != 0)
herror("Warning: problem closing our_socket");
printf("Exiting.\n");
exit(EXIT_SUCCESS);
}

Thanx again if any one can help!!!
 
Old 03-08-2005, 09:50 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
You have many syntax mistakes like not closing }, forgetting to put () in functions. Those are really easy to correct and you should do this by yourself, since you are a C newbie.

A newbie C network programming guide is here: http://www.ecst.csuchico.edu/~beej/guide/net/html/
It contains an example of a simple server client model. Also use spaces and tabs when nesting something .It will be a lot easier to correct syntax mistakes. it's easier to see if a branket is missing, when writing like this:
Code:
if(.....)
{
    if(...)
    {
         .................
     }
     .....................
}
than when writing like this:
if(.....)
{
if(...)
{
.................
}
.....................
}
Use the code tag when u post code, because otherwise the spaces are ignored (default HTML behaviour).

Last edited by perfect_circle; 03-08-2005 at 09:57 AM.
 
Old 03-08-2005, 02:12 PM   #3
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
since this is a daunting task for a beginner i will try to get you started
this will take the host name as an argument from the command line
i will not do any error checking so you can add that in
Code:
int main (int argc, char* const argv[]) {
   int our_socket;
   struct sockaddr_in our_address;
   struct hostent* host_info;
   our_socket = socket(PF_INET, SOCK_STREAM, 0);
   our_address.sin_family = AF_INET;
   host_info = gethostbyname(argv[1]);
   our_address.sin_addr = *((struct in_addr *) host_info->h_addr);
   // web servers generally listen to port 80
   our_address.sin_port = htons(80);
   connect(our_socket,(struct sockaddr *) &our_address, sizeof(our_address));
   // here call a function to get your page
   get_page (our_socket);
    return 0;
}

void get_page (int our_socket) {
    
}
 
Old 03-12-2005, 06:22 PM   #4
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Original Poster
Rep: Reputation: 0
Exclamation Thanx

Hey thanx for the help, i'll give that a go in the morning to c how it turns out!
 
Old 03-12-2005, 09:50 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
c_ool,

try to figure out how to get the page on you own
later if you can't, write back and someone can show

one note is that some web servers nowdays requirwe browser verification/authentication or whatever it is before they will serve up stuff --
 
Old 03-14-2005, 07:50 AM   #6
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Original Poster
Rep: Reputation: 0
hey, foo_bar_foo

thanx for the connecting code, i managed to get it to connect to a host defined in the the code now and have it reading a txt file from the webserver. I'll work on the validation now!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main (int argc, char* const argv[]) {
char buffer[1000];
int our_socket;
struct sockaddr_in our_address;
struct hostent* host_info;
our_socket = socket(PF_INET, SOCK_STREAM, 0);
our_address.sin_family = AF_INET;
host_info = gethostbyname("server_address");
our_address.sin_addr = *((struct in_addr *) host_info->h_addr);
// web servers generally listen to port 80
our_address.sin_port = htons(80);
connect(our_socket,(struct sockaddr *) &our_address, sizeof(our_address));
// here call a function to get your page
write(our_socket, "GET /~pjb/tmp/cnet221file.txt\n", 30);
recv(our_socket, buffer, 1000, 0);
printf("%s", buffer);
return 0;
}
Thanx again

Last edited by mdlister; 03-14-2005 at 07:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gethostbyname in script? farry Linux - Newbie 2 03-14-2005 02:14 AM
gethostbyname() error jnusa Programming 0 01-12-2005 04:56 AM
Problem with # and gethostbyname lolilol Linux - Software 0 04-26-2004 05:29 AM
Problem w/ gethostbyname() robeb Linux - Networking 3 05-22-2002 08:42 AM
gethostbyname error for aimstr8 Linux - Security 7 03-09-2002 04:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration