Using a domain name for socket connection in IOS

Source: Internet
Author: User

Monkey original, reprinted. Reprinted Please note: Reprinted from cocos2d Development Network --cocos2dev.com, thank you!


Original address: http://www.cocos2dev.com /? P = 289


The socket connection is used in the project. Because of the boost I use, the boost connection can only be used to transmit IP addresses, while the server address is a domain name, so the domain name must be transferred to the IP address.


After reading the code, it is actually quite simple.


//SERVER_ADDRESS  Server Domainhostent* host_entry = gethostbyname(SERVER_ADDRESS);char IPStr[64] = {0};if(host_entry !=0){sprintf(IPStr, "%d.%d.%d.%d",(host_entry->h_addr_list[0][0]&0x00ff),(host_entry->h_addr_list[0][1]&0x00ff),(host_entry->h_addr_list[0][2]&0x00ff),(host_entry->h_addr_list[0][3]&0x00ff));}

In this way, gethostbyname () is the system's own library function. You can resolve the domain name to the struct hostent, and then parse the struct yourself.

Related Article

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.