我已经用 c (cygwin) 编写了 ac socket 程序,我想将一些 html 代码发送到我的浏览器。
我已经编写了服务器,如果我运行它并在我的浏览器中输入localhost:8888
。我的程序说它发送了正确数量的字节,但我的浏览器似乎什么也没收到。
发送代码:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 1);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
char *reply =
"HTTP/1.1 200 OK\r\n<Content-Type: text/html\r\n\r\n<!DOCTYPE html>\r\n<html>\r\n<body>\r\n<h1>My First Heading</h1>\r\n<p>My first paragraph.</p>\r\n</body>\r\n</html>";
int i = send(new_socket,reply,strlen(reply),0);
printf("%d",i);
shutdown(new_socket,2);
return 0;
}
输出程序:
绑定完成
等待传入连接
已接受连接
98
输出浏览器:
错误 103 (net::ERR_CONNECTION_ABORTED):未知错误
怎么了 ?