Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 02-12-2010, 05:00 AM   #1
LQ Newbie
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
received UDP packet length

How to get the length of received UDP packet? Using wireshark I could see the correct length of datagram. How can I print this value in my simple udp server program? I am receiving binary data (Non printable ascii characters as a data) So I can not use strlen(buf) which throws incorrect length.

          if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
                error = ioctl(s, FIONREAD, &value);
                printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
udp packet length is always '0' from the above code. Any comments?

I also tried like below

     if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
                unsigned short iphdrlen;
                struct iphdr* iph = (struct iphdr*)buf;
                iphdrlen =iph->ihl*4;

                printf("IP version :%d\n", ((unsigned int)((iph->version))));
                printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
                printf("total len .. %d\n", (ntohs(iph->tot_len)));

The above code always returns me wrong values from ip header? Any comments?
Old 02-12-2010, 07:57 AM   #2
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Originally Posted by sasubillis View Post
How to get the length of received UDP packet?
From the man page for "recvfrom":
       These calls return the number of bytes received, or -1 if an
       error occurred. The return value will be 0 when the peer has
       performed an orderly shutdown.
In other words, the value in "ret" will be "the length of received UDP packet".


length, udp

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
packet queue length question vlyamtse Linux - Networking 4 10-08-2009 12:29 AM
trivia: extracting length from UDP packets thelordmule Programming 2 01-23-2007 10:46 PM
how to get source physical address of received udp datagrams? lyeoh Linux - Networking 0 05-31-2006 05:35 AM
problem in received packet linux_lover2005 Programming 2 04-24-2005 08:33 PM
How to read udp datagram length? sceadu Programming 1 01-05-2005 08:22 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration