#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MESG "<head></head><body><html><h1>Successfully Connected</h1>\
<p>You have connected to the server smoothly

main()
{
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket 创建出错");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))==-1){
perror("bind 出错");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen 出错");
exit(1);
}
while(1)
{
int sin_size = sizeof(struct sockaddr_in);
client_fd = accept(sockfd,(struct sockaddr *)&remote_addr,
&sin_size);
if(client_fd == -1)
{
perror("accept 出错");
continue;
}
printf("Now I am receiving a connection from %s\n",inet_ntoa(remote_addr.sin_addr));
if(!fork())
{
if(send(client_fd,MESG,strlen(MESG),0)==-1)
perror("send 出错");
close(client_fd);
exit(0);
}
close(client_fd);
}
}