user-0 提问: 求大神帮助

回复:
user: user-1
content: 引用 4 楼 xihu1364 的回复: server [code=c] #include <stdlib.h>    #include <stdio.h>    #include <netdb.h>    #include <sys/types.h>    #include <sys/socket.h>    #include  <string.h>    #include <netinet/in.h>    #include <arpa/inet.h>    #include <unistd.h>    #include <fcntl.h>    #include <time.h>       #define MAXDATASIZE 256       #define SERVPORT 4444      #define BACKLOG 1        #define STDIN 0                 int main(void)   {       FILE *fp,*fs_record,*f_read;               int sockfd,client_fd;         int sin_size;       struct sockaddr_in my_addr, remote_addr;         char buf[256],ch;                       char buff[256];       char send_str[256];         int recvbytes;                 fd_set rfd_set, wfd_set, efd_set;     struct timeval timeout;         int ret;                   char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};       struct tm *p;       time_t timep;          if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {              perror("socket");           exit(1);       }             bzero(&my_addr, sizeof(struct sockaddr_in));       my_addr.sin_family=AF_INET;      my_addr.sin_port=htons(SERVPORT);         inet_aton("127.0.0.1", &my_addr.sin_addr);          if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {                 perror("bind");           exit(1);       }       if (listen(sockfd, BACKLOG) == -1) {            perror("listen");           exit(1);       }          sin_size = sizeof(struct sockaddr_in);       if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) {             perror("accept");           exit(1);       }          fcntl(client_fd, F_SETFD, O_NONBLOCK);     recvbytes=recv(client_fd, buff, MAXDATASIZE, 0);          buff[recvbytes] = '\0';          fflush(stdout);        if((fp=fopen("name.txt","a+"))==NULL)              {                      printf("can not open file name.txt,exit...\n");                    return -1;             }       if((fs_record=fopen("s_record.txt","a+"))==NULL)               {                      printf("can not open file s_record.txt,exit...\n");                    return -1;             }                  fprintf(fp,"%s\n",buff);             while (1) {           FD_ZERO(&rfd_set);          FD_ZERO(&wfd_set);         FD_ZERO(&efd_set);           FD_SET(STDIN, &rfd_set);             FD_SET(client_fd, &rfd_set);            FD_SET(client_fd, &wfd_set);            FD_SET(client_fd, &efd_set);            timeout.tv_sec = 10;         timeout.tv_usec = 0;                ret = select(client_fd + 1, &rfd_set, &wfd_set, &efd_set, &timeout);           if (ret == 0) {               continue;           }                      if (ret < 0) {               perror("select error: ");               exit(-1);           }            if(FD_ISSET(STDIN, &rfd_set))            {               fgets(send_str, 256, stdin);             send_str[strlen(send_str)-1] = '\0';               if (strncmp("quit", send_str, 4) == 0) {                     close(client_fd);                   close(sockfd);                     exit(0);               }               if (strcmp("record", send_str) == 0) {                      if((f_read=fopen("s_record.txt","r"))==NULL)                       {                              printf("can not open file s_record.txt,exit...\n");                            return -1;                     }                   putchar('\t');                   while(!feof(f_read)){                           ch=fgetc(f_read);                           putchar(ch);                     if(ch=='\n')                                                                     putchar('\t');                   }                                                                                         fclose(f_read);                   printf("\nServer: ");                   fflush(stdout);                   continue;               }                  time(&timep);               p=localtime(&timep);               send(client_fd, send_str, strlen(send_str), 0);               printf("        ( %4d %d %2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               fprintf(fs_record,"%s\n",send_str);               fprintf(fs_record,"        ( %d %d %2d   ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fs_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);           }              if (FD_ISSET(client_fd, &rfd_set))           {               recvbytes=recv(client_fd, buf, MAXDATASIZE, 0);             if (recvbytes == 0) {                   close(client_fd);                   close(sockfd);                  exit(0);               }               time(&timep);               p=localtime(&timep);               buf[recvbytes] = '\0';               printf("%s:%s\n",buff,buf);            printf("        ( %d%d%2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               printf("Server: ");                  fprintf(fs_record,"%s:%s\n",buff,buf);            fprintf(fs_record,"        ( %d%d%2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fs_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               fprintf(fs_record,"Server: ");               fflush(stdout);           }            if (FD_ISSET(client_fd, &efd_set)) {               close(client_fd);                exit(0);           }       }   }     [/code] 哪里拷贝过来的代码？ 不太好！不推荐

user: user-2
content: 引用 1 楼 wangdahu888 的回复:网游穷三代，Dota毁一生 我最爱的就是dota了

user: user-3
content: server [code=c] #include <stdlib.h>    #include <stdio.h>    #include <netdb.h>    #include <sys/types.h>    #include <sys/socket.h>    #include  <string.h>    #include <netinet/in.h>    #include <arpa/inet.h>    #include <unistd.h>    #include <fcntl.h>    #include <time.h>       #define MAXDATASIZE 256       #define SERVPORT 4444      #define BACKLOG 1        #define STDIN 0                 int main(void)   {       FILE *fp,*fs_record,*f_read;               int sockfd,client_fd;         int sin_size;       struct sockaddr_in my_addr, remote_addr;         char buf[256],ch;                       char buff[256];       char send_str[256];         int recvbytes;                 fd_set rfd_set, wfd_set, efd_set;     struct timeval timeout;         int ret;                   char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};       struct tm *p;       time_t timep;          if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {              perror("socket");           exit(1);       }             bzero(&my_addr, sizeof(struct sockaddr_in));       my_addr.sin_family=AF_INET;      my_addr.sin_port=htons(SERVPORT);         inet_aton("127.0.0.1", &my_addr.sin_addr);          if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {                 perror("bind");           exit(1);       }       if (listen(sockfd, BACKLOG) == -1) {            perror("listen");           exit(1);       }          sin_size = sizeof(struct sockaddr_in);       if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) {             perror("accept");           exit(1);       }          fcntl(client_fd, F_SETFD, O_NONBLOCK);     recvbytes=recv(client_fd, buff, MAXDATASIZE, 0);          buff[recvbytes] = '\0';          fflush(stdout);        if((fp=fopen("name.txt","a+"))==NULL)              {                      printf("can not open file name.txt,exit...\n");                    return -1;             }       if((fs_record=fopen("s_record.txt","a+"))==NULL)               {                      printf("can not open file s_record.txt,exit...\n");                    return -1;             }                  fprintf(fp,"%s\n",buff);             while (1) {           FD_ZERO(&rfd_set);          FD_ZERO(&wfd_set);         FD_ZERO(&efd_set);           FD_SET(STDIN, &rfd_set);             FD_SET(client_fd, &rfd_set);            FD_SET(client_fd, &wfd_set);            FD_SET(client_fd, &efd_set);            timeout.tv_sec = 10;         timeout.tv_usec = 0;                ret = select(client_fd + 1, &rfd_set, &wfd_set, &efd_set, &timeout);           if (ret == 0) {               continue;           }                      if (ret < 0) {               perror("select error: ");               exit(-1);           }            if(FD_ISSET(STDIN, &rfd_set))            {               fgets(send_str, 256, stdin);             send_str[strlen(send_str)-1] = '\0';               if (strncmp("quit", send_str, 4) == 0) {                     close(client_fd);                   close(sockfd);                     exit(0);               }               if (strcmp("record", send_str) == 0) {                      if((f_read=fopen("s_record.txt","r"))==NULL)                       {                              printf("can not open file s_record.txt,exit...\n");                            return -1;                     }                   putchar('\t');                   while(!feof(f_read)){                           ch=fgetc(f_read);                           putchar(ch);                     if(ch=='\n')                                                                     putchar('\t');                   }                                                                                         fclose(f_read);                   printf("\nServer: ");                   fflush(stdout);                   continue;               }                  time(&timep);               p=localtime(&timep);               send(client_fd, send_str, strlen(send_str), 0);               printf("        ( %4d %d %2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               fprintf(fs_record,"%s\n",send_str);               fprintf(fs_record,"        ( %d %d %2d   ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fs_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);           }              if (FD_ISSET(client_fd, &rfd_set))           {               recvbytes=recv(client_fd, buf, MAXDATASIZE, 0);             if (recvbytes == 0) {                   close(client_fd);                   close(sockfd);                  exit(0);               }               time(&timep);               p=localtime(&timep);               buf[recvbytes] = '\0';               printf("%s:%s\n",buff,buf);            printf("        ( %d%d%2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               printf("Server: ");                  fprintf(fs_record,"%s:%s\n",buff,buf);            fprintf(fs_record,"        ( %d%d%2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fs_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               fprintf(fs_record,"Server: ");               fflush(stdout);           }            if (FD_ISSET(client_fd, &efd_set)) {               close(client_fd);                exit(0);           }       }   }     [/code]

user: user-3
content: client  #include <stdlib.h>    #include <stdio.h>    #include <netdb.h>    #include <sys/types.h>    #include <sys/socket.h>    #include <string.h>    #include <netinet/in.h>    #include <arpa/inet.h>    #include <unistd.h>    #include <fcntl.h>    #include <time.h>    #define SERVPORT 4444          #define MAXDATASIZE 256        #define STDIN 0               int main(void)   {       FILE *fc_record,*fc_read;       int sockfd;             int recvbytes;       char buf[MAXDATASIZE];            char *str;              char name[MAXDATASIZE];         char send_str[MAXDATASIZE];                         struct sockaddr_in serv_addr;           fd_set rfd_set, wfd_set, efd_set;     struct timeval timeout;      int ret;                       char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};       char ch;       struct tm *p;       time_t timep;       int first=1;              if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {           perror("socket");           exit(1);       }        bzero(&serv_addr, sizeof(struct sockaddr_in));       serv_addr.sin_family=AF_INET;       serv_addr.sin_port=htons(SERVPORT);       inet_aton("127.0.0.1", &serv_addr.sin_addr);       /*serv_addr.sin_addr.s_addr=inet_addr("192.168.0.101");*/          if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)        {            perror("connect");           exit(1);       }       fcntl(sockfd, F_SETFD, O_NONBLOCK);              if((fc_record=fopen("c_record.txt","a+"))==NULL)               {                      printf("can not open file c_record.txt,exit...\n");                    return -1;             }              printf("input your name:");              scanf("%s",name);       name[strlen(name)] = '\0';              printf("%s: ",name);                  send(sockfd, name, strlen(name), 0);         fprintf(fc_record,"%s: ",name);       fflush(stdout);              while (1)           {           FD_ZERO(&rfd_set);         FD_ZERO(&wfd_set);         FD_ZERO(&efd_set);         FD_SET(STDIN, &rfd_set);              FD_SET(sockfd, &rfd_set);            FD_SET(sockfd, &efd_set);            timeout.tv_sec = 10;         timeout.tv_usec = 0;         ret = select(sockfd + 1, &rfd_set, &wfd_set, &efd_set, &timeout);           if (ret == 0) {               continue;           }           if (ret < 0) {               perror("select error: ");               exit(-1);           }             if (FD_ISSET(STDIN, &rfd_set))            {               fgets(send_str, 256, stdin);               send_str[strlen(send_str)-1] = '\0';               if (strncmp("quit", send_str, 4) == 0) {                    close(sockfd);                   exit(0);               }               if (strcmp("record", send_str) == 0) {                      if((fc_read=fopen("c_record.txt","r"))==NULL)                      {                              printf("can not open file c_record.txt,exit...\n");                            return -1;                     }                   putchar('\t');                   while(!feof(fc_read)){                           ch=fgetc(fc_read);                           putchar(ch);                     if(ch=='\n')                                                                     putchar('\t');                   }                                                                                         fclose(fc_read);                   printf("\n%s: ",name);                   fflush(stdout);                   continue;               }                              time(&timep);               p=localtime(&timep);                       send(sockfd, send_str, strlen(send_str), 0);               printf("        ( %d %d  %2d   ", (1900+p->tm_year),(1+p->tm_mon),p->tm_mday);                              printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);                    fprintf(fc_record,"%s\n",send_str);               fprintf(fc_record,"        ( %d %d %2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fc_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);           }             if (FD_ISSET(sockfd, &rfd_set))            {               recvbytes=recv(sockfd, buf, MAXDATASIZE, 0);               if (recvbytes == 0)                {                   close(sockfd);                   exit(0);               }               buf[recvbytes] = '\0';               time(&timep);               p=localtime(&timep);               printf("Server: %s\n", buf);                           printf("        ( %d %d %2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               printf("%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);                   fprintf(fc_record,"Server:%s\n",buf);            fprintf(fc_record,"        ( %d  %d  %2d  ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);               fprintf(fc_record,"%s %2d:%2d:%2d )\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);               fprintf(fc_record,"%s: ",name);                              printf("%s: ",name);               fflush(stdout);           }            if (FD_ISSET(sockfd, &efd_set))            {               close(sockfd);               exit(0);           }       }   }

user: user-0
content: 大神，你能帮帮我吗？

user: user-4
content: 网游穷三代，Dota毁一生

user: user-5
content: 网络编程吧~~~

user: user-6
content: 引用 5 楼 bedynamic 的回复:[quote=引用 1 楼 wangdahu888 的回复:] 网游穷三代，Dota毁一生 我最爱的就是dota了[/quote] 被杀死了多少脑细胞啊？