Skip to content

Instantly share code, notes, and snippets.

@montogeek
Created May 3, 2013 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montogeek/5514138 to your computer and use it in GitHub Desktop.
Save montogeek/5514138 to your computer and use it in GitHub Desktop.
#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>
using namespace std;
#define MAXLON 256
#define MIPUERTO 9996
#define MAXLONGITUD 100
int nosocket;
char const host_servidor[]="192.168.9.168";
struct hostent *host_entrante;
struct sockaddr_in servidor;
string mensaje = "";
void Enviar(int, string);
void Recibir(int);
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();
int opcion=0;
string msjrec = recibirmensaje(nosocket);
cout<<"Mensaje servidor: "<<msjrec<<endl;
string msjenv;
int op = 0;
do{
cout<<"Enviar mensaje: ";
cin>>msjenv;
Enviar(nosocket,msjenv);
Recibir(nosocket);
}while(msjenv != "fin");
/*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 Recibir(int s){
int nb;
char msjrec[MAXLON];
nb = recv(s,msjrec,MAXLON,0);
if (nb == -1){
cout<<"Error al recibir."<<endl;
exit(-1);
}
if(nb == 0){
cout<<"Conexion finalizada."<<endl;
close(s);
}
if(nb > 0){
msjrec[nb]= '\0';
mensaje = (string) msjrec;
}
}
void Enviar(int s, string msj)
{
char msjenv[MAXLON];
strcpy(msjenv,msj.c_str());
send(s,msjenv,msj.size(),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