Skip to content

Instantly share code, notes, and snippets.

@montogeek
Created March 21, 2013 17: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/5214689 to your computer and use it in GitHub Desktop.
Save montogeek/5214689 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 <sstream>
#include <unistd.h>
using namespace std;
#define MIPUERTO 6779//http://zzantares.blogspot.com/2012/01/en-algunas-ocasiones-me-ha-sido.html //pto de conexion para usuarios
#define MAXLONGITUD 100 //max de caracteres que va a recibir
int nosocket, numbytes;
//char msj[MAXLONGITUD];
char const host_servidor[] = "127.0.0.1";
struct hostent *host_entrante; //netdb.h
struct sockaddr_in servidor;
void Servicio1();
void Servicio2();
int inihost(){
int sierror =0;
host_entrante = gethostbyname(host_servidor);
if (host_entrante == NULL){
cout<<"Error al recibir host"<<endl;
sierror= -1;
}
nosocket = socket(AF_INET, SOCK_STREAM, 0);
if (nosocket == -1){
cout<<"Error en conexion 1"<<endl;
sierror= -1;
}
return sierror;
}
int iniservidor(){
int sierror = 0;
servidor.sin_family = AF_INET;
servidor.sin_port = htons(MIPUERTO);
servidor.sin_addr = *((struct in_addr *) host_entrante->h_addr); //pasa la informacion
bzero(&(servidor.sin_zero),8);
if (connect(nosocket, (struct sockaddr *)&servidor, sizeof(struct sockaddr)) == -1){
cout<<"Error en conexion 2"<<nosocket<<endl;
sierror = -1;
}
return sierror;
}
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){
char msj[MAXLONGITUD];
msj[0] = '\0';
strcpy(msj, msj_nuevo.c_str()); //c_str() convierte a char
//cout<<"enviarmensaje: "<<msj_nuevo.size()<<endl;
send(s, msj, msj_nuevo.size(), 0);
}
int main(){
if (inihost() == -1){
exit(-1);
}
if (iniservidor() == -1){
exit(-1);
}
string msj = recibirmensaje(nosocket), msj1;
cout<<"Mensaje: "<<msj<<endl;
enviarmensaje(nosocket,"Hi there");
/*//Ciclo del servicio
int opcion = 0;
do{
cout<<"Menu: "<<endl;
cout<<"1. Registrarse"<<endl;
cout<<"2. Autentificar"<<endl;
cout<<"3. Salir"<<endl<<endl;
cout<<"Ingrese opcion: ";
cin>>opcion;
switch(opcion){
case 1:{
enviarmensaje(nosocket,"1");
Servicio1();
break;
}
case 2:{
enviarmensaje(nosocket,"2");
Servicio2();
break;
}
default: break;
}
}while((opcion>0) && (opcion<3));*/
close(nosocket);
return 0;
}
/*
void Servicio1(){
string msj = recibirmensaje(nosocket);
cout<<msj<<endl;
string id,nom,ap,cl;
char dato[MAXLONGITUD];
cout<<"Documento: ";
cin>>id;
enviarmensaje(nosocket,id);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj;
dato[0]='\0';
cout<<"Nombres: ";
cin>>nom;
enviarmensaje(nosocket,nom);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj;
dato[0]='\0';
cout<<"Apellido: ";
cin>>ap;
enviarmensaje(nosocket,ap);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj;
dato[0]='\0';
cout<<"Clave: ";
cin>>cl;
enviarmensaje(nosocket,cl);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj;
msj="";
msj=recibirmensaje(nosocket);
cout<<msj<<endl;
}
void Servicio2(){
string msj = recibirmensaje(nosocket);
cout<<msj<<endl;
string id,clave;
cout<<"Identificacion: ";
cin>>id;
enviarmensaje(nosocket,id);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj<<endl;
cout<<"Clave: ";
cin>>clave;
enviarmensaje(nosocket,clave);
msj="";
msj=recibirmensaje(nosocket);
cout<<msj<<endl;
msj="";
msj=recibirmensaje(nosocket);
cout<<msj<<endl;
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment