Ernie-
August 31st, 2009, 08:00 PM
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);
if (wasa != 0)
{
printf("Error initializing WSAStartup\n");
WSACleanup();
return 1; // Return 1 if something failed
}
return 0;
}
int DefineSocket()
{
// using Socket Stream(TCP)
locsock = socket(AF_INET/* IP V4 */, SOCK_STREAM, 0);
if (locsock == INVALID_SOCKET) // if something went wrong…
{
printf("Error defining socket\n");
WSACleanup(); //Limpiamos WSADATA
return 1; // Return 1 if something failed
}
return 0;
}
int estructsocket()
{
conexloc.sin_family = AF_INET;
/*
Define IPv4
*/
conexloc.sin_addr.s_addr = INADDR_ANY;
/*
Define local IP
*/
conexloc.sin_port = htons(9999);
// if something failed when binding...
if (bind(locsock, (sockaddr*)&conexloc, sizeof(conexloc)) == SOCKET_ERROR)
{
printf("Error defining socket\n"); //Mostramos un mensaje
WSACleanup(); //Limpiamos WSADATA
return 1; // Return 1 if something failed
}
else
{
if (listen(locsock, 5) == SOCKET_ERROR) // if something failed when listening...
{
printf("Error when listening\n");
WSACleanup(); //clean WSADATA
return 1; // Return 1 if something failed
}
else
{
printf("Listening on port 5555 for incoming connections...\n\n");
return 0; // Everything went okay so we'll return '0'
}
}
}
void connetion()
{
int conm;
conm=sizeof(struct sockaddr);
locsock=accept(locsock,(sockaddr*)&conexloc,&conm);
printf("Stablishing connection... OK");
while (conm!=0)
{ //while we're connected...
conm=recv(locsock,Buffer,sizeof(Buffer),0); //we'll receive the data
if (conm>0)
{ //if the client is still connected...
printf("Received data:%s",Buffer); //print the received data
}
}
}
void sockets()
{
if((WSAStarted()) == 0)
{ // WSAStarted correctly…
if((defininingsocket()) == 0)
{ // socket defined correctly…
if((estructsocket()) == 0)
{// estructsocket initialized correctly…
connection(); // Start the connection() procedure
}
}
}
}
int main(int argc, char *argv[])
{
sockets();
}
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);
if (wasa != 0)
{
printf("Error initializing WSAStartup\n");
WSACleanup();
return 1; // Return 1 if something failed
}
return 0;
}
int DefineSocket()
{
// using Socket Stream(TCP)
locsock = socket(AF_INET/* IP V4 */, SOCK_STREAM, 0);
if (locsock == INVALID_SOCKET) // if something went wrong…
{
printf("Error defining socket\n");
WSACleanup(); //Limpiamos WSADATA
return 1; // Return 1 if something failed
}
return 0;
}
int estructsocket()
{
conexloc.sin_family = AF_INET;
/*
Define IPv4
*/
conexloc.sin_addr.s_addr = INADDR_ANY;
/*
Define local IP
*/
conexloc.sin_port = htons(9999);
// if something failed when binding...
if (bind(locsock, (sockaddr*)&conexloc, sizeof(conexloc)) == SOCKET_ERROR)
{
printf("Error defining socket\n"); //Mostramos un mensaje
WSACleanup(); //Limpiamos WSADATA
return 1; // Return 1 if something failed
}
else
{
if (listen(locsock, 5) == SOCKET_ERROR) // if something failed when listening...
{
printf("Error when listening\n");
WSACleanup(); //clean WSADATA
return 1; // Return 1 if something failed
}
else
{
printf("Listening on port 5555 for incoming connections...\n\n");
return 0; // Everything went okay so we'll return '0'
}
}
}
void connetion()
{
int conm;
conm=sizeof(struct sockaddr);
locsock=accept(locsock,(sockaddr*)&conexloc,&conm);
printf("Stablishing connection... OK");
while (conm!=0)
{ //while we're connected...
conm=recv(locsock,Buffer,sizeof(Buffer),0); //we'll receive the data
if (conm>0)
{ //if the client is still connected...
printf("Received data:%s",Buffer); //print the received data
}
}
}
void sockets()
{
if((WSAStarted()) == 0)
{ // WSAStarted correctly…
if((defininingsocket()) == 0)
{ // socket defined correctly…
if((estructsocket()) == 0)
{// estructsocket initialized correctly…
connection(); // Start the connection() procedure
}
}
}
}
int main(int argc, char *argv[])
{
sockets();
}