uint64_t GetTimeStamp()
{
struct timespec start;
if ((clock_gettime(CLOCK_MONOTONIC, &start)) == -1)
{
perror("clock gettime\n");
}
return(start.tv_sec + start.tv_nsec * 1e-9); // seconds
}
const struct sigevent *intHandler(void *arg, int id)
{
start_clock = ClockCycles();
// printf("start clock: %lld\n", start_clock);
return(&event);
}
void *ConfigureISR()
{
// Get IO privilege
ThreadCtl(_NTO_TCTL_IO, 0);
// Setup COID and event
SIGEV_INTR_INIT(&event);
interruptID = InterruptAttach(intrNum, intHandler, NULL, 0, 0);
if (interruptID == -1)
{
fprintf(stderr, "can't attach to IRQ %d\n", intrNum);
perror(NULL);
exit(EXIT_FAILURE);
}
while (loop)
{
InterruptWait(0, NULL);
end_clock = ClockCycles();
InterruptLatency = (uint32) ((end_clock - start_clock) * 1000000/(SYSPAGE_ENTRY(qtime)->cycles_per_sec));
printf("Current Interrupt Latency microseconds: %ld\n", InterruptLatency);
InterruptUnmask(intrNum, interruptID);
measurements[17] = InterruptLatency;
}
return(EXIT_SUCCESS);
}
int CreateSocket()
{
// pthread_attr_t attr;
// Socket creation for UDP
acceptSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (acceptSocket == -1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
rc = bind(acceptSocket, (struct sockaddr *)&addr, sizeof(addr));
fcntl(acceptSocket, O_NONBLOCK);
if (rc == -1)
{
printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
return -1;
}
else
{
printf("Socket an port %d \n", port);
}
return acceptSocket;
}
int main(int argc, char *argv[])
{
pthread_t thread_id, thread_id1;
Xcp_Initialize();
pthread_create(&thread_id1, NULL, &ConfigureISR, NULL);
if ((sock = CreateSocket()) < 0)
{
perror("Create_socket");
exit(1);
}
if (pthread_create(&thread_id, NULL, &rastertask, NULL))
{
perror("pthread_create");
exit(1);
}
do
{
socklen_t len;
len = sizeof(client);
printf("NEW DATA ARRIVED\n");
// non blocking mode : MSG_DONTWAIT
rc = recvfrom(sock, buf, 256, 0, (struct sockaddr *) &client, &len);
Receive = GetTimeStamp();
receiveTime = (uint32) (Receive/1000000);
printf("Receive time: %lu\n", receiveTime);
// printf("RECEIVE from Time in microseconds: %lu\n", ReceiveTimestamp);
// measurements[19] = ReceiveTimestamp;
if (rc == 0)
{
printf("Server has no connection..\n");
loop = 0;
break;
}
if (rc == -1)
{
if (errno == SIGINT)
continue;
printf("Oh dear, something went wrong with read()! s\n", strerror(errno));
loop = 0;
break;
}
XcpIp_RxCallback((uint16) rc, (uint8 *) buf, (uint16) port);
} while (1);
close(sock);
return 0;
}
以上是服務器代碼,它通過ip地址和端口號接收數據。在多線程程序中不接收UDP和輸出數據
稍後將數據發送回客戶端。我正在接收數據,但在收到數據後,獲取時間戳(您可以在上面的代碼中看到:Send= GetTimeStamp()
)。
爲什麼不打印任何東西?我通過套接字接收數據,然後我給printf("new data arrived\n");
,然後它也不打印。我也沒有收到任何時間!有人能告訴我可能是什麼原因嗎?
接收到主函數中的數據後:它不是按照接收到的數據進行打印。奇怪的東西:( – user3635707
*格式*您的代碼(使用正確的縮進),*刪除*不相關的部分,拿出一個最小的例子。 –