LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 02-19-2008, 03:44 PM   #1
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
Need help with UDP sockets in c++


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 03:47 PM.
 
Old 02-19-2008, 03:57 PM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
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
 
Old 02-19-2008, 04:28 PM   #3
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Original Poster
Rep: Reputation: 15
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 ?)
 
Old 02-20-2008, 12:33 AM   #4
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
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
 
Old 02-20-2008, 02:19 PM   #5
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 02-20-2008, 02:20 PM   #6
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
No need to set specific ports because the os should normally take care of this
 
  


Reply

Tags
c++, socket, 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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 06:44 PM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 07:28 PM
open/query remote udp sockets in shell macie Programming 2 03-20-2005 03:45 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 11:49 AM
sockets, UDP, using select() Pres Programming 3 08-22-2003 09:41 AM


All times are GMT -5. The time now is 09:43 AM.

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
Open Source Consulting | Domain Registration