Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
When I'm learning programing of UDP communication on Linux, I found a problem which I could not explain.
Here is what I'm meant to do:
1) Setup UDP listener in main thread and print out what it got;
2) Create a timer in main thread and also set a TimerExpiration Callback to send Pkt periodicially.
The problem is that after Callback send out the Pkt Receiver have to wait for 2s, which is also the interval for the timer, to get the data. But I think there should be not the delay between TX&RX. Could you help tell me why? Thanks!
Here is log:
Timer is created successfully!
Timer is armed successfully!
/* sending_sockfd not initialized, which means sys will choose one port to send pkt? */
peer_addr.sin_family = AF_INET; /* Host byte order */
peer_addr.sin_port = htons(UDP_SERVER_PORT); /* Short, network byte order */
peer_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /* Set to local ip addr */
bzero(&(peer_addr.sin_zero), 8); /* Zero the rest of the struct */
struct timespec tp;
printf("\n[%d.%ld]Thread_%lu_Sent %d bytes to %s\n", (unsigned int)(tp.tv_sec), tp.tv_nsec, (unsigned long int)(pthread_self()), numbytes, inet_ntoa(peer_addr. sin_addr )) ;
close ( sending_sockfd ) ;
int main ()
struct sigevent sig_en;
/* Setup a timer and arm it */
memset((char *)&sig_en, 0, sizeof (sig_en));
sig_en.sigev_notify = SIGEV_THREAD;
sig_en.sigev_notify_function = &send_pkt;
result = timer_create(CLOCK_MONOTONIC, &sig_en, &timer_id);
if (0 == result)
printf("Timer is created successfully!\n");
printf("Timer failed to be created!\n");
struct itimerspec new_value, old_value;
/* Initial expiration */
new_value.it_value.tv_sec = 2;
new_value.it_value.tv_nsec = 0;
/* each time an armed timer expires, the timer is reloaded from the value specified in new_value->it_interval */
new_value.it_interval.tv_sec = new_value.it_value.tv_sec;
new_value.it_interval.tv_nsec = new_value.it_value.tv_nsec;
result = timer_settime(timer_id, 0, &new_value, &old_value);
if (0 == result)
printf("Timer is armed successfully!\n");
printf("Timer failed to be armed!\n");
/* Setup UDP server */
int recv_sockfd; /* Receiver's sockfd */
struct sockaddr_in my_addr; /* My address information */
struct sockaddr_in peer_addr; /* Peer's address information */
unsigned int addr_len;
/* Init my_addr */
my_addr.sin_family = AF_INET; /* IPV4, host byte order */
my_addr.sin_port = htons(UDP_SERVER_PORT); /* Short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* Auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* Zero the rest of the struct */
/* Bind socket with the UDP port */
result = bind(recv_sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
if (-1 == result)
printf ("Failed to bind socket, err_no %d\n", errno);
/* Receive packet from peer */
addr_len = sizeof(struct sockaddr);
numbytes = recvfrom(recv_sockfd, recv_buf, BUF_DEPTH, 0, (struct sockaddr *)&peer_addr, &addr_len);
if (numbytes >0)
struct timespec tp;
printf("[%d.%ld]Thread_%lu_Received pkt from peer: %s\n", (unsigned int)(tp.tv_sec), tp.tv_nsec, (unsigned long int)(pthread_self()), inet_ntoa(peer_addr.sin_addr));
printf("Msg content: %s\n", show_msg_content(recv_buf, numbytes));
printf("Error when recvfrom, err_no %d\n", errno);