10 Replies - 6346 Views - Last Post: 28 January 2013 - 05:10 PM Rate Topic: -----

#1 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 08:22 AM

So I've made a server to accept clients into.
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.

Is This A Good Question/Topic? 0
  • +

Replies To: Socket Server - Accepting Multiple Clients (C++)

#2 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 09:05 AM

Multithreading is actually quite an expensive optimisation, which I would suggest considering only once you've managed to get your server working using non-blocking sockets. Expose your code to multithreaded parallelism in the places where you can see the most benefit in doing so. You can't see provable benefit without a working solution.
Was This Post Helpful? 0
  • +
  • -

#3 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 12:47 PM

View Postundefined behaviour, on 17 January 2013 - 09:05 AM, said:

Multithreading is actually quite an expensive optimisation, which I would suggest considering only once you've managed to get your server working using non-blocking sockets. Expose your code to multithreaded parallelism in the places where you can see the most benefit in doing so. You can't see provable benefit without a working solution.

Ohhhh big words =o
So sorry, I don't understand very well.
Not a big grammar person
Was This Post Helpful? 0
  • +
  • -

#4 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 04:33 PM

Accepting a client is usually non-blocking (e.g. SDL_net's accept)

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.
Was This Post Helpful? 0
  • +
  • -

#5 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 06:19 PM

View PostDjabby, on 17 January 2013 - 04:33 PM, said:

Accepting a client is usually non-blocking (e.g. SDL_net's accept)

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?
Was This Post Helpful? 0
  • +
  • -

#6 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 17 January 2013 - 08:58 PM

It was merely pseudo code. You can't expect it to function well on its own.

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.
Was This Post Helpful? 0
  • +
  • -

#7 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 18 January 2013 - 08:00 AM

View PostDjabby, on 17 January 2013 - 08:58 PM, said:

It was merely pseudo code. You can't expect it to function well on its own.

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.
Was This Post Helpful? 0
  • +
  • -

#8 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 18 January 2013 - 10:58 AM

I suggest you to first have a look at C++/STL. The STL enriches C++ with a lot of functions, such as containers like std::vector. The STL comes with every C++ compiler, so you don't have to install any additional stuff.

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?
Was This Post Helpful? 0
  • +
  • -

#9 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 22 January 2013 - 08:01 AM

View PostDjabby, on 18 January 2013 - 10:58 AM, said:

I suggest you to first have a look at C++/STL. The STL enriches C++ with a lot of functions, such as containers like std::vector. The STL comes with every C++ compiler, so you don't have to install any additional stuff.

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 

Was This Post Helpful? 0
  • +
  • -

#10 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 22 January 2013 - 08:12 AM

View PostCPPB, on 22 January 2013 - 08:01 AM, said:

View PostDjabby, on 18 January 2013 - 10:58 AM, said:

I suggest you to first have a look at C++/STL. The STL enriches C++ with a lot of functions, such as containers like std::vector. The STL comes with every C++ compiler, so you don't have to install any additional stuff.

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
Was This Post Helpful? 0
  • +
  • -

#11 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Socket Server - Accepting Multiple Clients (C++)

Posted 28 January 2013 - 05:10 PM

You're having problems that seem to indicate you aren't reading a manual, and you haven't read a book. This isn't necessarily correct, but please keep in mind that our effort is best put to use helping people who have read their books and are having problems with the exercises despite clearly searching for the answer.

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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1