const char* url = "www.21ic.com"
struct hostent *host;
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = gethostbyname(url);
/* 初始化预连接的服务端地址 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* 连接到服务端 */
connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|