[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference decwet::visual

Title:Microsoft Visual C++ bug reports and kits
Notice:Register in Topic 2. 5.Last for latest Kit
Moderator:DECWET::THOMASN
Created:Tue May 17 1994
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:521
Total number of notes:2938

503.0. "problem with gethostname()" by VAOU09::LAM () Thu Apr 24 1997 18:12

    I am testing out tcp/ip programming on NT, and have built most
    of the winsock sample programs; they all seem to work fine. But I run
    into a strange problem with using gethostname(). Here's a code
    fragment:
    
    #include <winsock2.h>
    #include <stdio.h>
    #include <string.h>
    
    main()
    {
    	char *name;
    	int stat;
    	
    	if ((stat=gethostname(name,20))==0)
    		printf("stat = %d hostname = %s\n",stat,name);
    	else {
    		perror("ERROR : ");
    		printf("stat = %d",stat);		
    	}
    }
    I buid it on my Alpha 433a running NT4-SP2 and VC++5 inside DevStudio.
    The program compiles and links fine, but executing it gethostname() 
    returns -1. My machine is TCP/IP configured, as all of the sample
    winsock programs (from DevStudio and from 3rd party books/CD) work
    just fine. BTW, the same code works on my other Alpha running UNIX
    4.0B. Any help appreciated.
    
    
T.RTitleUserPersonal
Name
DateLines
503.1BIGUN::nessus.cao.dec.com::MayneA wretched hive of scum and villainyThu Apr 24 1997 22:299
If you added a call to WSAGetLastError (), you'd find out that the error was 
WSANOTINITIALISED. I'll leave you to figure the rest out.

Once you've fixed that, you have another problem. You're passing an uninitialsed 
pointer (*name) to gethostname. This will fail, since gethostname expects "name" 
to be pointing to a buffer. I'll leave you to figure that one out as well. (If 
it worked on UNIX, you were lucky.)

PJDM
503.2thanks, I fixed itVAOU09::LAMFri Apr 25 1997 14:484
    thanks for the help, I am new to NT TCP/IP programming and did not
    know about WSAStartup() until I looked closely; it is different from
    UNIX obviously. The char * was my typo, it should have been char xx[20].
    thank you.