LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 02-19-2008, 04:44 PM   #1
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Thanked: 0
Need help with UDP sockets in c++


[Log in to get rid of this advertisement]
Hello !

I have a disturbing problem : I am writing a client/server application and the socket (file descriptor) is always the same one !! Shouldn't it be different so the apps running on the same computer can be differenciated ??
Code:
#ifdef DEBUG
this->sock = socket(PF_INET, SOCK_DGRAM, 0);

if (sock == -1)
    throw "socket";

// Set port and address...
// ...

if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)) == -1)
    throw "bind";

#ifdef DEBUG
    cout << "id du socket : " << this->sock << endl
#endif
The socket (sock) is always 3, even if I run several clients on the same computer :s
Any ideas ?

Last edited by Shautieh; 02-19-2008 at 04:47 PM..
Shautieh is offline  
Tag This Post , ,
Reply With Quote
Old 02-19-2008, 04:57 PM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 327
Thanked: 2
You should look into file descriptors
Wikipedia
Quote:
Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table.
Which means a file descriptor is only meaningful to your program. Try fopening something before
Code:
this->sock = socket(PF_INET, SOCK_DGRAM, 0);
and you'll likely see the socket becomes 4. Hope this helps. Cheers
nc3b is offline     Reply With Quote
Old 02-19-2008, 05:28 PM   #3
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Thanked: 0

Original Poster
oh, ok ! Stupid me :F
But then I guess I have a problem of port as it seems a single client can receive several messages destined to others which run on the same computer :s

I use :
Code:
 localAddr.sin_port = htons(0);
which, it seems, should let the OS choose whatever port it wants so my clients all have a different port. Is it right ? (or should I use getsockname() manually afterwards or something ?)
Shautieh is offline     Reply With Quote
Old 02-20-2008, 01:33 AM   #4
nc3b
Member
 
Registered: Aug 2005
Posts: 327
Thanked: 2
When you write a network client you don't need to bind it and, being a UDP client you don't even need to connect it. Just sendto and recvfrom with it. The server on the other hand need to bind the bind it's socket on that port and listen on it. It's all kind of complicated, it's better to work on examples
http://sage.mc.yu.edu/kbeen/teaching...s/sockets.html
http://www.beej.us/guide/bgnet/outpu....html#datagram
Beej's tutorials are considered great, you might want to look around If you don't like them, google for more documentation or buy socket programming books. Cheers
nc3b is offline     Reply With Quote
Old 02-20-2008, 03:19 PM   #5
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64
Thanked: 0

Original Poster
Thanks for the links
The whole thing sure isn't easy, and even if I got quite a good grasp of what's going on now I fail to understand how the clients can be differenciated when the server sends a datagram and the clients are running on the same computer (especially if there is no binding on a specific port as they would listen, I guess, to anything is coming up <= all the fields of the sockaddr_in structure are the same for all the clients :s).
Setting specific ports for each client might be a solution (if there was a problem to begin with, but I might be wrong) but it's quite of a pain
Shautieh is offline     Reply With Quote
Old 02-20-2008, 03:20 PM   #6
nc3b
Member
 
Registered: Aug 2005
Posts: 327
Thanked: 2
No need to set specific ports because the os should normally take care of this
nc3b is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-28-2007 07:44 PM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 08:28 PM
open/query remote udp sockets in shell macie Programming 2 03-20-2005 04:45 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 12:49 PM
sockets, UDP, using select() Pres Programming 3 08-22-2003 10:41 AM


All times are GMT -5. The time now is 09:12 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration