Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-30-2004, 12:53 PM   #1
LQ Newbie
Registered: Feb 2003
Location: Hillsboro, OR
Posts: 13

Rep: Reputation: 0
Socket programming question


I am trying to receive a UDP packet from a socket and determine the IP address it came from. Is this possible?

I am currently able to receive the packet using a bind to any IP address with a specific port.

However, the recv call only returns the number of bytes read with no information about the incoming packet (other than the actual data).

Thanks in advance.

Old 12-30-2004, 01:06 PM   #2
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
For UDP packets, you should be using recvfrom instead of recv. That will get you an address that you are receiving from.
Old 12-30-2004, 01:36 PM   #3
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
Yes, it's possible to determine the IP address it came from. The IP address is stored in a sockaddr_in struct type whose address you have supplied to recvfrom as a pointer. The IP is in its sin_addr field.
The following code will print the foreign IP address:
printf("%s", inet_ntoa(client.sin_addr)); /* which client is your sockaddr_in struct */
I hope it helps

Last edited by Nerox; 12-30-2004 at 01:37 PM.


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
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 04:50 PM
Socket Programming Question? sharma_deepak83 Programming 1 06-29-2004 10:36 AM
C/ C++ socket programming question (proxy servers) juby Programming 4 09-30-2003 01:34 PM
UDP socket programming question. niac Programming 6 03-02-2003 02:23 PM
socket programming question.. concoran Programming 4 01-14-2002 02:23 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration