/* 要点:sockaddr_in
* 功能:连接server
* 成功:返回新生成的套接字文件描述符
* 失败:返回-1
*/
int std_connect_socket(char *ip, char *port)
{
int sockfd = 0, portnum = 0;
struct sockaddr_in serv_addr;
portnum = atoi ((const s8*)port);
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
DEBUG_PRINT("socket failed!(%s)\n", strerror(error));
return ERROR_FAILED;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr ((const s8*)ip);
serv_addr.sin_port = htons (portnum);
if(connect(sockfd, (void *)&serv_addr, sizeof(serv_addr)) < 0)
{
DEBUG_PRINT("connect failed! %s\n", strerror(error));
close(sockfd);
return ERROR_FAILED;
}
return sockfd;
}
sockaddr_in实例--连接server
最新推荐文章于 2022-04-12 14:50:45 发布