在“常量常量前出现预期的声明说明符或'...”之类的错误

我正在尝试使用一个用C语言开发的简单网络客户端从google.com发送和接收数据。代码如下。我有几个问题。

  1. 该代码不会产生任何错误,除了:

/tmp/ccryVL4g.o:在函数error': client.c:(.text+0x318): undefined reference to stderror'中 collect2:错误:ld返回1退出状态

这是什么错误,我该如何解决?

  1. 在这里,我只是使用sprintf()函数阅读HTML。但是,如果我想使用网络客户端在Google上搜索特定内容(例如天气),该怎么办?

代码:

'''

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>

// Preprocessor Macros for readability purposes
#define SERVER_PORT 80
#define MAXIMUMLINE   4096
#define SA struct sockaddr


void error(const char *fmt,...);


int main(int argc,char **argv){

    // Define some local variables
    int sockfd,n;
    int sendbytes;
    struct sockaddr_in servaddr;
    char sendline[MAXIMUMLINE];
    char recvline[MAXIMUMLINE]; 


    // Usage Check
    if (argc < 2){

        error("usage: %s <server address>",argv[0]);

    }

    // Create a new socket
    if ( (sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0 )
        error("Error while creating a socket");

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVER_PORT);


    // Translate the Address to a binary equivalent
    if (inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0)
        error("inet_pton error for %s",argv[1]);


    // Connect to the server
    if (connect(sockfd,(SA *) &servaddr,sizeof(servaddr)) < 0)
        error("Connection Failed. Program Terminating...");


    // Connected. Preparing the message
    sprintf(sendline,"GET / HTTP/1.1\r\n\r\n");    
    sendbytes = strlen(sendline);


    // Send the Request
    if (write(sockfd,sendline,sendbytes) != sendbytes)
        error("Error occurred while sending a request.");


    // Receive the Respons  
    memset(recvline,MAXIMUMLINE);


    // Read the Response
    while ( (n = read(sockfd,recvline,MAXIMUMLINE-1)) > 0)
    {
        printf("%s",recvline); 
    }

    if (n < 0)
        error("Error while reading the data");


    // Exit
    exit(0);

}


void error(const char *fmt,...){

    int errno_save;
    va_list  ap;

    // Save
    errno_save = errno;

    // Print out the fmt+args to standard out
    va_start(ap,fmt);
    vfprintf(stdout,fmt,ap);
    fprintf(stdout,"\n");


    // Print the message if errno was set
    if (errno != 0){

        fprintf(stdout,"(errno = %d) : %s\n",errno_save,stderror(errno_save));
        fprintf(stdout,"\n");
        fflush(stdout);
    }

    va_end(ap);

    exit(1);

}

'''

错误:

error: expected declaration specifiers or ‘...’ before numeric constant
 #define MAXIMUMLINE   4096

note: in expansion of macro ‘MAXIMUMLINE’
 char sendline(MAXIMUMLINE);

 error: ‘sendline’ undeclared (first use in this function)
  sprintf(sendline,"GET / HTTP/1.1\r\n\r\n"); 

In function ‘error’:
client.c:105:2: error: too few arguments to function ‘fprintf’
  fprintf(stdout);
a771635193 回答:在“常量常量前出现预期的声明说明符或'...”之类的错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3161822.html

大家都在问