#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/socket.h>
#define PORTNUMBER 3333
int main(int argc,char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size; int nbytes; char buffer[1024];
//socket
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n",strerror(errno)); exit(1); } //address
bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(PORTNUMBER);
//bind
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr_in))==-1) { fprintf(stderr,"Bind error:%s\n",strerror(errno)); exit(1); }
//listen
if(listen(sockfd,5)==-1) { fprintf(stderr,"Listen error:%s\n",strerror(errno)); exit(1); }
while(1) { sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr,"Accept error:%s\n",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
if((nbytes=read(new_fd,buffer,1024))==-1) { fprintf(stderr,"Read Error:%s\n",strerror(errno)); exit(1); } buffer[nbytes]='\0'; printf("Server received %s\n",buffer); close(new_fd); } }
|