明明定义了结构体,为什么还是报错

typedef struct CLIENT{
	int fd;
	char *name;
	struct sockaddr_in addr;
	char *data;
};
void process_cli(CLIENT *client,char *recvbuf,int len);
void savedata(char *recvbuf,int len,char *data);

main(){
	int i,maxi,maxfd,sockfd;
	int nready;
	ssize_t n;
	fd_setrset,allset;
	int listenfd,connectfd;
	struct sockaddr_in server;
	CLIENT client[FD_SETSIZE];
	char recvbuf[MAXDATASIZE];

编译的时候提示:

c:22:1: warning: useless storage class specifier in empty declaration

 };

 ^

iotcpserver.c:23:18: error: unknown type name ‘CLIENT’

 void process_cli(CLIENT *client,char *recvbuf,int len);

                  ^

iotcpserver.c:26:1: warning: return type defaults to ‘int’ [-Wimplicit-int]

 main(){

 ^

iotcpserver.c: In function ‘main’:

iotcpserver.c:30:2: error: ‘fd_setrset’ undeclared (first use in this function)

  fd_setrset,allset;

  ^

iotcpserver.c:30:2: note: each undeclared identifier is reported only once for each function it appears in

iotcpserver.c:30:13: error: ‘allset’ undeclared (first use in this function)

  fd_setrset,allset;

             ^

iotcpserver.c:33:2: error: unknown type name ‘CLIENT’

  CLIENT client[FD_SETSIZE];

  ^

iotcpserver.c:60:12: error: request for member ‘fd’ in something not a structure or union

   client[i].fd=-1;

            ^

iotcpserver.c:67:3: error: ‘rset’ undeclared (first use in this function)

   rset=allset;

   ^

iotcpserver.c:76:17: error: request for member ‘fd’ in something not a structure or union

     if(client[i].fd<0){

                 ^

iotcpserver.c:77:15: error: request for member ‘fd’ in something not a structure or union

      client[i].fd=connectfd;

               ^

iotcpserver.c:78:15: error: request for member ‘name’ in something not a structure or union

      client[i].name=new char[MAXDATASIZE];

               ^

iotcpserver.c:78:21: error: ‘new’ undeclared (first use in this function)

      client[i].name=new char[MAXDATASIZE];

                     ^

iotcpserver.c:78:25: error: expected ‘;’ before ‘char’

      client[i].name=new char[MAXDATASIZE];

                         ^

iotcpserver.c:79:15: error: request for member ‘addr’ in something not a structure or union

      client[i].addr=addr;

               ^

iotcpserver.c:80:15: error: request for member ‘data’ in something not a structure or union

      client[i].data=new char[MAXDATASIZE];

               ^

iotcpserver.c:80:25: error: expected ‘;’ before ‘char’

      client[i].data=new char[MAXDATASIZE];

                         ^

怎么解决啊?

qq_莫非
浏览 11515回答 4
4回答

qq_莫非

#include <stdio.h> #include <strings.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/time.h> #include <stdlib.h>   #include <string.h>   #define PORT 1234 #define BACKLOG 5 #define MAXDATASIZE 1000   typedef struct CLIENT{     int fd;     char *name;     struct sockaddr_in addr;     char *data; }; void process_cli(CLIENT *client,char *recvbuf,int len); void savedata(char *recvbuf,int len,char *data);   int main(){     int i,maxi,maxfd,sockfd;         CLIENT client[FD_SETSIZE];     int nready;     ssize_t n;     fd_setrset,allset;     int listenfd,connectfd;     struct sockaddr_in server;     char recvbuf[MAXDATASIZE];     int sin_size;             if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){         perror("Creating socket falied.");         exit(1);     }     int opt=SO_REUSEADDR;     setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));           bzero(&server,sizeof(server));     server.sin_family=AF_INET;     server.sin_port=htons(PORT);     server.sin_addr.s_addr=htonl(INADDR_ANY);     if(bind(listenfd,(struct sockaddr *)&server,sizeof(struct sockaddr))==1){         perror("Bind error.");         exit(1);     }     if(listen(listenfd,BACKLOG)==-1){         perror("listen() error.\n");         exit(1);     }     sin_size=sizeof(struct sockaddr_in);     maxfd=listenfd;     maxi=-1;     for(i=0;i<FD_SETSIZE;i++){         client[i].fd=-1;     }     FD_ZERO(&allset);     FD_SET(listenfd,&allset);       while(1){         struct sockaddr_in addr;         rset=allset;         nready=select(maxfd+1,&rset,NULL,NULL,NULL);           if(FD_ISSET(listenfd,&rset)){             if((connectfd=accept(listenfd,(struct sockaddr *)&addr,&sin_size))==-1){                 perror("accept() error\n");                 continue;             }             for(i=0;i<FD_SETSIZE;i++)                 if(client[i].fd<0){                     client[i].fd=connectfd;                     client[i].name=new char[MAXDATASIZE];                                         //client[i].name=(char *)malloc(MAXDATASIZE);                                         //stu[i].name=(char *)malloc(MAXDATASIZE);                     client[i].addr=addr;                     client[i].data=new char[MAXDATASIZE];                                         //stu[i].snum=(char *)malloc(MAXDATASIZE);                     client[i].name[0]='\0';                     client[i].data[0]='\0';                                         //stu[i].name[0]='\0';                                         //stu[i].snum[0]='\0';                     printf("You got a connection from %s.",inet_ntoa(client[i].addr.sin_addr));                     break;                 }                 if(i==FD_SETSIZE)                     printf("too many clients\n");                 FD_SET(connectfd,&allset);                 if(connectfd>maxfd)                     maxfd=connectfd;                 if(i>maxi)                     maxi=i;                 if(--nready<=0)                     continue;         }         for(i=0;i<=maxi;i++){             if((sockfd=client[i].fd)<0))                 continue;             if(FD_ISSET(sockfd,&rset)){                 if((n=recv(sockfd,recvbuf,MAXDATASIZE,0))==0){                     close(sockfd);                     printf("Client(%s) closed connection.User's data:%s\n",client[i].name,client[i].data);                     FD_CLR(sockfd,&allset);                     client[i].fd=-1;                                        // free(stu[i].name);                                         //free(stu[i].data);                     free(client[i].name);                     free(client[i].data);                 }else                     process_cli(&client[i],recvbuf,n);                 if(--nready<=0)                     break;                 }             }         }         close(listenfd); }   void process_cli(CLIENT *client,char *recvbuf,int len){     char sendbuf[MAXDATASIZE];     recvbuf[len-1]='\0';     if(strlen(client->name)==0){         memcpy(client->name,recvbuf,len);         printf("Client's name is:%s.\n",client->name);         return;     }     printf("Received client(%s) message:%s\n",client->name,recvbuf);     savedata(recvbuf,len,client->data);     for(int i1=0;i1<len-1;i1++){         sendbuf[i1]=recvbuf[len-i1-2];     }     sendbuf[len-1]='\0';     send(client->fd,sendbuf,strlen(sendbuf),0); }   void savedata(char *recvbuf,int len,char *data){     int start=strlen(data);     for(int i=0;i<len;i++){         data[start+i]=recvbuf[i];     } }

望远

typedef struct CLIENT{     int fd;     char *name;     struct sockaddr_in addr;     char *data; };你这里要将struct CLIENT结构体类型自定义成什么类型也没有指定, CLIENT client[FD_SETSIZE];这里也没有指定为struct  CLIENT client[FD_SETSIZE];

慕移动6052691

我用VS2008 编译的,没有出现你说的问题,是其他报错;修改后:#include <winsock2.h>#pragma comment (lib,"ws2_32.lib")#define MAXDATASIZE 1024typedef struct CLIENT{ int fd; char *name; struct sockaddr_in addr; char *data;};void process_cli(CLIENT *client,char *recvbuf,int len);void savedata(char *recvbuf,int len,char *data);int main(){ int i,maxi,maxfd,sockfd; int nready; //ssize_t n; //fd_setrset,allset; int listenfd,connectfd; struct sockaddr_in server; CLIENT client[FD_SETSIZE]; char recvbuf[MAXDATASIZE];}

onemoo

如果这是 C 代码,那么你并没有把 struct CLIENT 定义为类型,typedef 的用法没写对。14 行那里 fd_setrset,allset 缺少类型。还有,看后面的报错,你写了 new char[MAXDATASIZE]; 这样的代码!?  你确定你写的是 C??
打开App,查看更多内容
随时随地看视频慕课网APP