Obtain the ip address of vc6.0 (1), and obtain the ip address of vc6.0
As a newbie! I learned too many things I was afraid I would forget, so I made a record (mainly focusing on Single Chip Microcomputer); blog articles mainly focused on single chip microcomputer, supplemented by the upper computer
Recently, I was drunk by the protocol of the Internet of Things cloud server. I plan to build a small server for entertainment and write it using vc6.0, which is a pure foundation. I hope it will help you.
Because I bought the a6 GPRS module and only supports tcp, udp will be used later. As for why vc6.0 is used, I use the XP system and have no money.
If there is an error, please point out that you can directly copy and paste the code in your spare time (the code written should be annotated as much as possible) through vc6.0.
(The Code comes from the network,
It is only for learning and communication and is strictly prohibited for commercial purposes.
)
// WSAStartup (). Windows Sockets Asynchronous startup command. This command must be enabled for network programming in Windows. Otherwise, the following error will be reported in subsequent operations: 10093: WSAStartup.
// Gethostname () gets the local name
// Gethostbyname () obtains the local network information, stores it in the struct hostent * pointer, and returns it to the caller.
// Obtain the IP address from the struct hostent structure information and convert it to string output
// Copy and paste the following directly
# Include "winsock2.h" # include <stdio. h> # pragma comment (lib, "ws2_32.lib") int main (int argc, char * argv []) {WSADATA wsaData; char name [155]; char * ip; PHOSTENT hostinfo; if (WSAStartup (MAKEWORD (2, 0), & wsaData) = 0) // WSAStartup (). Windows Sockets Asynchronous startup command {if (gethostname (name, sizeof (name) = 0) // gethostname () obtain the local name {printf ("hostname = % s \ n", name); if (hostinfo = gethostbyname (name ))! = NULL) // gethostbyname () obtains the local network information, stores it in the struct hostent * pointer, and returns it to the caller {ip = inet_ntoa (* (struct in_addr *) * hostinfo-> h_addr_list); // obtain the address string printf ("local host ip: % s \ n", ip) ;}} WSACleanup (); // The function is to terminate the use of Winsock 2 DLL (Ws2_32.dll)} while (1); // an endless loop is added here to prevent the black program box from automatically disabling return 0; // exit directly. If debugging is normal, it will not quit}
The above while (1) can be commented out, the following is the Running Effect