views:

58

answers:

1

Hello.

I'm trying to develop a simple IRC bot. First I want to think out a proper design for this project. One of the things I'm wondering about right now is the read mechanism. I develop this bot on a Linux system. (Fedora 12) To read from a socket I use the system call "read()". I plan to use the reading functionality in the following way (code just an example. Not something from the final product):

while (uBytesRead = read(iServerSocket, caBuffer, MAX_MESSAGE_SIZE))
{
   //1. Parse the buffer and place it into a Message structure.
   //2. Add the message structure to a linked list that will act as a queue of message that are to be processed.
}

This code will be run in it's own thread. I choose for this option because I wanted there to be as small of a delay between reads as possible. (writes will be implemented in the same way) This is all slightly based on assumptions, that I would like to clear up. My question is: what if you receive so much data at such a quick rate, that the reading and processing the data (in this case just parsing it) goes slower than the rate at which data comes in. I made the assumption that this data will be buffered by the system. is this a right assumption? And if so:

  1. How big is this buffer?
  2. What happens with incomming data when this buffer gets full?
  3. To make my application protected against spam, how could I best deal with it?

I hope I've explained my issue clear enough.

Thanks in advance.

+1  A: 

IRC uses TCP sockets for networking. Linux/Posix TCP sockets have a data buffer for sending and another one for receiving. You can resize the buffers with setsockopt() and SO_SNDBUF/SO_RCVBUF.

TCP has flow control so when a receive buffer is getting full the OS will send a congestion notice. Received packets that didn't fit in the buffer will not be acknowledged by the receiver and will be eventually retransmitted by the sender.

So that's not to worry. What matters is what does the sender program when its socket's send buffer gets full. Some programs will close the socket, others would just discard written data and try again, while others might buffer internally.

alecco
Aha ok that clears things up. The IRC protocol states some rules regarding flood control so i think the issue of a send buffer getting full on the server side won't be much of an issue. I was just wondering how it works. I've looked at the MAN page for read but that didn't ge me much wiser on this subject, so thanks a lot!