繁体   English   中英

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

[英]TCP Socket Client - Redefinition Errors

所以我正在用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 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.hlinux/in.h标头的冲突是一个已知问题。

检出以下线程以获取宏的可能解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM