【发布时间】:2015-09-23 11:56:38
【问题描述】:
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);
}
如你所见,
“错误:'struct timespec'的重新定义
在我尝试编译时发生了。
/usr/include/time.h:122: 错误:'struct timespec' 的先前定义
也发生过。我该如何修改此代码。
请帮帮我。
【问题讨论】:
-
编译器告诉你答案。
-
显示准确的编译命令。
标签: c networking struct