我正在编写一个简单的客户端程序“套接字编程”,在使用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);
}