gpt4 book ai didi

c - 错误 : request for member ‘sin_addr’ in something not a structure or union

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:20 41 4
gpt4 key购买 nike

当我在 Linux 上编译代码(在下面找到)时,gcc 说:

error: request for member ‘sin_addr’ in something not a structure or union

但是我没有发现任何问题。

我是否遗漏了头文件中的任何内容,或者我是否存在一些语法错误?

#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define _PORT_ 98

char *SerIP = "127.0.0.1";
char *File;

int main(int argc, char *argv[])
{
int Clisock, Sersock;
struct sockaddr_in SerInfo, CliInfo;
int filefd, nByte;
char buf[BUFSIZ];

if(argc < 2)
{
fputs("Too less arguments!\n", stderr);
exit(1);
}
memset(&CliInfo, 0, sizeof(CliInfo));
File = argv[1];

SerInfo.sin_family = AF_INET;
if(inet_pton(AF_INET, SerIP, &SerIP.sin_addr.s_addr) < 0)
{
perror("inet_pton");
exit(1);
}
SerInfo.sin_port = htons(_PORT_);

if ((Clisock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("scoket");
exit(1);
}

if(connect(Clisock, (struct sockaddr*)&SerInfo, sizeof(SerInfo)) < 0)
{
perror("connect");
exit(1);
}
while (1)
{
filefd = open(File, O_RDONLY);
while((nByte = read(filefd, buf, BUFSIZ)) > 0)
{
if(send(Clisock, buf, nByte, 0) < 0)
{
perror("send");
exit(1);
}
if(nByte < BUFSIZ)
{
send(Clisock, "END", 3, 0);
break;
}

}
send(Clisock, "END", 3, 0);
break;
}
close(Clisock);
close(filefd);
return 0;
}

最佳答案

if(inet_pton(AF_INET, SerIP, &SerIP.sin_addr.s_addr) < 0)

SerIP.sin_addr 没有意义。 SerIP 是一个 char*

关于c - 错误 : request for member ‘sin_addr’ in something not a structure or union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8562435/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com