The design of rate allocation and queue length control in computer networks is treated as a stochastic optimal control problem. The performance index is chosen to achieve the twin objectives of minimising queue length fluctuations and fully utilising the available bandwidth. Simple, practically realisable optimal control schemes are obtained for both LANs and WANs. An adaptive scheme is proposed where the auto-regressive parameters of the traffic, needed for gain calculations, are estimated by an LMS algorithm. Discrete event simulations are carried out to verify the fluid-flow models used in developing the controllers, to compare their performance against PI controllers proposed previously, and to study the effect of self-similar traffic. Two key results are obtained. First, queue-length fluctuations, and hence potentially packet losses, are much smaller for the optimal feedforward controller than for the PI controller. Second, in contrast to uncontrolled queues, the queue length variance decreases with increasing Hurst parameter for self-similar traffic.