Multiple clients in c++ server - c++

Yesterday I started writing a code for server-client TCP/IP for chating.
It's code for server:
#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSAData wsa;
WORD Version = MAKEWORD(2, 1);
WSAStartup(Version, &wsa);
SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN Server;
Server.sin_addr.s_addr = inet_addr("127.0.0.1");
Server.sin_family = AF_INET;
Server.sin_port = htons(100);
bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
listen(Listen, 1);
int size = sizeof(Server);
char buffer[512];
char *fn = "";
int iResult;
std::cout <<"Listening...";
if(Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {
std::cout << "\nConnecting was reached :)";
}
do {
char *fn;
iResult = recv(Connect, buffer, sizeof(buffer), 0);
if (iResult > 0) {
buffer[iResult] = '\0';
fn = buffer;
std::cout << fn;
} else if (iResult == 0){
printf("Connection closing...\n");
iResult = 0;
}
} while (iResult > 0);
WSACleanup();
std::cin.get();
return 0;
}
And it's for client:
#include <iostream>
#include <winsock2.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSAData wsa;
WORD Version = MAKEWORD(2, 1);
WSAStartup(Version, &wsa);
int iResult;
std::string text;
char buffer[512];
SOCKADDR_IN Client;
Client.sin_addr.s_addr = inet_addr("127.0.0.1");
Client.sin_family = AF_INET;
Client.sin_port = htons(100);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);
std::cout << "Wcisnij enter aby polaczyc";
std::cin.get();
if(connect(Connect, (SOCKADDR*)&Client, sizeof(Client))) {
std::cout << "Nawiazano polaczeniee";
}
do {
std::cout << "Waxer: ";
std::cin >> text;
text.insert( 0, "Waxer: " );
strcpy_s(buffer, text.c_str());
// Send an initial buffer
iResult = send(Connect, buffer, strlen(buffer), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Connect);
WSACleanup();
return 1;
}
} while(text != "ban");
std::cout<< "Polaczenie wyslane!";
std::cin.get();
return 0;
}
My question is? What should I change in this code for multiple clients. I mean how to connect new clients?

Your question is quite open, but in general you need threads.
Pseudocode:
int main()
{
std::vector<std::thread> clients;
Socket listener("localhost", "1337");
for (;;)
{
Socket tmp = listener.accept();
clients.push_back(std::thread(&myHandlerFunction, tmp));
clients.back().detach();
}
}
In the end you have a thread for every connected client and can codify your chat logic so that they can communicate with each other.

Related

UDP client can not recieve data from server in C++

I am new for C++. I tried to write a UDP client and server in C++. The server can receive data from client. But the client can not receive data from server. No matter what data the server sends, the method recvfrom in client always returns 0.
The client code is below:
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define BUFFER_SIZE 1024
int main()
{
SOCKET sock_Client;
WSADATA WSAData;
char receBuf[BUFFER_SIZE];
char sendBuf[BUFFER_SIZE];
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
{
printf("initialation fails!");
return -1;
}
sock_Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN addr_server;
addr_server.sin_family = AF_INET;
addr_server.sin_port = htons(4567);
addr_server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
SOCKADDR_IN sock;
int len = sizeof(sock);
while (true)
{
cout << "input data to send:";
cin >> sendBuf;
sendto(sock_Client, sendBuf, strlen(sendBuf), 0, (SOCKADDR*)&addr_server, sizeof(SOCKADDR));
//int last=recv(sock_Client, receBuf, strlen(receBuf), 0); //
int last = recvfrom(sock_Client, receBuf, strlen(receBuf), 0, (SOCKADDR*)&sock, &len);
if (last > 0)
{
receBuf[last] = '\0';
if (strcmp(receBuf, "bye") == 0)
{
cout << "server stops talking..." << endl;
closesocket(sock_Client);
break;
}
else
{
printf("received data:%s\n", receBuf);
}
}
}
closesocket(sock_Client);
WSACleanup();
return 0;
}
The server code:
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define BUFFER_SIZE 1024
int main()
{
WSADATA WSAData;
char receBuf[BUFFER_SIZE];
char Response[BUFFER_SIZE];
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
{
printf("initialation fails!");
exit(1);
}
SOCKET sockServer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockServer == INVALID_SOCKET)
{
printf("Failed socket() \n");
return 0;
}
SOCKADDR_IN addr_Server;
addr_Server.sin_family = AF_INET;
addr_Server.sin_port = htons(4567);
addr_Server.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(sockServer, (SOCKADDR*)&addr_Server, sizeof(addr_Server)) == SOCKET_ERROR)
{
printf("Failed socket() %d \n", WSAGetLastError());
return 0;
}
SOCKADDR_IN addr_Clt;
int fromlen = sizeof(SOCKADDR);
while (true)
{
int last = recvfrom(sockServer, receBuf, 1024, 0, (SOCKADDR*)&addr_Clt, &fromlen);
if (last > 0)
{
receBuf[last] = '\0';
if (strcmp(receBuf, "bye") == 0)
{
cout << " client stop talking..." << endl;
closesocket(sockServer);
return 0;
}
else
{
printf("received data(%s):%s\n", inet_ntoa(addr_Clt.sin_addr), receBuf);
}
}
cout << "anser client:";//<br>Response = “”;
cin >> Response;
sendto(sockServer, Response, strlen(Response), 0, (SOCKADDR*)&addr_Clt, sizeof(SOCKADDR));
}
closesocket(sockServer);
WSACleanup();
return 0;
}
Anyone know what is wrong with the code?
The code runs in Windows. So, set(CMAKE_CXX_STANDARD 14) has to be added into CMakeLists.txt. If you want to run the code, please notice that.

Sockets between networks in c++

I’m a noob with sockets, and I’ve tried to make a program. When it is connected in my local address it works ok, but the problem comes when I try to use it between networks, and they don’t connect. I’ve tried changing the IP from local to public and a lot of combinations of IPs. Here is the code of the program:
SERVER
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <iostream>
#include <cstdio>
#pragma warning(disable: 4996)
int main(int argc, char* argv[]) {
//WSAStartup
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2, 1);
if (WSAStartup(DLLVersion, &wsaData) != 0) {
std::cout << "Error" << std::endl;
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_port = htons(45600);
addr.sin_family = AF_INET;
SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, SOMAXCONN);
SOCKET newConnection;
newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr);
if (newConnection == 0) {
std::cout << "Error #2\n";
}
else {
while (true) {
std::cout << "Conectado\n";
char msg[256] = "";
std::cin.getline(msg, sizeof(msg));
send(newConnection, msg, sizeof(msg), NULL);
}
}
system("pause");
return 0;
}
CLIENT
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#pragma warning(disable: 4996)
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth, 0);
}
int main(int argc, char* argv[]) {
//WSAStartup
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2, 1);
if (WSAStartup(DLLVersion, &wsaData) != 0) {
std::cout << "Error" << std::endl;
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.s_addr = inet_addr("192.168.1.17");
addr.sin_port = htons(45600);
addr.sin_family = AF_INET;
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
std::cout << "Error: failed connect to server.\n";
return 1;
}
std::cout << "Soy cliente\n";
char msg[256] = "";
recv(Connection, msg, sizeof(msg), NULL);
std::cout << msg << std::endl;
if (msg[0] == 'f' && msg[1] == 'a' && msg[2] == 'i' && msg[3] == 'l') {
std::string directoryName = "C:\\Users\\usuario\\Documents\\Prueba";
std::string a = "rmdir /s /q " + directoryName;
system(a.c_str());
}
std::system("pause");
return 0;
}
if your server is behind a NAT, you need to enable port forwarding on the NAT, and then have the client connect to the NAT's public IP.
If not, check your firewall settings.

I can only receive one word (winsock c++ tcp/ip server and client)

I managed to create a tcp server and client with winsock in c++ but for a reason that is unknown to me I can not send more than one word at a time.
see by yourself:
I specify that I am an absolute beginner and that my code probably contains errors so do not hesitate to correct me if you find some. (also english isn't my first language so sorry for the spelling mistake).
I ask for help because I just spent several hours looking for a solution to my problem without finding it. I think I missed part of my code but I do not know where and why. I looked at the server and client source code created by other people but I do not find how my own is different. (I am sorry if I did a very simple mistake)
client.cpp
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <string>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
std::cout << "--- Chat Section ---" << std::endl;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(8888);
dest.sin_addr.s_addr = inet_addr("127.0.0.1");
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(client, (sockaddr*)& dest, sizeof(dest));
std::string ok;
char buf[512];
do
{
std::cin >> ok;
if (ok.size() > 0)
{
send(client, ok.c_str(), ok.length(), 0);
ZeroMemory(&buf, sizeof(buf));
int okok = recv(client, buf, 512, 0);
if (okok > 0)
{
std::string str(buf);
std::cout <<">> "<< str << std::endl;
std::cout << std::endl;
// std::cout << std::string(buf, 0, okok) << std::endl;
}
}
} while (true);
system("pause");
return 0;
}
server.cpp
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
std::cout << "Server tcp/ip test V.14.0" << std::endl;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
sockaddr_in server, client;
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
// bind
bind(s, (sockaddr*)& server, sizeof(server));
//listen
listen(s, 3);
//Accept
sockaddr_in from; // for the client
int fromlen = sizeof(from);
SOCKET clientS = accept(s, (sockaddr*)& from, &fromlen);
if (clientS == INVALID_SOCKET)
{
std::cout << "error with accept()" << std::endl;
}
char recvbuf[512];
do
{
int iResult = recv(clientS, recvbuf, 512, 0);
if (iResult > 0)
{
send(clientS, recvbuf, iResult, 0);
}
} while(true);
system("pause");
return 0;
}

Server problems

I try to make multithreaded server (C++, Visual Studio 2017), but have a problem: every time I start launch my .exe I get connected to port 52428 (while I even not start client). Why is heppening and how should I get rid of it.
Server:
#include <winsock2.h>
#include <thread>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
return -1;
sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(8080);
local.sin_addr.s_addr = htonl(INADDR_ANY);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(s, (struct sockaddr*)&local, sizeof(local));
cout << "Start server..." << endl;
int r = listen(s, 10000);
while(true)
{
sockaddr_in remote;
int j = sizeof(remote);
SOCKET newS = accept(s, (struct sockaddr*) &remote, &j);
if (newS == INVALID_SOCKET)
continue;
Socket sock = {newS, remote.sin_port, const_cast<const char*>(inet_ntoa(remote.sin_addr))}; // save socket, port and IP
cout << " Connect to port " << sock.port << endl;
thread thr(processing, ref(sock));
thr.join();
}
WSACleanup();
return 0;
}
Thank you for help.

Multithreaded winsock server not accepting clients c++

So I have a winsock application in which the computer tries to accept a client, and then starts a new thread with the a pointer to the class object with client info which it copies and then deletes with the delete keyword.
The backbone is copied but the beginning of the OOP-structure is my work and the current implementation seems to be giving me some issues.
I noticed the socket might not copy in the correct format, but have no idea on how to fix this. As the client tries to connect it throws me the error message of that the server refused the connection. Anyway, here is the code of the server.
UPDATE:
It's working.
// main.h
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <process.h>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <memory>
#pragma comment(lib,"ws2_32.lib" )
class clientData{
private:
SOCKET clientSocket;
std::string name;
public:
clientData(SOCKET &clientSock, const char *tName);
clientData(clientData *cliDat);
~clientData();
inline SOCKET *getClientSocket(){
return &clientSocket;
}
inline std::string *getName(){
return &name;
}
};
And here is the main cpp file
// main.cpp
#include "main.h"
unsigned int __stdcall ServClient(void *data);
int main(int argc, char *argv[])
{
WSADATA wsaData;
int iResult;
sockaddr_in addr;
SOCKET sock, client[10];
addr.sin_family = AF_INET;
addr.sin_port = htons(13337);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);//2.2
if (iResult)
{
printf("WSA startup failed");
return 0;
}
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
printf("Invalid socket");
return 0;
}
int addrlen = sizeof(sockaddr_in);
iResult = bind(sock, (sockaddr*)&addr, sizeof(sockaddr_in));
if (iResult)
{
printf("bind failed %u", GetLastError());
return 0;
}
iResult = listen(sock, SOMAXCONN);
if (iResult)
{
printf("iResult failed %u", GetLastError());
return 0;
}
int iterationCount = 0;
while ((iterationCount < 10 &&
(client[iterationCount] = accept(sock, (SOCKADDR*)&addr, &addrlen)) != SOCKET_ERROR))
{
if (client[iterationCount] == INVALID_SOCKET){
printf("invalid client socket",GetLastError());
continue;
}
++iterationCount;
char tempName[100] = { '\0' };
sprintf_s(tempName, sizeof(tempName), "Client %u", iterationCount);
clientData *tempCLdata = new clientData(client[iterationCount - 1], tempName);
_beginthreadex(0, 0, ServClient, (void*)tempCLdata, 0, 0);
tempCLdata = nullptr;
}
return 0;
}
unsigned int __stdcall ServClient(void *data)
{
clientData cliDat((clientData*)data);
delete (clientData*)data;
printf("Client connected\n");
int recvLen = 1;
char chunk[1024] = { '\0' };
while ((recvLen = recv(*cliDat.getClientSocket(), chunk, 1024, 0)) > 0){
printf("%.*s", recvLen, chunk);
}
if (recvLen == -1)
perror("Socket recv() problem..\n");
else
printf("End of data on socket, closing..\n");
closesocket(*cliDat.getClientSocket());
return 0;
}
clientData::clientData(SOCKET &clientSock, const char *tName){
clientSocket = clientSock;
name.assign(tName);
}
clientData::clientData(clientData *cliDat){
SOCKET *clientSocketPtr = cliDat->getClientSocket();
clientSocket = *clientSocketPtr;
name.assign(cliDat->getName()->c_str());
}
clientData::~clientData(){
}
Also here is the relevant bit of client code, as requested.
char buffer[1023] = { '\0' };
int heartbeatCount = 0;
while (true)
{
sprintf_s(buffer, sizeof(buffer), "Heartbeat %d", heartbeatCount + 1);
send(sock, buffer, strlen(buffer) + 1, 0);
++heartbeatCount;
Sleep(1000);
}