PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Problem with gethostbyname - can't find any!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with gethostbyname - can't find any!

Réponse
 
LinkBack Outils de la discussion
Vieux 07/12/2007, 13h57   #1
jonashn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with gethostbyname - can't find any!

I have the following visual c++ code. It's taken from Beej's guide to
network programming, and modified a little foruse with winsock. But
gethostbyname just doesn't work, not for localhost nor any other ala
http://google.com
CODE:

#include "stdafx.h"
#include <winsock.h>
#include <iostream>
#define BACKLOG 1
#define MYPORT 18999
#define PORT 18999 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once


class WSAInitializer // Winsock Initializer
{
public:
WSAInitializer() {
if (WSAStartup(0x101,&m_wsadata))
{
exit(-1);
}
}
~WSAInitializer() {
WSACleanup();
}
private:
WSADATA m_wsadata;
};



int _tmain(int argc, _TCHAR* argv[])
{
WSAInitializer* WSAINIT=new WSAInitializer;
SOCKET sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information

if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}

if ((he=gethostbyname((char*)argv[1])) == NULL) { // get the host
info
std::cout<<WSAGetLastError();
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof
their_addr) == -1) {
perror("connect");
exit(1);
}

if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}

buf[numbytes] = '\0';

printf("Received: %s",buf);

closesocket(sockfd);

return 0;

}

Do you see any problems with the above code? I'm nearly 100% sure the
error isn't caused by my local machine.

When running it like client.exe localhost it gives the Winsock error
code 11001 aka host not found.

Any suggestions?
  Réponse avec citation
Vieux 07/12/2007, 14h37   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with gethostbyname - can't find any!

jonashn wrote:
> I have the following visual c++ code. It's taken from Beej's guide to
> network programming, and modified a little foruse with winsock. But
> gethostbyname just doesn't work, not for localhost nor any other ala
> http://google.com
> CODE:
>
> #include "stdafx.h"
> #include <winsock.h>
> #include <iostream>
> [..]
>
> Do you see any problems with the above code?


You mean, beyond the fact that it's non-standard C++?

> I'm nearly 100% sure the
> error isn't caused by my local machine.


Then it's caused by something else.

> When running it like client.exe localhost it gives the Winsock error
> code 11001 aka host not found.
>
> Any suggestions?


Post to the newsgroup where Winsock is on topic, like the Windows
programming newsgroup, there are several. The FAQ contains the list
of the proposed ones.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 08/12/2007, 13h20   #3
jonashn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with gethostbyname - can't find any!

On 7 Dec., 15:37, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> jonashn wrote:
> > I have the following visual c++ code. It's taken from Beej's guide to
> > network programming, and modified a little foruse with winsock. But
> > gethostbyname just doesn't work, not for localhost nor any other ala
> >http://google.com
> > CODE:

>
> > #include "stdafx.h"
> > #include <winsock.h>
> > #include <iostream>
> > [..]

>
> > Do you see any problems with the above code?

>
> You mean, beyond the fact that it's non-standard C++?


Yes, and actually a started using standard C++ and WxWidgets, and
would at any time prefer gcc and standard C. I just coudn't get WxW to
work, and since this is part of a school project which has to be
finished fast, i had to switch and ended up by choosing VCE.

> > I'm nearly 100% sure the
> > error isn't caused by my local machine.

>
> Then it's caused by something else.
>
> > When running it like client.exe localhost it gives the Winsock error
> > code 11001 aka host not found.

>
> > Any suggestions?

>
> Post to the newsgroup where Winsock is on topic, like the Windows
> programming newsgroup, there are several. The FAQ contains the list
> of the proposed ones.

I'll do so.
>
> V
> --



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h27.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14011 seconds with 11 queries