c - TCP 套接字客户端 - 重新定义错误

标签 c linux sockets compiler-errors redefinition

所以我正在用 C 编写一个 TCP 套接字客户端(java 中的 TCP 服务器已经启动并运行),但是我有一些错误。 首先这是我的一些代码:

#include <stdint.h>
#include <stdlib.h> //for exit
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>  //for sockaddr_in and inet_addr() #include netinet/in.h
#include <W5500/w5500.h>
#include "mnWizSpi.h"   //PacketMaker() and BSB()
#include <linux/in.h> //for socket(), inet_addr(), send(), ect.

int main(void) {
    struct sockaddr_in servAddr; //Server address
    int prtno = 1234;

    //int prtno = atoi(argv[2]);
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { //AF_INET = TCP, AF_LOCAL = local to host
        perror("Could not open socket.");
        exit(1);
    }

    servAddr.sin_family = AF_INET;                  //Internet Address Family (2 for TCP)
    servAddr.sin_port = htons(prtno);               //Server Port

    if(connect(sock, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0) {
        perror ("Failed Connection");
        exit(1);
    }

    //when connected to the java server, the java server will ask for a handshake
    bzero(buffer, 256);
    int n = recv(sock, buffer, 255 , 0);
    if(n < 0)
    {
        perror("Failed to read message");
        exit(1);
    }

    //write message to server
    int n = send(sock, msg, strlen(msg), 0);
    if(n < 0)
    {
        perror("Failed to write message");
        exit(1);
    }
    return 0;
}

我得到的错误是重定义错误,来自 linux/in.h

redefinition of 'struct group_filter'
redefinition of 'struct group_req'
redefinition of 'struct group_source_req'
redefinition of 'struct in_addr'

等等。那些连同 34 个重新定义的错误。当我评论 #include 时,这些消失了,但是当我这样做时,我得到了一些 undefined reference 错误。

undefined reference to 'connect'
undefined reference to 'htons'
undefined reference to 'recv'
undefined reference to 'send'
undefined reference to 'socket'

我已经尝试过其他具有此类功能的 header ,但它们最终会出现相同的错误。

我确实检查了包含防护的头文件

#ifndef _LINUX_IN_H 
#define _LINUX_IN_H 

/* header content */

#endif /* _LINUX_IN_H */

他们似乎都很好。

我做错了什么/我该如何解决?

提前致谢!

编辑: 我在 ubuntu VM 中工作

最佳答案

您包括 #include <arpa/inet.h>标题,其中的文档说

Inclusion of the <arpa/inet.h> header may also make visible all symbols 
from <netinet/in.h> and <inttypes.h>.

netinet/in.h的冲突和 linux/in.h header 是一个已知问题。

结账关注thread对于可能的宏解决方案

关于c - TCP 套接字客户端 - 重新定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112400/

相关文章:

c - 如何在C中使用字符串存储100位数字

linux - 从大型 malloced 数组读取而不是仅存储时,页面错误的数量是原来的两倍?

php - 用于 PDF 缩小的 ImageMagick 替代方案

sockets - boot2docker shellinit命令以错误 “Error requesting socket: exit status 255”结尾

c - libreadline.so.7 : undefined symbol: UP

将包含 float 的结构转换为对网络更友好的结构

c - 如何使用 C 中的插入排序为每一行提供有序索引来对文件中的字符串进行排序

python - 通过 STDOUT 与正在运行的进程通信

c++ - 如何在 Linux 上强制关闭套接字?

c - 使用本地主机 53 或给定 dns 服务器上的套接字发送反向 dns 请求