By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Crash with hostent data retrieve

P: n/a
I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct.

Any help here? I get a clean compile...

Here's the code

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
char * hostname;

int socket_desc;

socket_desc=socket(AF_INET,SOCK_STREAM,0);
if (socket_desc==-1)
perror("Create socket");

struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_about:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, hostname, -1, &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Devon Reed" <de***@jiffysquid.com> wrote in message
news:27*************************@posting.google.co m...
I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct.

Any help here? I get a clean compile...

Here's the code
struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;


Taking a wild guess I would say that he is a null pointer.

If that doesn't solve it then take this question to a Windows programming
group like news:comp.os.ms-windows.programmer.win32. This group is for
standard C++ only, and there very little standard C++ in your code.

john
Jul 22 '05 #2

P: n/a
In article <27*************************@posting.google.com> , Devon Reed wrote:
I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct. [-] struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;

[-]
How about ...
if (he != NULL) {
hostname = ...
} else {
shite();
}
.... or something similar?
[-]
Ta',
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.