C++ Move client out of the server loop and onto the next function
Good evening, everyone.
Ok, I have been searching and searching the web for the last 2 days and haven't found any solution. So I decided to finally ask one here. Forgive me, maybe the reason I cannot find my solution is because I don't know how to phrase it correctly. I will try my best here.
I am trying to have a client soon as he connects to my server to be pushed off into lets say the "login" function. While keeping the server in its loop waiting for a new connection to arrive, and then forward them off to the next function as well. The only thing I can think of is multi-threading. I have it working on my client side program to open a window and keep communication with the server. Here is my code:
So this is how I have my header setup. I am not having any compile errors. Only an exception when the program gets to my transfer function.
#include "pch.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <sstream>
//////////////////////////SERVER MULTIPLE CLIENTS/////////////////////////
#pragma comment (lib, "ws2_32.lib")
///Function for transferring clients
void sockTransfer();
using namespace std;
class SERVER
This is how I have my loop setup to keep receiving connections. And at the bottom you will see me call out my function for sending clients to attempt to put them onto a separate thread.
while (true)
{
//Copy the master port//?? What reason do you have to copy the port?
fd_set copy = master;
//Checks the ammount of connected clients(Open Sockets)
int socketCount = select(0, ©, nullptr, nullptr, nullptr);
// While there is a connection to the server(Open Sockets) run this
loop
for (int i = 0; i < socketCount; i++)
{
SOCKET sock = copy.fd_array[i];
if (sock == listening)
{
//accept the new connection
SOCKET client = accept(listening, nullptr, nullptr);
cout << "Adding client socket to master Arrary...";
//Add the new connection to the list of clients connected to
//&master(array)
FD_SET(client, &master);
cout << "done" << endl;
//Send a welcome message to the client
send(client, bannarMsg.c_str(), bannarMsg.size() + 1, 0);
cout << client << "has connected to socket: " << sock << endl;
///////////////////////////////////TESTING/////////////////
sockTransfer();
the sockTransfer() callout sends to here with the attempt to adding it to a new thread.
so I had to separate the functions because the WinSock2.h, WS2tcpip.h headers conflict with the boost/asio.hpp header.
void sockTransfer()
{
clientMultithreading();
}
then finally arriving to my threading function. here...
void clientMultithreading()
{
thread tnew(login);
if (1 == 0)
{
tnew.join();
}
}
My hope was that once the new socket was created it would bounce back to where it left off in the listening loop.
I know this is a very poor setup of code. with little error checking. I was going to clean it up and modify it once I can get it to work properly.
I know it is a lengthy question. I tried to cut out as much code unnecessary to this question. I thank you for your time in looking this over. Hope this is clear enough of a question.
c++ multithreading server-side
add a comment |
Good evening, everyone.
Ok, I have been searching and searching the web for the last 2 days and haven't found any solution. So I decided to finally ask one here. Forgive me, maybe the reason I cannot find my solution is because I don't know how to phrase it correctly. I will try my best here.
I am trying to have a client soon as he connects to my server to be pushed off into lets say the "login" function. While keeping the server in its loop waiting for a new connection to arrive, and then forward them off to the next function as well. The only thing I can think of is multi-threading. I have it working on my client side program to open a window and keep communication with the server. Here is my code:
So this is how I have my header setup. I am not having any compile errors. Only an exception when the program gets to my transfer function.
#include "pch.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <sstream>
//////////////////////////SERVER MULTIPLE CLIENTS/////////////////////////
#pragma comment (lib, "ws2_32.lib")
///Function for transferring clients
void sockTransfer();
using namespace std;
class SERVER
This is how I have my loop setup to keep receiving connections. And at the bottom you will see me call out my function for sending clients to attempt to put them onto a separate thread.
while (true)
{
//Copy the master port//?? What reason do you have to copy the port?
fd_set copy = master;
//Checks the ammount of connected clients(Open Sockets)
int socketCount = select(0, ©, nullptr, nullptr, nullptr);
// While there is a connection to the server(Open Sockets) run this
loop
for (int i = 0; i < socketCount; i++)
{
SOCKET sock = copy.fd_array[i];
if (sock == listening)
{
//accept the new connection
SOCKET client = accept(listening, nullptr, nullptr);
cout << "Adding client socket to master Arrary...";
//Add the new connection to the list of clients connected to
//&master(array)
FD_SET(client, &master);
cout << "done" << endl;
//Send a welcome message to the client
send(client, bannarMsg.c_str(), bannarMsg.size() + 1, 0);
cout << client << "has connected to socket: " << sock << endl;
///////////////////////////////////TESTING/////////////////
sockTransfer();
the sockTransfer() callout sends to here with the attempt to adding it to a new thread.
so I had to separate the functions because the WinSock2.h, WS2tcpip.h headers conflict with the boost/asio.hpp header.
void sockTransfer()
{
clientMultithreading();
}
then finally arriving to my threading function. here...
void clientMultithreading()
{
thread tnew(login);
if (1 == 0)
{
tnew.join();
}
}
My hope was that once the new socket was created it would bounce back to where it left off in the listening loop.
I know this is a very poor setup of code. with little error checking. I was going to clean it up and modify it once I can get it to work properly.
I know it is a lengthy question. I tried to cut out as much code unnecessary to this question. I thank you for your time in looking this over. Hope this is clear enough of a question.
c++ multithreading server-side
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34
add a comment |
Good evening, everyone.
Ok, I have been searching and searching the web for the last 2 days and haven't found any solution. So I decided to finally ask one here. Forgive me, maybe the reason I cannot find my solution is because I don't know how to phrase it correctly. I will try my best here.
I am trying to have a client soon as he connects to my server to be pushed off into lets say the "login" function. While keeping the server in its loop waiting for a new connection to arrive, and then forward them off to the next function as well. The only thing I can think of is multi-threading. I have it working on my client side program to open a window and keep communication with the server. Here is my code:
So this is how I have my header setup. I am not having any compile errors. Only an exception when the program gets to my transfer function.
#include "pch.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <sstream>
//////////////////////////SERVER MULTIPLE CLIENTS/////////////////////////
#pragma comment (lib, "ws2_32.lib")
///Function for transferring clients
void sockTransfer();
using namespace std;
class SERVER
This is how I have my loop setup to keep receiving connections. And at the bottom you will see me call out my function for sending clients to attempt to put them onto a separate thread.
while (true)
{
//Copy the master port//?? What reason do you have to copy the port?
fd_set copy = master;
//Checks the ammount of connected clients(Open Sockets)
int socketCount = select(0, ©, nullptr, nullptr, nullptr);
// While there is a connection to the server(Open Sockets) run this
loop
for (int i = 0; i < socketCount; i++)
{
SOCKET sock = copy.fd_array[i];
if (sock == listening)
{
//accept the new connection
SOCKET client = accept(listening, nullptr, nullptr);
cout << "Adding client socket to master Arrary...";
//Add the new connection to the list of clients connected to
//&master(array)
FD_SET(client, &master);
cout << "done" << endl;
//Send a welcome message to the client
send(client, bannarMsg.c_str(), bannarMsg.size() + 1, 0);
cout << client << "has connected to socket: " << sock << endl;
///////////////////////////////////TESTING/////////////////
sockTransfer();
the sockTransfer() callout sends to here with the attempt to adding it to a new thread.
so I had to separate the functions because the WinSock2.h, WS2tcpip.h headers conflict with the boost/asio.hpp header.
void sockTransfer()
{
clientMultithreading();
}
then finally arriving to my threading function. here...
void clientMultithreading()
{
thread tnew(login);
if (1 == 0)
{
tnew.join();
}
}
My hope was that once the new socket was created it would bounce back to where it left off in the listening loop.
I know this is a very poor setup of code. with little error checking. I was going to clean it up and modify it once I can get it to work properly.
I know it is a lengthy question. I tried to cut out as much code unnecessary to this question. I thank you for your time in looking this over. Hope this is clear enough of a question.
c++ multithreading server-side
Good evening, everyone.
Ok, I have been searching and searching the web for the last 2 days and haven't found any solution. So I decided to finally ask one here. Forgive me, maybe the reason I cannot find my solution is because I don't know how to phrase it correctly. I will try my best here.
I am trying to have a client soon as he connects to my server to be pushed off into lets say the "login" function. While keeping the server in its loop waiting for a new connection to arrive, and then forward them off to the next function as well. The only thing I can think of is multi-threading. I have it working on my client side program to open a window and keep communication with the server. Here is my code:
So this is how I have my header setup. I am not having any compile errors. Only an exception when the program gets to my transfer function.
#include "pch.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <sstream>
//////////////////////////SERVER MULTIPLE CLIENTS/////////////////////////
#pragma comment (lib, "ws2_32.lib")
///Function for transferring clients
void sockTransfer();
using namespace std;
class SERVER
This is how I have my loop setup to keep receiving connections. And at the bottom you will see me call out my function for sending clients to attempt to put them onto a separate thread.
while (true)
{
//Copy the master port//?? What reason do you have to copy the port?
fd_set copy = master;
//Checks the ammount of connected clients(Open Sockets)
int socketCount = select(0, ©, nullptr, nullptr, nullptr);
// While there is a connection to the server(Open Sockets) run this
loop
for (int i = 0; i < socketCount; i++)
{
SOCKET sock = copy.fd_array[i];
if (sock == listening)
{
//accept the new connection
SOCKET client = accept(listening, nullptr, nullptr);
cout << "Adding client socket to master Arrary...";
//Add the new connection to the list of clients connected to
//&master(array)
FD_SET(client, &master);
cout << "done" << endl;
//Send a welcome message to the client
send(client, bannarMsg.c_str(), bannarMsg.size() + 1, 0);
cout << client << "has connected to socket: " << sock << endl;
///////////////////////////////////TESTING/////////////////
sockTransfer();
the sockTransfer() callout sends to here with the attempt to adding it to a new thread.
so I had to separate the functions because the WinSock2.h, WS2tcpip.h headers conflict with the boost/asio.hpp header.
void sockTransfer()
{
clientMultithreading();
}
then finally arriving to my threading function. here...
void clientMultithreading()
{
thread tnew(login);
if (1 == 0)
{
tnew.join();
}
}
My hope was that once the new socket was created it would bounce back to where it left off in the listening loop.
I know this is a very poor setup of code. with little error checking. I was going to clean it up and modify it once I can get it to work properly.
I know it is a lengthy question. I tried to cut out as much code unnecessary to this question. I thank you for your time in looking this over. Hope this is clear enough of a question.
c++ multithreading server-side
c++ multithreading server-side
asked Nov 23 at 10:09
Josiah Safley
1
1
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34
add a comment |
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444615%2fc-move-client-out-of-the-server-loop-and-onto-the-next-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444615%2fc-move-client-out-of-the-server-loop-and-onto-the-next-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am going to try writing a separate class that checks the amount of open sockets and puts them in a thread. See if that works.
– Josiah Safley
Nov 23 at 10:34