Return-Path: william@bourbon.usc.edu Delivery-Date: Sun Sep 7 09:03:25 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m87G3Pw8008200 for ; Sun, 7 Sep 2008 09:03:25 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m87G2Ba6013018 for ; Sun, 7 Sep 2008 09:02:11 -0700 Message-Id: <200809071602.m87G2Ba6013018@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: 551 warmup-1 Date: Sun, 07 Sep 2008 09:02:11 -0700 From: Bill Cheng Someone wrote: > So do we really need to use select(). From what I read select() > is used to get over the blocking accept. But here since we are > forking a new process for every connection there is no blocking, > we do not need select. I think what's on the Internet about using select() together with accept() is wrong! But you should not trust me on this one, you should try it and see which way it works. I think I've explained about using select() with recv()/read() during lectures... Recv()/read() is a blocking read. So, if there is no data, your child process can block forever! So, you should make sure that there is data there before you call recv()/read(). To make sure there is data there, you can use select(). Here's something I didn't mention in lectures... If you decide that if a client does not send you a single byte of data in 30 seconds, you want to kill the connection. How would you do this? One easy way is to use select() with a timeout value of 100 milliseconds. You can keep a counter. If select() returns with the status saying that there is data in the socket, you reset the counter to 0. If select() returns with a timeout, you increment the counter by 1. If the counter reaches 300, you close the connection. If you do it this way, you don't need a separate timer! (You will need to do something like this for the final project.) Select() is a good API for networking. I think you should all learn to use it! -- Bill Cheng // bill.cheng@usc.edu ----- Original Message ----- From: Bill Cheng Date: Sunday, September 7, 2008 8:00 am Subject: Re: 551 warmup-1 To: cs551@merlot.usc.edu > Someone wrote: > > > How does the recv() know that the transmission has ended? Should I see the > > data length in the message and run a loop for that many bytes? > > Yes. You should read the common header first and then use > the data length field to determine how many bytes to read. > > > Till now I was doing > > while(ret == 1) { > > .......ret = recv(....); > > } > > You should also call select() before recv(). > -- > Bill Cheng // bill.cheng@usc.edu >