对_red的未定义引用

时间:2012-10-18 19:41:30

标签: c sockets cygwin

我在编写一个简单的客户端程序“socket编程”时使用cygwin编译代码我得到了这个错误“未定义引用'_red'  代码如下。我一直用这个语句编译gcc client.c 我真的不明白为什么我会收到这个错误。我真的可以使用一些帮助。代码如下。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <netinet/in.h>

#define SERVER_TCP_PORT 2000
#define BUFLEN 256

int main(int argc,char **argv)
{
    int n,bytes_to_read;
    int sd,port;
    struct hostent *hp;
    struct sockaddr_in server;
    char *host,*bp,rbuf[BUFLEN],sbuf[BUFLEN];
    switch(argc) {
        case 2:
            host = argv[1];
            port = atoi(argv[2]);
            break;
        default:
            fprintf(stderr,"Usage: %s host[port] \n",argv[0]);
            exit(1);
    }
    /*Create a streamm socket*/
    if ((sd=socket(AF_INET,SOCK_STREAM,0))==-1){
        fprintf(stderr,"Can't create a socket\n");
        exit(1);
    }
    bzero ((char *)&server,sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    if((hp = gethostbyname(host))==NULL){
        fprintf (stderr,"Can't get server's address\n");
        exit(1);
    }
    //bcopy(hp->h_addr,(char *)&server.sin_addr,hp->h_length);
    /*connecting to the server*/
    if (connect (sd,(struct sockaddr *)&server,sizeof(server))==-1){
        fprintf(stderr,"Can't connect \n");
        exit(1);
    }
    printf("Connected:server's address is %s\n",hp->h_name);
    printf("Transmit: \n");
    gets(sbuf);                  /*get user's text*/
    write (sd,sbuf,BUFLEN);      /*send it out*/
    printf("Receive: \n");
    bp = rbuf;
    bytes_to_read = BUFLEN;
    while ((n=red(sd,bp,bytes_to_read))>0){
        bp+=n;
        bytes_to_read -=n;
    }
    printf("%s\n",rbuf);
    close(sd);
    return(0);
}

1 个答案:

答案 0 :(得分:2)

在线

while ((n=red(sd,bp,bytes_to_read))>0){

它有红色,你可能意思是读