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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-08-2005, 09:54 AM
|
#1
|
LQ Newbie
Registered: Mar 2005
Location: Plymouth UK
Posts: 6
Rep:
|
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!!!
|
|
|
03-08-2005, 10:50 AM
|
#2
|
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783
Rep:
|
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 10:57 AM.
|
|
|
03-08-2005, 03:12 PM
|
#3
|
Senior Member
Registered: Jun 2004
Posts: 2,553
Rep:
|
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) {
}
|
|
|
03-12-2005, 07:22 PM
|
#4
|
LQ Newbie
Registered: Mar 2005
Location: Plymouth UK
Posts: 6
Original Poster
Rep:
|
Thanx
Hey thanx for the help, i'll give that a go in the morning to c how it turns out!
|
|
|
03-12-2005, 10:50 PM
|
#5
|
Senior Member
Registered: Jun 2004
Posts: 2,553
Rep:
|
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 --
|
|
|
03-14-2005, 08:50 AM
|
#6
|
LQ Newbie
Registered: Mar 2005
Location: Plymouth UK
Posts: 6
Original Poster
Rep:
|
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 08:56 AM.
|
|
|
All times are GMT -5. The time now is 03:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|