For a server using c++, how do you accept multiple clients, handle X requests at a time with a backlog of X
I was wondering how I could handle multiple requests at the same time on a C server. I think I have the backlog correct (I wanted it at 20). I also am stumped on how to handle X (I want to use 10 in my case) requests at the same time. I have a working client with the server so far.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <iterator>
#include "mathParse.h"
class Mathparse;
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
MathParse tester;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %dn", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %dn", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ldn", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %dn", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, 20);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//char* str = { recvbuf };
//nt x = sizeof(str);
//std::string s(x, str);
//;
// Echo the buffer back to the sender
std::string x = MathParse::pars(recvbuf);
std::string s(recvbuf);
iSendResult = send(ClientSocket, x.c_str(), iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//std::cout << "ASDKJAHSDKJD" << s << std::endl;
printf("Bytes sentsdsdsdsdsdsdsdsdssdsdsdsd: n", x);
//system("PAUSE");
}
else if (iResult == 0)
printf("Connection closing...n");
else {
printf("recv failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
c++ server
add a comment |
I was wondering how I could handle multiple requests at the same time on a C server. I think I have the backlog correct (I wanted it at 20). I also am stumped on how to handle X (I want to use 10 in my case) requests at the same time. I have a working client with the server so far.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <iterator>
#include "mathParse.h"
class Mathparse;
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
MathParse tester;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %dn", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %dn", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ldn", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %dn", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, 20);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//char* str = { recvbuf };
//nt x = sizeof(str);
//std::string s(x, str);
//;
// Echo the buffer back to the sender
std::string x = MathParse::pars(recvbuf);
std::string s(recvbuf);
iSendResult = send(ClientSocket, x.c_str(), iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//std::cout << "ASDKJAHSDKJD" << s << std::endl;
printf("Bytes sentsdsdsdsdsdsdsdsdssdsdsdsd: n", x);
//system("PAUSE");
}
else if (iResult == 0)
printf("Connection closing...n");
else {
printf("recv failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
c++ server
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
1
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input toMathParse::pars(recvbuf);
could be anywhere between 1 andrecvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly,send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's inx
to be sure) the receiver with any way to tell one message from the next.
– user4581301
Nov 19 '18 at 23:18
1
You would need to either have multiple threads or use aselect
-based event loop.
– n.m.
Nov 19 '18 at 23:19
1
On topic, normally I would use IO multiplexing (select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.
– user4581301
Nov 19 '18 at 23:25
add a comment |
I was wondering how I could handle multiple requests at the same time on a C server. I think I have the backlog correct (I wanted it at 20). I also am stumped on how to handle X (I want to use 10 in my case) requests at the same time. I have a working client with the server so far.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <iterator>
#include "mathParse.h"
class Mathparse;
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
MathParse tester;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %dn", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %dn", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ldn", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %dn", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, 20);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//char* str = { recvbuf };
//nt x = sizeof(str);
//std::string s(x, str);
//;
// Echo the buffer back to the sender
std::string x = MathParse::pars(recvbuf);
std::string s(recvbuf);
iSendResult = send(ClientSocket, x.c_str(), iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//std::cout << "ASDKJAHSDKJD" << s << std::endl;
printf("Bytes sentsdsdsdsdsdsdsdsdssdsdsdsd: n", x);
//system("PAUSE");
}
else if (iResult == 0)
printf("Connection closing...n");
else {
printf("recv failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
c++ server
I was wondering how I could handle multiple requests at the same time on a C server. I think I have the backlog correct (I wanted it at 20). I also am stumped on how to handle X (I want to use 10 in my case) requests at the same time. I have a working client with the server so far.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <iterator>
#include "mathParse.h"
class Mathparse;
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
MathParse tester;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %dn", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %dn", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ldn", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %dn", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, 20);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %dn", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//char* str = { recvbuf };
//nt x = sizeof(str);
//std::string s(x, str);
//;
// Echo the buffer back to the sender
std::string x = MathParse::pars(recvbuf);
std::string s(recvbuf);
iSendResult = send(ClientSocket, x.c_str(), iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//std::cout << "ASDKJAHSDKJD" << s << std::endl;
printf("Bytes sentsdsdsdsdsdsdsdsdssdsdsdsd: n", x);
//system("PAUSE");
}
else if (iResult == 0)
printf("Connection closing...n");
else {
printf("recv failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %dn", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
c++ server
c++ server
edited Nov 19 '18 at 23:12


Galik
33.6k34876
33.6k34876
asked Nov 19 '18 at 22:49


Glenville PecorGlenville Pecor
12
12
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
1
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input toMathParse::pars(recvbuf);
could be anywhere between 1 andrecvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly,send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's inx
to be sure) the receiver with any way to tell one message from the next.
– user4581301
Nov 19 '18 at 23:18
1
You would need to either have multiple threads or use aselect
-based event loop.
– n.m.
Nov 19 '18 at 23:19
1
On topic, normally I would use IO multiplexing (select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.
– user4581301
Nov 19 '18 at 23:25
add a comment |
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
1
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input toMathParse::pars(recvbuf);
could be anywhere between 1 andrecvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly,send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's inx
to be sure) the receiver with any way to tell one message from the next.
– user4581301
Nov 19 '18 at 23:18
1
You would need to either have multiple threads or use aselect
-based event loop.
– n.m.
Nov 19 '18 at 23:19
1
On topic, normally I would use IO multiplexing (select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.
– user4581301
Nov 19 '18 at 23:25
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
1
1
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input to MathParse::pars(recvbuf);
could be anywhere between 1 and recvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly, send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's in x
to be sure) the receiver with any way to tell one message from the next.– user4581301
Nov 19 '18 at 23:18
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input to MathParse::pars(recvbuf);
could be anywhere between 1 and recvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly, send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's in x
to be sure) the receiver with any way to tell one message from the next.– user4581301
Nov 19 '18 at 23:18
1
1
You would need to either have multiple threads or use a
select
-based event loop.– n.m.
Nov 19 '18 at 23:19
You would need to either have multiple threads or use a
select
-based event loop.– n.m.
Nov 19 '18 at 23:19
1
1
On topic, normally I would use IO multiplexing (
select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.– user4581301
Nov 19 '18 at 23:25
On topic, normally I would use IO multiplexing (
select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.– user4581301
Nov 19 '18 at 23:25
add a comment |
0
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%2f53383771%2ffor-a-server-using-c-how-do-you-accept-multiple-clients-handle-x-requests-at%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
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.
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%2f53383771%2ffor-a-server-using-c-how-do-you-accept-multiple-clients-handle-x-requests-at%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
@NathanOliver and PasserBy ty! sorry I mixed them up because I was using printf with this.
– Glenville Pecor
Nov 19 '18 at 22:58
1
Unrelated: Remember that TCP is a stream-based and not a message-based protocol.
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
doesn't give any guarantees that you will receive one-and-only-one message. The input toMathParse::pars(recvbuf);
could be anywhere between 1 andrecvbuflen
bytes. How many bytes has not been provided, so the function is almost certainly doomed. Similarly,send(ClientSocket, x.c_str(), iResult, 0);
may not be providing (have to see what's inx
to be sure) the receiver with any way to tell one message from the next.– user4581301
Nov 19 '18 at 23:18
1
You would need to either have multiple threads or use a
select
-based event loop.– n.m.
Nov 19 '18 at 23:19
1
On topic, normally I would use IO multiplexing (
select
(epoll
if it's available) in a POSIX OS or Overlapped IO on Windows) to manage multiple simultaneous connections. Link this up with a thread pool if the transactions could be time-consuming and cause unreasonable delays in other transactions.– user4581301
Nov 19 '18 at 23:25