/* The Hello World web server. Type http://localhost:2001 in your web browser after running this program. */ #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define SOURCE_PORT 2001 /* Use a large port number. */ #define SIZE 1024 /* This is used to initialize an address with a port */ void initaddr(struct sockaddr_in * s,char *hostname,int port) { s->sin_family=AF_INET; s->sin_port=htons(port); inet_aton(hostname,&s->sin_addr); memset(&s->sin_zero,0,8); } int main() { struct sockaddr_in source_addr,new_addr; int sockfd; int newfd; char message[SIZE]; int sin_size=sizeof(struct sockaddr_in); sockfd=socket(PF_INET,SOCK_STREAM,0); initaddr(&source_addr,"127.0.0.1",SOURCE_PORT); bind(sockfd,(struct sockaddr *)&source_addr,sin_size); listen(sockfd,1); while(newfd=accept(sockfd,(struct sockaddr *)&new_addr,&sin_size)) { recv(newfd,message,SIZE,0); strcpy(message,"Hello world!!\n"); send(newfd,message,strlen(message),0); close(newfd); } close(sockfd); }
Sunday, November 06, 2005
Hello World Web Server
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment