-3

当我编译这个文件时,它一直给我

Undefined reference to size
Undefined reference to file

我在第二次编译它两次时它给了我两个错误,而第一次它只给我第二个错误。

我可以确切地知道问题是什么以及如何解决它?

ps:我gcc -o ser server.c用来编译

抱歉,我的网络有问题

 #include<stdio.h>
 #include<errno.h>
 #include<arpa/inet.h>
 #include<string.h>
 #include<netinet/in.h>
 #include<sys/types.h>
 #include<sys/socket.h>
 #include<stdlib.h>
 #include<fcntl.h>

#define Max_len 1049
void error (char * str)
{
perror(str);
exit(0);
}

这是完整的代码

int main (int argc, char ** arg)
 {
      //------------------------- define data type
 int listenfd,connfd,clilen,portno,fil_fd;
 char buff[Max_len], *str,f_name[100],car=0xAA;
 struct sockaddr_in servaddr,cliaddr;
 FILE * file_ptr;         //to store the pointer that point to the read file
     //------------------------- socket intinalizing
 if(argc != 2)          //of user forget to insert the port number
   error("missing port number to complet establishment\n");

 portno=atoi(arg[1]);       //convert the port to int and check if it was within the rang
 if((portno<=22000) || (portno>=23000))
   error("your port number not valid .... pleas insert one in reng (22000,22999)\n");

 listenfd=socket(AF_INET,SOCK_STREAM,0);     //creat the listing socket
     bzero((char *)&servaddr ,sizeof(servaddr));
     servaddr.sin_family=AF_INET;       //Ipv4
     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
     servaddr.sin_port=htons(portno);

 bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));//bind the liten socket to specific port
 listen(listenfd,5);
     //-------------------------- server start to wait to handle requests
 for(;;)
 {
 printf("server wait for connections\n");
 bzero((char *)&cliaddr ,sizeof(cliaddr));  //clear the previus client information
 clilen=sizeof(cliaddr);                //client informaton container
 connfd=accept(listenfd,(struct  sockaddr *)&cliaddr,&clilen);
 if(connfd<0)
  error("error in accept this connection\n"); 
   //---------------------------------------        //start the secret hand chacking
 snprintf(buff,/*sizeof(buff)*/1,"%c",0x55);
 write(connfd,buff,sizeof(buff));
    //---------------------------------------       //read file name

  car=0xAA;
  read(connfd,buff,size(buff));
  printf("client reply is %c",buff[0]);
 if(car==buff[0])
     {
       printf("wait for file name \n");
      // bzero(buff,sizeof(buff));
   printf("1#%s\n",buff);
       read(connfd,buff,sizeof(buff));
   printf("2#%s\n",buff);
       strncpy(f_name,buff,sizeof(buff)-1);
       printf("file return name is %s\n",buff);
 }
 else
  error("this is not secure client\n");

 //--------------------------------------   if file found send 1 else 0
 fil_fd=open(f_name,O_RDONLY);      //file opent to be read only
 if(fil_fd < 0)
 {
   snprintf(buff, sizeof(buff),"%d",0);
   write(connfd,buff,1);
   file("file is not found\n");     //retminate connection only
    break;
 }
 else
 {
  snprintf(buff, sizeof(buff),"%d",1);
      write(connfd,buff,1);
  printf("file exists.Send 1 to client\nreading and send the deil\n");
 //-----------------------------------reading and sending
 while(read(buff,1048,fil_fd)>0)
          { 
             printf("%s \n\n",buff);
              write(connfd,buff,sizeof(buff)); //sending the file process
           }//end of uploading

  }//end of if_else
   //-------------------------------------------- close file resources " file"
  close(fil_fd);
  close(connfd);
   }//end of for()
     //--------------------------------------------prepair the server connection
  close(listenfd);
 exit(0);

    return 0;
    }//main()
4

1 回答 1

2

您在使用未定义的标识符时只有两个简单的错误。这个:

read(connfd,buff,size(buff));

应该是这样的::

read(connfd,buff,sizeof(buff));

和这个:

file("file is not found\n");

应该是这样的:

error("file is not found\n");

当您使用这样的未定义标识符时,您的编译器无法知道它们只是错误,因为您可能已经单独编译了另一个定义它们的源文件,所以它只是假设您在某处调用size()file()定义了函数,然后去查看为他们。当它找不到它们时,它会给你“未定义的引用”错误。

于 2013-10-24T23:56:57.230 回答