Jose Solares

Sample Code for RDT Server

by on Jun.01, 2005, under english, Linux


Note:This code expects to be using sockets blocking I/O request, to use non blocking I/O we should use libevent or a similar solution (like threads), this is outside of the scope of this code. It also assumes that we already have the server listening on a port and that we have already accepted a client connection `cliente_socket`. RCVBUFFER is 256.



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");

if (message_size > 5 && bufer<sup><a href="#fn4894584045a347c453015c">0</a></sup> -128) counter = buffer3; i = (buffer0 -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 (buffer0 0x50) { char respond[] = {0x51}; 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] 0x0d && 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...