我写了服务器和客户端代码客户端可以正常地与服务器连接,但只能通过本地地址连接,我不知道如何通过因特网连接
int main()
{
struct addrinfo host_info;
struct addrinfo * host_list;
struct sockaddr sa;
struct sockaddr_in ip4addr;
memset(&host_info, 0, sizeof host_info);
host_info.ai_family = AF_INET; // IP version not specified. Can be both.
host_info.ai_socktype = SOCK_STREAM;
getaddrinfo(NULL, "5555", &host_info, &host_list);
int s;
s=socket(host_list->ai_family,host_list->ai_socktype,host_list->ai_protocol);
if(s==-1) printf("error\n");
int status = connect(s, host_list->ai_addr, host_list->ai_addrlen);
if (status == -1) printf("connect error\n");
char *message="Hi there!";
send(s, message, 10, 0);
return 0;
}我试图将getaddrinfo中的NULL更改为ip地址,但它总是无法连接。
发布于 2013-11-06 15:30:02
您想通过互联网连接到哪个TCP/IP地址?
getaddrinfo()应该有某种类型的IP地址,否则它将不知道在哪里连接。这就像试图在邮件中寄出一封没有地址的信。
此外,有很多原因,你可能无法连接。你的防火墙可能挡住了什么东西..。另一头可能有防火墙挡住了什么东西..。AV软件甚至可以检测到它认为“奇怪”的连接,并阻止它.IP地址可能错了。
对于您来说,讨论一下您所做的故障排除以及您所看到的将是很有帮助的。
https://stackoverflow.com/questions/19814243
复制相似问题