Title: | Microsoft Visual C++ bug reports and kits |
Notice: | Register in Topic 2. 5.Last for latest Kit |
Moderator: | DECWET::THOMAS N |
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 |
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.R | Title | User | Personal Name | Date | Lines |
---|---|---|---|---|---|
503.1 | BIGUN::nessus.cao.dec.com::Mayne | A wretched hive of scum and villainy | Thu Apr 24 1997 22:29 | 9 | |
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.2 | thanks, I fixed it | VAOU09::LAM | Fri Apr 25 1997 14:48 | 4 | |
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. |