下面是一个TCP服务器程序。服务器进程在收到客户进程的连接请求并建立连接成功后,将在标准输出上显示客户进程所在的主机IP地址,并使用write函数通过套接字向客户进程返回数据信息“Hello,client!”。填空并完成下列TCP服务器程序。
#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> #include <signal.h> #include <netdb.h> #include <unistd.h> #include <arpa/inet.h> #define SERVERPORT 3456 #define BACKLOG 5 int main(int argc,char *argv[]) { int sockfd,connetfd; struct sockaddr_in srvaddr; struct sockaddr_in cliaddr; socklen_t sinSize; sockfd = __________________(AF_INET,SOCK_STREAM,0); if(sockfd <0) { fprintf(stderr,"socket error when creating...\n"); exit(1); } bzero(&srvaddr,sizeof(srvaddr)); srvaddr.sin_family=AF_INET; srvaddr.sin_port = htons( _______________________ ); srvaddr.sin_addr.s_addr = htonl( _______________________ ); if( _______________ (sockfd,(struct sockaddr *)&srvaddr,sizeof(struct sockaddr))<0) { fprintf(stderr,"Bind error.\n"); exit(1); } if (_______________ ( _______________ ,BACKLOG)< 0) { fprintf(stderr,"Listen error.\n"); exit(1); } sinSize = sizeof(struct sockaddr_in); for( ; ; ) { connetfd = _______________ (sockfd,(struct sockaddr *)&cliaddr,&sinSize); if (connetfd<0) { fprintf(stderr,"accept error.\n"); exit(1); } fprintf(stderr,"server: have got a connection from %s\n",inet_ntoa(cliaddr.sin_addr)); if (write(_______________,"Hello,client!\n",15)==-1) fprintf(stderr,"write error\n."); ______________________; } ________________________; }