|
el codigo de c es el siguiente y es solo un ejemplo :
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <windows.h>
#include <winsock.h>
//---------------------------------------------------------------------------
char SendBuff[2048],RecvBuff[2048];
#pragma argsused
int main(int argc, char* argv[])
{
WSADATA wsaData;
SOCKET conn_socket,comm_socket;
//SOCKET comunicacion;
struct sockaddr_in server;
struct sockaddr_in client;
struct hostent *hp;
int resp,stsize;
//Inicializamos la DLL de sockets
resp = ::WSAStartup(MAKEWORD(2,2),&wsaData);
if( resp )
{
printf("Error al inicializar socket\n");
getchar();
return resp;
}
//Obtenemos la IP que usará nuestro servidor...
// en este caso localhost indica nuestra propia máquina...
hp = (struct hostent *)::gethostbyname("localhost");
if (!hp )
{
printf("No se ha encontrado servidor...\n");
getchar();
::WSACleanup();
return ::WSAGetLastError();
}
// Creamos el socket...
conn_socket = ::socket(AF_INET,SOCK_STREAM, 0);
if ( conn_socket==INVALID_SOCKET )
{
printf("Error al crear socket\n");
getchar();
::WSACleanup();
return ::WSAGetLastError();
}
// Asociamos ip y puerto al socket
memset(&server, 0, sizeof(server)) ;
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(6000);
resp = ::bind(conn_socket, (struct sockaddr *)&server, sizeof(server));
if ( resp == SOCKET_ERROR)
{
printf("Error al asociar puerto e ip al socket\n");
::closesocket(conn_socket);
::WSACleanup();
getchar();
return ::WSAGetLastError();
}
// Listen ...
if ( ::listen(conn_socket, 1) == SOCKET_ERROR)
{
printf("Error al habilitar conexiones entrantes\n");
::closesocket(conn_socket);
::WSACleanup();
getchar();
return ::WSAGetLastError();
}
// Aceptamos conexiones entrantes
printf("Lite HTTP Server v 0.01 , esperando conexiones entrantes... \n");
stsize = sizeof(struct sockaddr);
comm_socket = ::accept(conn_socket,(struct sockaddr *)&client,&stsize);
if ( comm_socket == INVALID_SOCKET)
{
printf("Error al aceptar conexión entrante\n");
::closesocket(conn_socket);
::WSACleanup();
getchar();
return ::WSAGetLastError();
}
// Identificamos al cliente ...
printf("Conexión entrante desde: %s\n", inet_ntoa(client.sin_addr));
// Como no vamos a aceptar más conexiones cerramos el listen socket ...
::closesocket(conn_socket);
// Recibimos la peticion HTTP ...
//
printf("HTTP Request... \n\n");
::recv (comm_socket, RecvBuff, sizeof(RecvBuff), 0);
printf("Datos recibidos:\n %s \n", RecvBuff);
//Enviamos la respuesta HTTP...
//
char httpStartLine[] = "HTTP/1.1 200 OK\r\n" ;
char httpHeaderDate[] = "Date: Vie, 17 Set 2010 13:05:59 GMT\r\n" ;
char httpHeaderContentType[] = "Content-Type: text/html\r\n" ;
char httpHeaderContentLength[] = "Content-Length: %d\r\n" ;
char blankLine[] = "\r\n" ;
char httpBody[] = "<html><body><h1>LiteHttp ( one shot http server )"
" Welcome Page</h1></body></html>" ;
char httpResponse[ 4096 ] = { "\0" } ;
strcpy( httpResponse, httpStartLine ) ;
strcat( httpResponse, httpHeaderDate ) ;
strcat( httpResponse, httpHeaderContentType ) ;
int contentLenght = strlen( httpBody ) ;
sprintf( httpHeaderContentLength, "Content-Length: %d\r\n", contentLenght ) ;
strcat( httpResponse, httpHeaderContentLength ) ;
strcat( httpResponse, blankLine ) ;
strcat( httpResponse, httpBody ) ;
// Respuesta Enviada ...
printf("Respuesta enviada ...\n\n" );
printf("%s\n\n", httpResponse );
printf("Enviando http response... \n\n");
::send (comm_socket, httpResponse, sizeof(httpResponse), 0);
getchar();
// Cerramos el socket de la comunicacion
::closesocket(comm_socket);
// Cerramos liberia winsock
::WSACleanup();
return 0 ;
}
|