Hello, I'm trying a very simple chat application with accepts some clients with ID (not yet implemented) and it works perfectly but for only 1 client.
Any ideas on how to accept more clients and then send information between them?
I want to assign an ID to every client so then it would be easy to select one.
Here goes my code so far using (Dev-C++):
#include <stdio.h>
#include <iostream>
#ifdef _WIN32 // WINDOWS
#include <winsock2.h> // Librería de sockets
#include <cstdlib>
#else // LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
using namespace std;
WSADATA wsadata; //Declaramos WSADATA
struct hostent *host;
SOCKADDR_IN conexloc;
SOCKET locsock;
char Buffer[1024];
int WSAStarted()
{
int wasa = WSAStartup(MAKEWORD(2,0),&wsadata);
you are using a unique loop for receiving data from the clients... look at the code, accept() is called once...
first, you will need to create an array of SOCKETS to store client connections...
an acceptor thread must be coded for accepting the clients (it can be done on the main thread)
but after that, for receiving data, you will need another threads (one-per-client, just one for all clients, which can be a little laggy, or a thread pool, read about Io Completion Ports too)
Hello, before posting this doubt I knew that the way to do it is by storing the clients in an array, but.. How can I store the clients in that array? I've never worked before with sockets so I don't know exacty what should be stored.. the IP?? if the answer is yes.. How can I capture that IP? if not.. what do I have to do? with a sample modification of my code or pseudo-code it'll be great for me to understand.
The server itself will be laggy so I think I won't use just 1 thread for all clients. How can I make it with 1 thread for each client?
Thanks for your answer. You've given me some ideas but unfortunately I don't know how to implement them.
but, keep in mind: a lot of clients connected will create a lot of threads, which can be a little problem. once you have understood how the whole system works, try doing it over an IOCP server (no, it isnt so simple to do :P)
Thank you very much dude, I'll take a look at that site to see what can I learn about sockets.
I think I'll re-use a code that I've found around google. As you say... it's not simple thing to do hehe. So I think I can study it without trying to code something that is already done.
Anyways... if someone wants to make a contribution helping me to code it I would appreciate it very much.
Bookmarks