Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

recv() wrapper

Name: Anonymous 2008-03-13 21:43

is there a wrapper for doing things like recv'ing tcp data and putting it into a buffer and then being able to pop things off delimited with \r\n's?  I hate writing that bit twiddling shit.

Name: Anonymous 2008-03-14 5:27

OP, study this and don't post until you understand. It turns \r\n's into C strings but the idea is the same:

char *ns_resevoir;
int NS_BUFLEN;
int ns_curlen;
int a;
...

/* data available on socket */
 if(!(a=NS_BUFLEN-ns_curlen)) {
  /* input buffer full, dump everything and try accumulating again */
  printf("expect a few invalid commands\ninput buffer full");
  ns_curlen=0;
 }
 a=recv(ns_sock,ns_resevoir+ns_curlen,a,0);
 if(!a) {
 is_closed:
  printf("Unexpected close connection\n");
  goto error_1024;
 }
 ns_curlen+=a;
 for(a=0;a<ns_curlen;a++) {
  if(ns_resevoir[a]!=13) continue;
  ns_resevoir[a]=0; /* make ASCIIZ */
  a++;
  if(ns_resevoir[a]==10) a++;
  process_ns_cmd();
  ns_curlen-=a;
  if(ns_curlen)
   memmove(ns_resevoir,ns_resevoir[a],ns_curlen);
  a=0;
 }


I kept stubs moved to the front of the resevoir so that received commands are contiguous, but a circular buffer could work too (although it does miss the point of having a complete string at the front of the buffer ready to process).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List