Created
May 3, 2013 21:05
-
-
Save montogeek/5514142 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 <unistd.h> | |
#include <sstream> | |
#include <list> | |
using namespace std; | |
#define MIPUERTO 9997 // pto de conexion para usuarios | |
#define MIIP "192.168.9.91" //ip | |
#define MAXLON 100 | |
#define ENCOLA 10 //No de conexiones | |
int nosocket, nuevo; | |
struct sockaddr_in dirservidor; | |
struct sockaddr_in cliente; | |
socklen_t senal_tam; | |
socklen_t lon; | |
void Enviar(int, struct sockaddr_in); | |
int Recibir(int); | |
void Responder(int); | |
void inisocket(); | |
void iniservidor(); | |
void iniconexion(); | |
/*string recibirmensaje(int); | |
void enviarmensaje(string,int); | |
Asignatura Servicio1(Asignatura); | |
list<Asignatura> Servicio2(list<Asignatura>); | |
void Servicio3(list<Asignatura>); | |
void Servicio4(list<Asignatura>); | |
bool existep(string id, list<Asignatura>); | |
void Mostrar(list<Asignatura>);*/ | |
int main(int args, char *argv[]){ | |
inisocket(); | |
iniservidor(); | |
iniconexion(); | |
int fin = 1; | |
do{ | |
lon = sizeof(struct sockaddr_in); | |
nuevo = accept(nosocket, (struct sockaddr *)&cliente,&lon); | |
if(nuevo ==-1){ | |
cout<<"Error en accept"<<endl; | |
} | |
else{ | |
pid_t idProceso; | |
int estadoHijo; | |
if((idProceso = fork()) == -1){ | |
perror("No se puede crear proceso"); | |
exit(-1); | |
} | |
// hijo | |
if(idProceso == 0){ | |
int estado = 0; | |
// Ciclo de servicio del hijo | |
do{ | |
estado = Recibir(nuevo); | |
if(estado != 33){ | |
Responder(nuevo); | |
} | |
}while((estado != 33) && (estado != -1)); | |
exit(33); | |
} | |
// padre | |
if(idProceso > 0){ | |
Enviar(nuevo,cliente); | |
} | |
}// fin si accept | |
} while(fin == 1); | |
//list<Asignatura> lp; | |
/*while(1) | |
{ | |
senal_tam = sizeof(struct sockaddr_in); | |
nuevo_socket = accept(nosocket,(struct sockaddr*)&cliente,&senal_tam); | |
if(nuevo_socket == -1) | |
{ | |
cout<<"error en accept() "<<endl; | |
exit(-1); | |
} | |
cout<<"nuevo_socket: "<<nuevo_socket<<endl; | |
char *dire; | |
dire=inet_ntoa(cliente.sin_addr); | |
cout<<"Direccion del cliente: "<<dire<<endl; | |
char msjrec[MAXLONGITUD]; | |
msjrec[0] = '\0'; | |
int opcion = 0; | |
char msjenv[MAXLONGITUD]= "Bienvenido al servidor, Ingrese los datos personales."; | |
enviarmensaje(msjenv, nuevo_socket); | |
do{ | |
strcpy(msjrec, (recibirmensaje(nuevo_socket)).c_str()); | |
opcion = atoi(msjrec); | |
enviarmensaje("Opcion recibida", nuevo_socket); | |
cout<<"opcion: "<<msjrec<<endl; | |
Asignatura a; | |
if(opcion == 1) a = Servicio1(a); | |
if(opcion == 2) lp = Servicio2(lp); | |
if(opcion == 3) Servicio3(lp); | |
if(opcion == 4) Servicio4(lp); | |
if(opcion==1){ | |
lp.push_back(a); | |
enviarmensaje("Se ha guardado la asignatura", nuevo_socket); | |
} | |
else if(opcion==2){ | |
enviarmensaje("Se ha guardado el alumno", nuevo_socket); | |
} | |
else if(opcion==3){ | |
enviarmensaje("Lista de asignaturas", nuevo_socket); | |
} | |
else if(opcion==4){ | |
enviarmensaje("Lista de alumnos asignaturas", nuevo_socket); | |
} | |
else if(opcion==5 ){ | |
enviarmensaje("Ha salido del servidor", nuevo_socket); | |
} | |
Mostrar(lp); | |
}while(opcion>0 && opcion <5); | |
cout<<"Cliente: "<<recibirmensaje(nuevo_socket)<<endl; | |
enviarmensaje("Fin", nuevo_socket); | |
//cerramos la conexion | |
close(nuevo_socket); | |
} | |
*/ | |
return 0; | |
} | |
int Recibir(int ns){ | |
int nb, salida; | |
char msj[MAXLON]; | |
nb = recv(ns,msj,MAXLON,0); | |
if(nb == -1){ | |
cout<<"Error al recibir. "<<endl; | |
salida = -1; | |
} | |
if(nb == 0){ | |
cout<<"Conexion finalizada. "<<endl; | |
close(ns); | |
salida = 33; | |
} | |
if(nb > 0){ | |
msj[nb] = '\0'; | |
cout<<"Mensaje cliente "<<ns<<": "<<msj<<endl; | |
msj[0] = '\0'; | |
salida = 1; | |
} | |
return salida; | |
} | |
void Enviar(int ns, struct sockaddr_in cl){ | |
char *dire; | |
dire = inet_ntoa(cl.sin_addr); | |
send(ns,"Bienvenido al servidor.\n",22,0); | |
cout<<"Direccion entrante: "<<dire<<endl; | |
} | |
void Responder(int ns){ | |
send(ns,"Recibido.\n",8,0); | |
} | |
/*Asignatura Servicio1(Asignatura a){ | |
a.AdIdAsignatura(recibirmensaje(nuevo_socket)); | |
enviarmensaje("Recibido", nuevo_socket); | |
a.AdHora(recibirmensaje(nuevo_socket)); | |
enviarmensaje("Recibido", nuevo_socket); | |
a.AdDia(recibirmensaje(nuevo_socket)); | |
enviarmensaje("Recibido", nuevo_socket); | |
a.AdIdAlumno("0"); | |
return a; | |
}*/ | |
/*list<Asignatura> Servicio2(list<Asignatura> ls){ | |
stringstream tam; | |
string id = recibirmensaje(nuevo_socket); | |
enviarmensaje("Recibido el id de la asignatura", nuevo_socket); | |
list<Asignatura>::iterator i; | |
Asignatura a; | |
Asignatura b; | |
for (i = ls.begin(); i != ls.end(); ++i) | |
{ | |
a = *i; | |
if(a.RetIdAsignatura().compare(id)==0){ | |
b.AdIdAsignatura(id); | |
b.AdIdAlumno(recibirmensaje(nuevo_socket)); | |
enviarmensaje("Recibido alumno", nuevo_socket); | |
b.AdHora(a.RetHora()); | |
b.AdDia(a.RetDia()); | |
ls.push_back(b); | |
break; | |
} | |
} | |
return ls; | |
}*/ | |
/*void Servicio3(list<Asignatura> ls){ | |
string msjenv; | |
list<Asignatura>::iterator i; | |
Asignatura a; | |
list<Asignatura> aux; | |
for (i = ls.begin(); i != ls.end(); ++i) | |
{ | |
a = *i; | |
if(a.RetIdAlumno().compare("0")==0){ | |
aux.push_back(a); | |
} | |
} | |
stringstream s; | |
s<<aux.size(); | |
enviarmensaje(s.str(), nuevo_socket); | |
cout<<"Cliente: "<<recibirmensaje(nuevo_socket)<<endl; | |
for (i = aux.begin(); i != aux.end(); ++i) | |
{ | |
a = *i; | |
enviarmensaje(a.RetIdAsignatura(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
enviarmensaje(a.RetHora(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
enviarmensaje(a.RetDia(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
} | |
}*/ | |
/*void Servicio4(list<Asignatura> ls){ | |
string msjenv; | |
string id = recibirmensaje(nuevo_socket); | |
enviarmensaje("Recibido el id de asignatura", nuevo_socket); | |
list<Asignatura>::iterator i; | |
Asignatura a; | |
list<Asignatura> aux; | |
for (i = ls.begin(); i != ls.end(); ++i) | |
{ | |
a = *i; | |
cout<<"id: "<<id<<endl; | |
cout<<"RetIdAsignatura: "<<a.RetIdAsignatura()<<endl; | |
if(a.RetIdAsignatura().compare(id)==0){ | |
aux.push_back(a); | |
cout<<"entrooo"<<endl; | |
} | |
} | |
stringstream s; | |
s<<aux.size(); | |
enviarmensaje(s.str(), nuevo_socket); | |
cout<<"Cliente: "<<recibirmensaje(nuevo_socket)<<endl; | |
for (i = aux.begin(); i != aux.end(); ++i) | |
{ | |
a = *i; | |
enviarmensaje(a.RetIdAsignatura(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
enviarmensaje(a.RetIdAlumno(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
enviarmensaje(a.RetHora(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
enviarmensaje(a.RetDia(), nuevo_socket); | |
cout<<"Cliente: "<<(recibirmensaje(nuevo_socket))<<endl; | |
} | |
}*/ | |
/*bool existep(string id, list<Asignatura> ls){ | |
bool existe=false; | |
list<Asignatura>::iterator i; | |
Asignatura m; | |
for (i = ls.begin(); i != ls.end(); ++i) | |
{ | |
m = *i; | |
if(m.RetIdAsignatura().compare(id)==0)existe=true; | |
} | |
return existe; | |
}*/ | |
/*void Mostrar(list<Asignatura> ls){ | |
list<Asignatura>::iterator it; | |
Asignatura m; | |
cout<<"----------------------------------"<<endl; | |
cout<<"Lista de Asignatura: "<<endl; | |
for (it = ls.begin(); it != ls.end(); ++it) | |
{ | |
m=*it; | |
cout<<endl; | |
cout<<"Asignatura: "<<m.RetIdAsignatura()<<endl; | |
cout<<"Alumno: "<<m.RetIdAlumno()<<endl; | |
cout<<"Hora: "<<m.RetHora()<<endl; | |
cout<<"Dia: "<<m.RetDia()<<endl; | |
} | |
cout<<"----------------------------------"<<endl; | |
}*/ | |
void inisocket() | |
{ | |
nosocket = socket(AF_INET, SOCK_STREAM,0); | |
if(nosocket==-1) | |
{ | |
cout<<"Error en socket."<<endl; | |
exit(-1); // salida forzada del programa | |
} | |
} | |
void iniservidor() | |
{ | |
dirservidor.sin_family = AF_INET; | |
dirservidor.sin_port = htons(MIPUERTO); | |
dirservidor.sin_addr.s_addr = inet_addr(MIIP); | |
bzero(&(dirservidor.sin_zero),8); | |
} | |
void iniconexion() | |
{ | |
// bind(socket, direccion/pueto, tamaño socket) asgina el puerto si esta libre | |
if(bind(nosocket,(struct sockaddr*)&dirservidor,sizeof(struct sockaddr))==-1){ | |
cout<< "error en bind() " << endl; | |
exit(-1); | |
} | |
//listen() establece cual es el numero maximo de clientes que puede recibir el servidor. Solo se usa con servidores TCP. | |
if(listen(nosocket, ENCOLA) == -1) | |
{ | |
cout<<"error en listen() "<<endl; | |
exit(-1); | |
} | |
} | |
string recibirmensaje(int s) | |
{ | |
char msj[MAXLON]; | |
int numbytes=recv(s, msj, MAXLON, 0); | |
if(numbytes==-1)// falla en la conexion ajena a ambos | |
{ | |
cout<<"Error al recibir. "<<endl; | |
} | |
if(numbytes==0)//conexion finalizada por parte del cliente | |
{ | |
cout<<"Conexion finalizada. "<<endl; | |
} | |
if(numbytes>0) | |
{ | |
msj[numbytes]='\0'; | |
} | |
return (string)msj; | |
} | |
void enviarmensaje(string msj, int s) | |
{ | |
int ln=0; | |
ln = msj.size(); | |
char msj2[ln]; | |
strcpy(msj2, msj.c_str()); | |
send(s, msj2, ln, 0); | |
} | |
/* | |
#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 <netdb.h> | |
#include <unistd.h> | |
#include <sstream> | |
#include "asignatura.hpp" | |
using namespace std; | |
#define MAXLON 256 | |
#define MIPUERTO 9999 | |
#define MAXLONGITUD 100 | |
int nosocket; | |
char const host_servidor[]="192.168.8.35"; | |
struct hostent *host_entrante; | |
struct sockaddr_in servidor; | |
void inihost(); | |
void iniservidor(); | |
void iniconexion(); | |
string recibirmensaje(int); | |
void enviarmensaje(string,int); | |
void Servicio1(); | |
void Servicio2(); | |
void Servicio3(); | |
void Servicio4(); | |
int main() | |
{ | |
inihost(); | |
iniservidor(); | |
iniconexion(); | |
char msjenv[MAXLONGITUD]; | |
msjenv[0] = '\0'; | |
int opcion=0; | |
string msjrec = recibirmensaje(nosocket); | |
cout<<"Mensaje servidor: "<<msjrec<<endl; | |
do{ | |
cout<<"Menu"<<endl; | |
cout<<"1. Adicionar asignatura"<<endl; | |
cout<<"2. Adicionar alumno"<<endl; | |
cout<<"3. Lista de asignaturas"<<endl; | |
cout<<"4. lista alumnos por asignatura"<<endl; | |
cout<<"5. Salir"<<endl; | |
cin>> msjenv; | |
enviarmensaje(msjenv,nosocket); | |
string msj = recibirmensaje(nosocket); | |
cout<<"servidor: "<<msj<<endl; | |
opcion = atoi(msjenv); | |
if(opcion == 1)Servicio1(); | |
if(opcion == 2)Servicio2(); | |
if(opcion == 3)Servicio3(); | |
if(opcion == 4)Servicio4(); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<endl; | |
}while(opcion>0 && opcion <5); | |
enviarmensaje("Fin",nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
close(nosocket); | |
return 0; | |
} | |
void Servicio1(){ | |
string msjenv; | |
char dato[MAXLON]; | |
cout<<"----------------------------------"<<endl; | |
cout<<"REGISTRO ASIGNATURA"<<endl; | |
cout<<"----------------------------------"<<endl; | |
cout<<"ID Asignatura: "; | |
cin.ignore(); | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<endl; | |
dato[0] = '\0'; | |
cout<<"Hora: "; | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<endl; | |
dato[0] = '\0'; | |
cout<<"Dia: "; | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<"----------------------------------"<<endl; | |
} | |
void Servicio2(){ | |
string msjenv; | |
char dato[MAXLON]; | |
cout<<"----------------------------------"<<endl; | |
cout<<"Actualizar "<<endl; | |
cout<<"----------------------------------"<<endl; | |
cout<<"ID Asignatura: "; | |
cin.ignore(); | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<endl; | |
dato[0] = '\0'; | |
cout<<"ID Alumno: "; | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
cout<<"Servidor: "<<recibirmensaje(nosocket)<<endl; | |
cout<<endl; | |
} | |
void Servicio3(){ | |
string msjrec, msjenv; | |
char dato[MAXLON]; | |
dato[0] = '\0'; | |
cout<<"----------------------------------"<<endl; | |
cout<<"Asignaturas Registradas: "; | |
strcpy(dato, (recibirmensaje(nosocket)).c_str()); | |
int num = atoi(dato); | |
enviarmensaje("recibido",nosocket); | |
for (int i = 0; i < num; ++i) | |
{ | |
cout<<endl; | |
cout<<"Asignatura: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
cout<<"Hora: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
cout<<"Dia: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
} | |
cout<<"----------------------------------"<<endl; | |
} | |
void Servicio4(){ | |
string msjrec, msjenv; | |
char dato[MAXLON]; | |
dato[0] = '\0'; | |
cout<<"----------------------------------"<<endl; | |
cout<<"Consulta id Asignatura: "; | |
cin.ignore(); | |
cin.getline(dato,MAXLON,'\n'); | |
msjenv=(string)dato; | |
enviarmensaje(msjenv,nosocket); | |
recibirmensaje(nosocket); | |
strcpy(dato, (recibirmensaje(nosocket)).c_str()); | |
int num = atoi(dato); | |
enviarmensaje("recibido",nosocket); | |
for (int i = 0; i < num; ++i) | |
{ | |
cout<<endl; | |
cout<<"Asignatura: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
cout<<"Alumno: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
cout<<"Hora: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
cout<<"Dia: "<<recibirmensaje(nosocket)<<endl; | |
enviarmensaje("recibido",nosocket); | |
} | |
cout<<"----------------------------------"<<endl; | |
} | |
void inihost(){ | |
host_entrante=gethostbyname(host_servidor); | |
if(host_entrante==NULL) | |
{ | |
cout<<"error al recibir host"<<endl; | |
exit(-1); | |
} | |
nosocket = socket(AF_INET, SOCK_STREAM, 0); | |
if(nosocket == -1) | |
{ | |
cout<<"error en conexion 1"<<endl; | |
exit(-1); | |
} | |
} | |
void iniservidor() | |
{ | |
servidor.sin_family = AF_INET; | |
servidor.sin_port = htons(MIPUERTO); | |
servidor.sin_addr = *((struct in_addr *)host_entrante->h_addr); | |
//he pasa la informacion de *he hacia h_addr | |
bzero(&(servidor.sin_zero),8); | |
} | |
void iniconexion() | |
{ | |
if(connect(nosocket, (struct sockaddr *)&servidor, sizeof(struct sockaddr))==-1) | |
{ | |
cout<<"Error en conexion 2"<<endl; | |
exit(-1); | |
} | |
} | |
string recibirmensaje(int s) | |
{ | |
char msj[MAXLONGITUD]; | |
string msjrec=""; | |
int numbytes=recv(s,msj,MAXLONGITUD,0); | |
if(numbytes==-1) | |
{ | |
cout<<"Error al recibir. "<<endl; | |
exit(-1); | |
} | |
if (numbytes == 0){ | |
cout<<"Conexion finalizada, 0 bytes"<<endl; | |
} | |
if (numbytes > 0){ | |
msj[numbytes] = '\0'; | |
msjrec = (string)msj; | |
} | |
return msjrec; | |
} | |
void enviarmensaje(string msjenv,int s) | |
{ | |
char msj[MAXLONGITUD]; | |
msj[0]='\0'; | |
strcpy(msj, msjenv.c_str()); | |
send(nosocket, msj, msjenv.size(), 0); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment