"Network Programming" uses Getnameinfo ()/getaddrinfo ()/inetpton ()

Source: Internet
Author: User
Tags deprecated goto

1. Brief

The previously used network programming function has now made a certain change, reported so 3 errors.

error C4996: ‘inet_ntoa‘: Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warningserror C4996: ‘gethostbyaddr‘: Use getnameinfo() or GetNameInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warningserror C4996: ‘inet_ntoa‘: Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
2. Workaround 1

Right-click Properties-Property Page Select the SDL check box, general, C + +, select No.

3. Code resolution

Now that you have made it clear that you need to switch to a more secure function, just follow the requirements.

serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//自己的IP或者127.0.0.1

Switch

//新改进代码InetPton(AF_INET, _T("127.0.0.1"), &serverAddr.sin_addr.s_addr);

GETHOSTBYADDR () switch to Getnameinfo ();

Inet_ntop () switch to getaddrinfo ();

The Modified code:

#include "stdafx.h" #include <WinSock2.h> #pragma comment (lib, "Ws2_32.lib") #include <ws2tcpip.h>/*1. Initializes the environment wsastartup2. Create socket Socket3. Binds socket Bind4. Listen for socket listen5. Handles socket connection ACCEPT6. Send receive Data send/recv7. Close socket closesocket8.    Clean up the Environment Wsacleanup*/int main () {//1. Initialize the environment WSAStartup wsadata WSD = {0};    int re = WSAStartup (Makeword (2, 2), &AMP;WSD);    if (Re! = 0) {return 0;}        if (Lobyte (wsd.wversion)! = 2 | |    Hibyte (wsd.wversion)! = 2) {return 0; }//2.    Create socket sockets Socket Listensock = socket (af_inet, sock_stream, ipproto_tcp);    if (Listensock = = invalid_socket) {return 0; }//3.    Bind socket bind sockaddr_in serveraddr;    serveraddr.sin_family = af_inet;        Serveraddr.sin_port = htons (0x5566); Old code//SERVERADDR.SIN_ADDR. S_un. S_ADDR = inet_addr ("127.0.0.1");//own IP or 127.0.0.1//New Improved Code Inetpton (Af_inet, _t ("127.0.0.1"), &serveraddr.sin_ad Dr.s_addr);    Re = Bind (Listensock, (SOCKADDR *) &serveraddr, sizeof (SERVERADDR)); 4.    Listener Socket Listen RE = Listen (Listensock, somaxconn); 5.    Handle socket connection Accept sockaddr_in clientaddr;    int size = sizeof (CLIENTADDR);    SOCKET Stoclientsock = Accept (Listensock, (SOCKADDR *) &clientaddr, &size);    Test get results//replace for new function view using result//old hostname hostent * phost = gethostbyaddr ((char *) &AMP;SERVERADDR.SIN_ADDR, 4, af_inet);        Old IP Address char * p = inet_ntoa (* (struct in_addr *) (*phost->h_addr_list));    New code Test///////////////////////////////////////////////////////////////////struct Addrinfo *ptr = NULL;    struct Addrinfo hints;    struct Addrinfo *result = NULL;    struct sockaddr_in *sockaddr_ipv4; Char Hostname[ni_maxhost]; Host name Char Servinfo[ni_maxserv]; Specific connection succeeded Port Getnameinfo (struct sockaddr *) &serveraddr, sizeof (struct sockaddr), hostname, NI    _maxhost, Servinfo, Ni_maxserv, Ni_numericserv); ZeromemorY (&hints, sizeof (hints));    hints.ai_family = Af_unspec;    Hints.ai_socktype = Sock_stream;    Hints.ai_protocol = ipproto_tcp;    GETADDRINFO (hostname, servinfo, &hints, &result);            for (ptr = result; ptr! = NULL; ptr = ptr->ai_next) {switch (ptr->ai_family) {case AF_UNSPEC:            printf ("unspecified\n");        Break            Case af_inet:printf ("af_inet (IPv4) \ n");            Sockaddr_ipv4 = (struct sockaddr_in *) ptr->ai_addr;            printf ("\tipv4 address%s\n", Inet_ntoa (SOCKADDR_IPV4-&GT;SIN_ADDR));        Break }}/////////////////////////////////////////////////////////////////////6.1 sends data send char buf[100] = "    Hello socket client,from server! ";    Re = Send (Stoclientsock, buf, strlen (BUF), 0);    if (re = = Socket_error) {goto over;    }//6.2 receive data recv re = recv (Stoclientsock, buf, sizeof (BUF), 0); if (re = = invalid_socket) {goto OVer }over://7.    Close socket closesocket closesocket (listensock);    Closesocket (Stoclientsock); 8.    Clean up the Environment WSACleanup WSACleanup (); return 0;}

Network programming uses Getnameinfo ()/getaddrinfo ()/inetpton ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.