Reputation: 55
So I'm making a TCP socket client in C (the TCP server in java is already up and running), but I have a few errors. Firstly here is some of my code:
#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;
}
The errors I get are redefinition errors, coming from linux/in.h
redefinition of 'struct group_filter'
redefinition of 'struct group_req'
redefinition of 'struct group_source_req'
redefinition of 'struct in_addr'
ect. Those along with 34 redefined errors. These dissappear when I comment #include , but when I do that I get some undefined reference errors.
undefined reference to 'connect'
undefined reference to 'htons'
undefined reference to 'recv'
undefined reference to 'send'
undefined reference to 'socket'
I have tried other headers that have such functions, but they would end up with the same errors.
I did check the header files on include guards
#ifndef _LINUX_IN_H
#define _LINUX_IN_H
/* header content */
#endif /* _LINUX_IN_H */
and they all seem to be fine.
What am I doing wrong / how can I fix this?
Thanks in advance!
EDIT: I work in a ubuntu VM
Upvotes: 1
Views: 1610
Reputation: 9893
You are including #include <arpa/inet.h>
header, the documentation of which says
Inclusion of the <arpa/inet.h> header may also make visible all symbols
from <netinet/in.h> and <inttypes.h>.
The conflict of netinet/in.h
and linux/in.h
headers is a known issue.
Checkout following thread for possible solution with macros
Upvotes: 3