当前位置: 技术问答>linux和unix
【求助】套接字链接linux服务,显示connection refused
来源: 互联网 发布时间:2016-12-21
本文导语: 本帖最后由 Redshadows 于 2011-08-13 15:22:11 编辑 #include #include #include #include #include #include int main() { int sfd, err, n; struct addrinfo *alist, *aptr; struct addrinfo hint; char buf[128]; hint.ai_flags = AI_CANONNAME; hint.ai_fami...
#include
#include
#include
#include
#include
#include
int
main()
{
int sfd, err, n;
struct addrinfo *alist, *aptr;
struct addrinfo hint;
char buf[128];
hint.ai_flags = AI_CANONNAME;
hint.ai_family = 0;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
if ((err = getaddrinfo("localhost", "daytime", &hint, &alist)) != 0) {
printf("getaddrinfo error & exitn");
printf("%sn", gai_strerror(err));
exit(1);
}
printf("getaddrinfo succeedn");
if (alist == NULL) {
printf("alist = NULLn");
exit(1);
}
for (aptr = alist; aptr != NULL; aptr = aptr->ai_next) {
if ((sfd = socket(aptr->ai_family, SOCK_STREAM, 0)) ai_addr, aptr->ai_addrlen) h_addr_list;
len = sizeof(address);
printf("socket succeedn");
if (connect(sfd, (struct sockaddr *)&address, len)