Reputation: 137
myheader.h
#ifndef _MYHEAD_
#define _MYHEAD_
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <time.h>
#endif
echoClient.c
#include "myheader.h"
int clock_gettime(clockid_t clock_id, struct timespec *tp);
int nanosleep(const struct timespec *req, struct timespec *rem);
typedef struct timespec // error message line
{
time_t tv_sec;
long tv_nsec;
};
int main(int argc, char* argv[])
{
int s;
char* servName;
int servPort;
char* string;
char buf[256 + 1];
int len = 0;
int maxLen = sizeof(buf);
struct sockaddr_in serverAddr;
timespec t1,t2,t3;
double practice;
int temp = 0;
int n;
if(argc != 4)
{
printf("Usage: client <server> <port> <virtual packet size> <string>\n");
exit(1);
}
servName = argv[1];
servPort = atoi(argv[2]);
string = argv[3];
memset(&buf, 0, sizeof(buf));
/* Create remote (server) socket address */
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
inet_pton(AF_INET, servName, &serverAddr.sin_addr); // server IP addr
serverAddr.sin_port = htons(servPort);
practice = clock_gettime(CLOCK_REALTIME,&t3);
printf("time %f\n",practice);
/* Creat socket */
if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("Error: socket creation failed\n");
exit(1);
}
// send the echo message
n = sendto(s, string, strlen(string), 0,
(struct sockaddr *)&serverAddr, sizeof(serverAddr));
printf("-- actual tx frame size: (%d bytes)\n",n);
/* here, the message has been sent, and the echo will come */
/* receive Echo */
memset(&buf, 0, sizeof(buf));
len = recvfrom(s, buf, sizeof(buf), 0, NULL, NULL);
buf[len] = '\0';
printf("Echoed string received: ");
printf("%s\n", buf);
close(s);
exit(0);
}
As you can see the topic,
"error:redefinition of 'struct timespec'
have been happened when i try to compile.
/usr/include/time.h:122: error: previous definition of 'struct timespec'
have been also happened. How can i modify this code.
Please help me.
Upvotes: 0
Views: 9327
Reputation: 1945
I got the same error in Ecliepse Neon IDE and i resolved it by adding '-DHAVE_STRUCT_TIMESPEC' in C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous -> Others flag
https://github.com/ghostlander/nsgminer/issues/1
Upvotes: 0
Reputation: 16223
timespec
is defined in the following way into time.h
struct timespec
{
__time_t tv_sec; /* Seconds. */
__syscall_slong_t tv_nsec; /* Nanoseconds. */
};
Remove the struct definition in your code.
Prototype at the top of your code must be removed, also
int clock_gettime(clockid_t clock_id, struct timespec *tp);
int nanosleep(const struct timespec *req, struct timespec *rem);
Are already defined in time.h
Last thing, definition of
timespec t1,t2,t3;
Must be
struct timespec t1,t2,t3;
EDIT
If you add -Wall
option compiler gives you warning about unused variable: temp
, maxlen
, t1
and t2
Upvotes: 2
Reputation: 23058
struct timespec
is defined in time.h
. Don't define your own again.
You should remove the typedef struct timespec { ... };
part in echoClient.c
.
Upvotes: 1