【问题标题】:TCP Socket Client - Redefinition ErrorsTCP 套接字客户端 - 重新定义错误
【发布时间】:2015-08-20 07:46:08
【问题描述】:

所以我在 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 to 'connect'
undefined reference to 'htons'
undefined reference to 'recv'
undefined reference to 'send'
undefined reference to 'socket'

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

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

#ifndef _LINUX_IN_H 
#define _LINUX_IN_H 

/* header content */

#endif /* _LINUX_IN_H */

他们似乎都很好。

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

提前致谢!

编辑: 我在 ubuntu 虚拟机中工作

【问题讨论】:

    标签: c linux sockets compiler-errors redefinition


    【解决方案1】:

    您包括 #include &lt;arpa/inet.h&gt; 标头,其中的文档说

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

    netinet/in.hlinux/in.h 标头的冲突是一个已知问题。

    thread 之后结帐以获取可能的宏解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-29
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多