Created
March 21, 2013 17:06
-
-
Save montogeek/5214695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <stdlib.h> | |
#include "string.h" | |
#include "malloc.h" | |
#include "sys/types.h" | |
#include "sys/socket.h" | |
#include "netinet/in.h" | |
#include "arpa/inet.h" | |
#include <sstream> | |
#include <unistd.h> | |
#include <list> | |
using namespace std; | |
#define MIPUERTO 6779 //pto de conexion para usuarios | |
#define MIIPO "127.0.0.1" | |
#define ENCOLA 10 //nº de conexiones permitidas | |
#define MAXLONGITUD 100 //max de caracteres que va a recibir | |
/*list<Persona> Servicio1(int,list<Persona>); | |
bool Servicio2(int,list<Persona>); | |
bool existep(string,list<Persona>); | |
bool authi(string,string,list<Persona>);*/ | |
void recibir(int); | |
fd_set lector, maestro; | |
int maxdes; | |
int nosocket, nuevo; | |
struct sockaddr_in dirservidor; //info sobre mi direccion | |
struct sockaddr_in cliente; //info sobre conexiones entrantes | |
socklen_t senal_tam,lon; //tamano de la senal | |
void inisocket(){ | |
//llamada al socket | |
nosocket = socket(AF_INET, SOCK_STREAM,0); | |
if (nosocket == -1){ | |
cout<<"Error en socket."<<endl; | |
exit(-1); | |
} | |
} | |
void iniservidor(){ | |
dirservidor.sin_family = AF_INET; | |
dirservidor.sin_port = htons(MIPUERTO); | |
dirservidor.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
bzero(&(dirservidor.sin_zero),8); | |
} | |
void iniconexion(){ | |
//permite asociar un socket a un puerto | |
if (bind(nosocket, (struct sockaddr*)&dirservidor, sizeof(struct sockaddr)) == -1){ | |
cout<<"error en bind()"<<endl; | |
exit(-1); | |
} | |
//maximo numero de conexiones | |
if (listen(nosocket, ENCOLA) == -1){ | |
cout<<"error en listen()"<<endl; | |
exit(-1); | |
} | |
cout<<"No socket"<<nosocket<<endl; | |
} | |
string recibirmensaje(int s){ | |
char msj[MAXLONGITUD]; | |
int numbytes = recv(s, msj, MAXLONGITUD, 0); | |
string msjrec=""; | |
if (numbytes == -1){ | |
cout<<"error al recibir"<<endl; | |
} | |
if (numbytes == 0){ | |
cout<<"Conexion finalizada, 0 bytes"<<endl; | |
} | |
if (numbytes > 0){ | |
msj[numbytes] = '\0'; | |
//cout<<"Mensaje del cliente: "<<msj<<endl; | |
msjrec = (string)msj; | |
} | |
return msjrec; | |
} | |
void enviarmensaje(int s, string msj_nuevo){ | |
//cout<<"Socket: "<<s<<endl; | |
//cout<<"Mensaje: "<<msj_nuevo<<endl; | |
char msj[MAXLONGITUD]; | |
msj[0] = '\0'; | |
//cout<<"msj antes: "<<msj<<endl; | |
strcpy(msj, msj_nuevo.c_str()); //c_str() convierte a char | |
//cout<<"msj despues: "<<msj<<endl; | |
//cout<<"msj: "<<msj<<endl; | |
//cout<<"msj_nuevo: "<<msj_nuevo<<endl; | |
send(s, msj, msj_nuevo.size(), 0); | |
} | |
string inttostr(int number){ | |
if (number == 0) | |
return "0"; | |
string temp=""; | |
string returnvalue=""; | |
while (number>0) | |
{ | |
temp+=number%10+48; | |
number/=10; | |
} | |
for (int i=0;i<temp.length();i++) | |
returnvalue+=temp[temp.length()-i-1]; | |
return returnvalue; | |
} | |
int strtoint(string str){ | |
string aux = str; | |
return atoi(aux.c_str()); | |
} | |
int main(){ | |
inisocket(); | |
iniservidor(); | |
iniconexion(); | |
char *dire; | |
char msjenv[MAXLONGITUD]; | |
string msj = ""; | |
FD_ZERO(&maestro); | |
FD_ZERO(&lector); | |
FD_SET(nosocket,&maestro); | |
maxdes = nosocket; | |
while(msj != "fin"){ | |
lector = maestro; | |
if(select(maxdes+1,&lector,NULL,NULL,NULL) == -1){ | |
cout<<"Error en select"<<endl; | |
exit(-1); | |
} | |
//Explorar las conexiones | |
for (int i = 0; i <= maxdes; ++i){ | |
if(FD_ISSET(i,&lector)){ | |
if(i == nosocket){ | |
lon = sizeof(struct sockaddr_in); | |
nuevo = accept(nosocket,(struct sockaddr *)&cliente,&lon); | |
if(nuevo == -1){ | |
cout<<"Error en accept"<<endl; | |
}else{ | |
FD_SET(nuevo,&maestro); | |
if(nuevo > maxdes){ | |
maxdes = nuevo; | |
dire = inet_ntoa(cliente.sin_addr); | |
send(nuevo,"Bienvenido al servidor.\n",22,0); | |
cout<<"Direccion entrante: "<<dire<<endl; | |
} | |
} | |
}else{ | |
recibir(i); | |
} | |
} | |
} | |
} | |
close(nosocket); | |
return 0; | |
} | |
void recibir(int socket){ | |
int nb; | |
char msjrec[MAXLONGITUD]; | |
nb = recv(socket,msjrec,MAXLONGITUD,0); | |
if(nb == -1){ | |
cout<<"Error al recibir. "<<endl; | |
exit(-1); | |
} | |
if(nb == 0){ | |
cout<<"Conexion finalizada"<<endl; | |
close(socket); | |
FD_CLR(socket,&maestro); | |
} | |
if(nb>0){ | |
msjrec[nb] = '\0'; | |
cout<<"Mensaje: "<<msjrec<<endl; | |
} | |
} | |
/* | |
list<Persona> Servicio1(int s,list<Persona> l){ | |
enviarmensaje(s,"Empieza querido"); | |
string id,nom,ap,cl; | |
id = recibirmensaje(s); | |
cout<<"Identificacion: "<<id<<" | "; | |
enviarmensaje(s,"\tRecibido\n"); | |
nom = recibirmensaje(s); | |
cout<<"Nombre: "<<nom<<" | "; | |
enviarmensaje(s,"\tRecibido\n"); | |
ap = recibirmensaje(s); | |
cout<<"Apellido: "<<ap<<" | "; | |
enviarmensaje(s,"\tRecibido\n"); | |
cl = recibirmensaje(s); | |
cout<<"Clave: "<<cl<<" | "<<endl; | |
enviarmensaje(s,"\tRecibido\n"); | |
Persona p; | |
p.AdDocumento(id); | |
p.AdNombre(nom); | |
p.AdApellido(ap); | |
p.AdClave(cl); | |
if(existep(id,l) == false){ | |
l.push_back(p); | |
enviarmensaje(s,"Registro correcto :*"); | |
} | |
else{ | |
enviarmensaje(s,"Ya estas registrado boludo!"); | |
} | |
return l; | |
} | |
bool Servicio2(int s, list<Persona> l){ | |
enviarmensaje(s,"Estas en el servicio 2"); | |
bool auth = false; | |
string id,clave; | |
id = recibirmensaje(s); | |
cout<<"Identificacion: "<<id<<" | "; | |
enviarmensaje(s,"\tRecibido"); | |
clave = recibirmensaje(s); | |
cout<<"Clave: "<<clave<<" | "; | |
enviarmensaje(s,"\tRecibido"); | |
return authi(id,clave,l); | |
} | |
bool authi(string id, string clave, list<Persona> l){ | |
bool auth = false; | |
Persona aux; | |
list<Persona>::iterator i; | |
for (i = l.begin(); i != l.end(); i++) | |
{ | |
aux = *i; | |
if(aux.RetDocumento() == id && aux.RetClave() == clave){ | |
auth = true; | |
} | |
} | |
return auth; | |
} | |
bool existep(string id, list<Persona> l){ | |
bool existe = false; | |
Persona aux; | |
list<Persona>::iterator i; | |
for (i = l.begin(); i != l.end(); i++) | |
{ | |
aux = *i; | |
if(aux.RetDocumento() == id){ | |
existe = true; | |
} | |
} | |
return existe; | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment