Socket Server - Accepting Multiple Clients (C++)
Page 1 of 110 Replies - 2012 Views - Last Post: 28 January 2013 - 05:10 PM
#1
Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 08:22 AM
By doing so, I've noticed this works.
However, only one client is accepted.
I've read online, and decided I needed help.
Multithreading honestly confused me,
And I've saw posts on a continuous accept loop.
Upon this, it still doesn't work.
Would anyone like to help me on this topic?
Again, I'm trying to get it so I can open one server, and multiple people are able to join that one server.
Replies To: Socket Server - Accepting Multiple Clients (C++)
#2
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 09:05 AM
#3
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 12:47 PM
undefined behaviour, on 17 January 2013 - 09:05 AM, said:
Ohhhh big words =o
So sorry, I don't understand very well.
Not a big grammar person
#4
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 04:33 PM
Non-blocking in this case means that the server will either accept a new client or continue, but it will NOT wait for a new client.
So you can accept multiple clients, without multithreading:
// Pseudocode while(new_socket = accept()) sockets.push_back(new_socket);
Writing to clients is non-blocking as well.
You only have to be careful with reading from clients. Reading is usually a blocking function (e.g. SDL_net's recv). But there are ways to determine if there is data available on the socket, before blocking it by reading (SocketReady).
You should have a look which of these functions are blocking and non-blocking in your network library.
#5
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 06:19 PM
Djabby, on 17 January 2013 - 04:33 PM, said:
Non-blocking in this case means that the server will either accept a new client or continue, but it will NOT wait for a new client.
So you can accept multiple clients, without multithreading:
// Pseudocode while(new_socket = accept()) sockets.push_back(new_socket);
Writing to clients is non-blocking as well.
You only have to be careful with reading from clients. Reading is usually a blocking function (e.g. SDL_net's recv). But there are ways to determine if there is data available on the socket, before blocking it by reading (SocketReady).
You should have a look which of these functions are blocking and non-blocking in your network library.
Alright awesome =D
i tried this, however, i get the error "sockets undeclared."
Am I forgetting to add something/An included file?
#6
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 17 January 2013 - 08:58 PM
I assumed that you have a container for your sockets. If 'socket' is your type for a socket, you would have something as follows:
// Create a container for all client sockets std::vector<socket> sockets; ... // If accept failed, it returns null, which interrupts our loop. // Otherwise it returns the new accepted socket which we assign to new_socket while(socket new_socket = accept()) // We store our new socket in our container sockets.push_back(new_socket);
I hope this is more clear.
#7
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 18 January 2013 - 08:00 AM
Djabby, on 17 January 2013 - 08:58 PM, said:
I assumed that you have a container for your sockets. If 'socket' is your type for a socket, you would have something as follows:
// Create a container for all client sockets std::vector<socket> sockets; ... // If accept failed, it returns null, which interrupts our loop. // Otherwise it returns the new accepted socket which we assign to new_socket while(socket new_socket = accept()) // We store our new socket in our container sockets.push_back(new_socket);
I hope this is more clear.
No=( I'm sorry.
std::vector does not work.
Also, I do not have a container for my client sockets.
Is their maybe another way?
I'm obviously having some problems.
What if I put my client and server sockets in a forever loop?
Would that solve anything?
I tried it on the server, but there was no change.
So I'm not sure what else I could do.
#8
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 18 January 2013 - 10:58 AM
You can find a complete reference about STL here.
A tutorial on how to use std::vector you can find here.
Once you understand what containers are and if you feel comfortable to use them, we can proceed: Your server shall know about all clients. This is done by storing a socket for each client in a container.
By the way, what kind of server are you planning to write?
#9
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 22 January 2013 - 08:01 AM
Djabby, on 18 January 2013 - 10:58 AM, said:
You can find a complete reference about STL here.
A tutorial on how to use std::vector you can find here.
Once you understand what containers are and if you feel comfortable to use them, we can proceed: Your server shall know about all clients. This is done by storing a socket for each client in a container.
By the way, what kind of server are you planning to write?
I see, thanks.
I got the basic understanding of the function of Vectors,
But the socket part isn't working.
I included "vector" and it got std::vector to work, but when I do:
std::vector<socket> sockets, it doesn't work and I get this error:
35 C:\Users\||||||||\Desktop\C++ Files - TEST\Blaze2D-Server.cpp type/value mismatch at argument 1 in template parameter list for `template<class _Tp, class _Alloc> class std::vector' 35 C:\Users\Treyten\Desktop\C++ Files - TEST\Blaze2D-Server.cpp expected a type, got `socket' 35 C:\Users\Treyten\Desktop\C++ Files - TEST\Blaze2D-Server.cpp template argument 2 is invalid
#10
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 22 January 2013 - 08:12 AM
CPPB, on 22 January 2013 - 08:01 AM, said:
Djabby, on 18 January 2013 - 10:58 AM, said:
You can find a complete reference about STL here.
A tutorial on how to use std::vector you can find here.
Once you understand what containers are and if you feel comfortable to use them, we can proceed: Your server shall know about all clients. This is done by storing a socket for each client in a container.
By the way, what kind of server are you planning to write?
I see, thanks.
I got the basic understanding of the function of Vectors,
But the socket part isn't working.
I included "vector" and it got std::vector to work, but when I do:
std::vector<socket> sockets, it doesn't work and I get this error:
35 C:\Users\||||||||\Desktop\C++ Files - TEST\Blaze2D-Server.cpp type/value mismatch at argument 1 in template parameter list for `template<class _Tp, class _Alloc> class std::vector' 35 C:\Users\Treyten\Desktop\C++ Files - TEST\Blaze2D-Server.cpp expected a type, got `socket' 35 C:\Users\Treyten\Desktop\C++ Files - TEST\Blaze2D-Server.cpp template argument 2 is invalid
Thats embarrasing xD
tried to take my name out of the error, but i completely forgot to on the last two.
oh well, really not a big deal xD
#11
Re: Socket Server - Accepting Multiple Clients (C++)
Posted 28 January 2013 - 05:10 PM
This is to avoid writing redundant manuals and books, because the information that is out there is good enough for you to learn from. There is no need to duplicate it. Which book are you reading? What does your book say about "types"? Does it describe how you might find out the type of "socket"? If your book doesn't give you the answer to these questions, then it's not a very good book; Get a new one.
As a prerequisite to reading manuals and books, I assume that you have the ability to read. If you can read and you're interested in learning C++, then it would make sense that you'll enjoy a book about C++. I don't want to treat you like a baby... Please don't ask me to. If you wouldn't be interested in a book about C++, then you wouldn't be interested in C++; Go and find something you find interesting to do with your time.
|
|

New Topic/Question
Reply



MultiQuote



|