Codigo del programa que envia la imagen:
Código:
#include <iostream>
#include <winsock.h>
#include <fstream>
using namespace std;
int main(){
int iResult;
struct sockaddr_in server;
struct sockaddr_in client;
struct hostent *hp;
WSADATA wsaData;
SOCKET servidor;
SOCKET cliente;
ifstream input("ejemplo.jpg", ifstream::binary);
input.seekg(0, input.end);
long size = input.tellg();
input.seekg(0);
char * buffer = new char[size];
char tamanio[512];
itoa(size, tamanio, 10);
input.read(buffer, size);
cout << "\n\t\t ----- Servidor FTP by Abraham -----" << endl;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult){
cout << "Error al iniciar." << endl;
return -1;
}
hp = (struct hostent *)gethostbyname("localhost");
if(!hp){
cout << "No se puede conectar al servidor." << endl;
return -1;
}
servidor = socket(AF_INET, SOCK_STREAM, 0);
if(servidor == INVALID_SOCKET){
cout << "No se puede crear socket." << endl;
WSACleanup();
return -1;
}
memset(&server, 0, sizeof(servidor));
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(6000);
if(bind(servidor, (struct sockaddr*)&server, sizeof(server))==SOCKET_ERROR){
cout << "Error al asociar socket." << endl;
WSACleanup();
return -1;
}
if(listen(servidor, 0)==SOCKET_ERROR){
cout << "Error al escuchar." << endl;
WSACleanup();
return -1;
}
cout << "Esperando al cliente." << endl;
int size1 = sizeof(struct sockaddr);
cliente = accept(servidor, (struct sockaddr *)&client, &size1);
if(cliente == 0){
cout << "Error al aceptar." << endl;
WSACleanup();
return -1;
}
send(cliente, tamanio, sizeof(tamanio), 0);
Sleep(1000);
send(cliente, buffer, size, 0);
cout << "Enviados: " << size << " bytes." << endl;
closesocket(servidor);
input.close();
WSACleanup();
return 0;
}
Codigo del programa que recibe la imagen
Código:
#include <iostream>
#include <winsock.h>
#include <fstream>
using namespace std;
int main(){
int iResult;
struct sockaddr_in client;
struct hostent *hp;
WSADATA wsaData;
SOCKET cliente;
char buffer[512];
ofstream output("salida.jpg", ofstream::binary);
cout << "Recibir imagen" << endl;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult){
cout << "Error al iniciar." << endl;
return -1;
}
hp=(struct hostent*)gethostbyname("localhost");
if(!hp){
cout << "No se puede conectar con servidor." << endl;
return -1;
}
cliente = socket(AF_INET, SOCK_STREAM, 0);
if(cliente == INVALID_SOCKET){
cout << "No se puede crear socket." << endl;
return -1;
}
memset(&client, 0, sizeof(client));
memcpy(&client.sin_addr, hp->h_addr, hp->h_length);
client.sin_family = hp->h_addrtype;
client.sin_port = htons(6000);
if(connect(cliente, (struct sockaddr *)&client, sizeof(client))==SOCKET_ERROR){
cout << "Error al conectar" << endl;
WSACleanup();
return -1;
}
cout << "Recibiendo datos... " << endl;
recv(cliente, buffer, sizeof(buffer), 0);
long int li;
li = atol(buffer);
char *buffer2 = new char[li];
Sleep(1000);
recv(cliente, buffer2, li, 0);
output.write(buffer2, li);
delete[] buffer2;
cout << "Listo!" << endl;
closesocket(cliente);
output.close();
WSACleanup();
return 0;
}