ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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 ?
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
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 ?)
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.