Jose Solares

Codigo de Ejemplo para Servidor RDT

by on Jun.01, 2005, under Linux

Nota:Este codigo esta hecho para usar blocking I/O de sockets, para utilizar non blocking I/O debemos utilizar libevent, eso esta fuera del ambito de este codigo. Y asume que ya tenemos al servidor “escuchando” en un puerto y que ya aceptamos la conexion de un cliente `cliente_socket`.




char buffer[RCVBUFFER + 2];
buffer[RCVBUFFER] = 0;
char response[] = {2,6,1,0,7,1,0,2,1,6,0x81};
int message_size,i,j;
char counter = 0;

if (( message_size = recv(client_socket, buffer, RCVBUFFER, 0)) < 0 ) err_handler ("recv() failed");

if ( message_size 17 ) { if ( send(client_socket, response, 11, 0) != 11 ) err_handler ("send() failed");
} else { printf ( "- %d - \n", message_size ); err_handler ("Protocol Error");
}

printf ("Starting...\n");

for (;;) { if (( message_size = recv(client_socket, buffer, RCVBUFFER, 0)) < 0 ) err_handler ("recv() failed"); counter = ( buffer[0] == -128 && message_size > 5 )? buffer[3] : counter; i = (buffer[0] == -128)? 5 : 0; buffer[message_size + 1] = '\0'; buffer[message_size] = '\n';

//printf ("Capturing %d bytes\n",message_size); fprintf (stderr, "%s",&buffer[i]); //printf ("Current Counter : %d\n",counter); if (message_size 1) { if (buffer[0] == 0×50) { char respond[] = {0×51}; if ( send (client_socket, respond, 1, 0) != 1) err_handler(“send() failed”); } } else { i = message_size – 1; if (buffer[i-2] == 0×20 && buffer[i-1] == 0×0d && buffer[i] == 0×0a) { char respond[] = {0×40, 0×05, 0×01, ++counter, 0×81}; if ( send (client_socket, respond, 5, 0) != 5 ) err_handler(“send() failed”); } } /* Do Something with the Data we received */ }
:, , , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...