September 30, 2006

Whoooo!

Dear MSU,

You were our first of hopefully many Big Ten wins in a long time (we haven't won in 24 Big1T1en games). Please don't feel bad.

Love,

Marc

Posted by molson at 2:21 PM | Comments (0)

September 29, 2006

Links for 29 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)

September 25, 2006

Links for 25 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)

September 20, 2006

Links for 20 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)

September 14, 2006

Links for 14 Sep 2006 [del.icio.us]

  • A Lawyer's Advice: How to deal with Cops - "I desire to exercise all my rights guaranteed by the Constitution of the United States and the Constitution of the State of Illinois to be free from your interference with my personal affairs."
Posted by molson at 7:43 PM | Comments (0)

September 12, 2006

Links for 12 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)

September 8, 2006

Links for 08 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)

September 6, 2006

The best part of Dayton

I'm sitting in my hotel room and thinking about what's good about Dayton, OH. About the only thing I can think of is the Speedway station I can see out my window.

Yeah, gas is $2.259 a gallon here. Haven't seen that price in a loooong time.

Other than that? Not much.

UPDATE: It was $2.209 this morning. Can't wait to get back to Chicago.

Posted by molson at 10:13 PM | Comments (0)

September 5, 2006

Links for 05 Sep 2006 [del.icio.us]

  • A handsome, but cranky, coot - "It is my birthright to disagree as strongly as I can with my government because, surprise, I am 100 percent American."
Posted by molson at 7:43 PM | Comments (0)

Using TCP Sockets as Streams (C)

For those that don't care about programming C, you can skip this entry now.

Today I learned something about C sockets that I didn't know before (and in the process fixed a bug in my code). One technique for reading and writing to a socket is to use the stream functions (fprintf(), fgets(), etc.). To do that you must create a stream handle from the socket using fdopen(), and of course make sure you clean up your mess when you are done.

The following code allows you to fprintf() to a socket (some things skipped for brevity):
int main(int argc, char **argv)
{
  int sock;
  FILE *sockfd = NULL;
    :  // setup and accept socket here
  sockfd = fdopen(sock, "w");
  fprintf(sockfd, "pity the foo\n");
    : // do other stuff
}
With most streams you can open them up in read (r), write (w) modes. Also you can change either one to do both read and write by adding a + so that it's either r+ or w+.

Notice I said most.

What I learned today is that if you open up a socket in read/write mode, everything works fine if your data is synchronous and you service things in order (read then write then read then write). However, if you read the stream, then write to the stream, and while you're writing more data is written from the other end, when you go to read again, you will get a read error (most likely ESPIPE (29) "Illegal Seek"). The way to fix this problem is to open up two streams, one for reading (getting data from the other side) and one for writing (sending data to the other side).

int main(int argc, char **argv)
{
  int sock;
  char buf[2048];
  FILE *wsockfd = NULL, *rsockfd = NULL;
    :  // setup and accept socket here
  wsockfd = fdopen(sock, "w");
  rsockfd = fdopen(sock, "r");
  fprintf(wsockfd, "pity the foo\n");
  fgets(buf, 2048, rsock);
    : // do other stuff
}
Of course, you should error check everything, etc. Also don't get the two mixed up (try to read from the write/write to the read) or you will have other issues.

Hopefully this will help someone as it took me a little while to figure it out.
Posted by molson at 4:53 PM | Comments (0)

September 3, 2006

Links for 03 Sep 2006 [del.icio.us]

  • F-14 Last Flight - Nice narrated slideshow of the last F-14 catapulted off of the USS Virginia.
Posted by molson at 7:43 PM | Comments (0)

September 1, 2006

Links for 01 Sep 2006 [del.icio.us]

Posted by molson at 7:43 PM | Comments (0)