LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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!

Notices


Reply
  Search this Thread
Old 08-23-2011, 12:06 AM   #1
yethish
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Rep: Reputation: 0
Retrieve client ip address and port number in kernel udp socket


I am writing a simple client server udp program.

The server runs at the kernel level and client runs at
application level.

The client sends the request packet to server through
"sendto(server_sock, buf, BUFLEN, 0,
(struct sockaddr *)&server_addr, addr_len);"
function and the server at the kernel level receives the
message through "sock_recvmsg(server_socket, &msg, size, flags);".

my doubt is how to retrieve the client IP address and port number
at server side.
 
Old 09-06-2011, 09:38 PM   #2
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
Hi Yethish,

I'm sorry I didn't notice your question has not been answered yet. If you still need it, here is a suggestion:

Code:
#include <sys/socket.h>
#include <netdb.h>

...

  struct sockaddr_storage peer_addr;
  socklen_t peer_addr_len = sizeof(struct sockaddr_storage); 
  char host[NI_MAXHOST], service[NI_MAXSERV];

...
  nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);

...

  rc = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);

...

  printf("Received %d bytes from %s:%s\n", nread, host, service);
I hope that helps.
 
Old 09-08-2011, 09:23 AM   #3
yethish
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Arrow Retrieve client ip address and port number in kernel udp socket

Hi tBrand,

Thanks for the response.

The code suggested by you is for kernel level or
application level ?

Because at kernel level we use sock_recvmsg() call
to receive the data, but in your code there is
recvfrom() call to receive the data.

I am looking for the solution at kernel level.

Regards
Yethish
 
Old 09-08-2011, 08:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
For a qn like this, I'd recommend asking the Mods via the Report button to move this to the programming forum.
You may also find this useful http://beej.us/guide/bgnet/. Not sure if it does the kernel level version.
 
Old 09-08-2011, 09:59 PM   #5
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
Sorry, Yethish, I didn't read your post carefully enough. I'll do a little experimenting and see if I can find something. I would like to know the answer myself.
 
  


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
tcp client socket : how to get own port number ?? vlyamtse Programming 2 10-01-2010 07:26 PM
what is the maximum number of bytes is possible to send from a udp socket? boidi Linux - Software 3 10-01-2010 04:57 PM
Could not listen on UDP socket: address is in use. Marv Bon *BSD 10 08-29-2007 12:50 PM
Could not listen on UDP socket: address is in use. Marv Bon *BSD 1 08-26-2007 01:11 PM
could not listen on UDP socket: address in use peaceofcrap2001 Linux - Networking 2 07-10-2004 12:18 PM


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

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