LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-18-2006, 09:52 PM   #1
socialjazz
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Rep: Reputation: 0
How to specify source port when sending UDP packet


I want to send UDP packet from a specify source port
how do i do that? When i try to send a UDP packet, the system will ususally find a source port for me.

my present code is:


struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN] = {0xa,0xd};

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
printf("socket() failed\n");

memset((char *) &si_other, sizeof(si_other), 0);
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
printf("inet_aton() failed\n");
}

if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
printf("sendto() failed\n");
 
Old 09-18-2006, 10:58 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
How do you mean, usually find a source port

http://www.cs.utah.edu/~swalton/list...kets/programs/ The coonection-less sender and connectionless-receiver in chapter 4 give examples of UDP programming.
 
Old 09-18-2006, 11:43 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Its done with bind(), it takes a struct sockaddr * where you can set the local interface address (if there is more than 1) and local port number. Binding is normally done immediately after calling socket().
 
Old 09-19-2006, 01:56 AM   #4
socialjazz
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by randyding
Its done with bind(), it takes a struct sockaddr * where you can set the local interface address (if there is more than 1) and local port number. Binding is normally done immediately after calling socket().
Thanks for replying.
What should is use for the sin_addr.s_addr address?
my code does not allow me to send packet thru udp anymore if i add the following:

memset((char *) &si_me, sizeof(si_me), 0);
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);

si_me.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(s, &si_me, sizeof(si_me))==-1)
printf("bind() failed\n");
 
Old 09-19-2006, 08:15 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This is what I do and it works.
Code:
struct sockaddr_in sin;

if (-1==(sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))) return 0;
memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(fromport);
sin.sin_addr.s_addr=INADDR_ANY;
if (-1==bind(sock,(struct sockaddr *)&sin,sizeof(struct sockaddr_in))) return 0;
Edit: I just noticed your memset() arguments are ordered wrong.

Last edited by randyding; 09-19-2006 at 08:16 PM.
 
  


Reply


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
How to reliably detect UDP packet loss. rikusg Linux - Networking 2 07-14-2006 03:50 AM
How to read UDP packet payload ? sceadu Programming 2 02-07-2006 09:00 PM
How To Stop a UDP Packet Flood ! murder Linux - Newbie 2 09-19-2005 10:14 AM
UDP packet oss dazdaz Linux - Networking 2 04-05-2004 01:30 PM
UDP: short packet markus1982 Linux - Security 4 08-12-2003 08:03 AM


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

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