我正在使用 Linux 中的 Sockets 学习基本的网络编程。我编写了一个示例程序,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#define SERVER_PORT 9000
unsigned int client_s;
int main(int argc, char *argv[])
{
unsigned int server_s = 0; ///server socket
struct sockaddr_in server_addr; /// server address
struct sockaddr_in client_addr; ///client address
struct in_addr client_pi_addr; /// client ip address
int addr_len; /// internet address length
unsigned int ids; ///thread arguments
/// create a new socket
server_s = socket(AF_INET, SOCK_STREAM, 0);
if( !server_s)
return -1;
server_addr.sin_family = AF_INET; /// internet
server_addr.sin_port = htons(SERVER_PORT); /// host to network string : port 9000
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /// listen on any (all) addresses, host to network long
/// bind it
if (bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 )
return -2;
/// listen over it
if( listen(server_s, 100) < 0 ) /// 100 : backlog
{
perror("Error in listen");
}
while (1)
{
addr_len = sizeof(client_addr);
client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len);
if(client_s < 0)
{
perror("Error accepting connection");
exit(-1);
}
else
{
ids = client_s;
char outbuf[128];
strcpy(outbuf, "HTTP/1.1 200 OK\nContent-Type:text/html\n\n");
if( send(client_s, outbuf, strlen(outbuf), 0) < 0 )
perror("ERROR in send");
if( send(client_s, "OKIE", 4, 0) < 0 )
perror("Error in sending oK : ");
close(client_s);
}
}
close (server_s);
return 0;
}
现在,当我在 IP:9000 上使用 Chrome 进行查询时,我在浏览器中得到了预期的响应“OKIE”。但是当我尝试在 Internet Explorer 10 中执行相同操作时,没有响应。IE 报告此页无法显示。
任何解释都会非常有帮助。
我出于好奇而发布这个问题。如果它完全没有意义,请随时关闭这个问题:)。