In this paper we present a closed-loop optimal control approach for the online control of a legged robot locomotion, particularly the hopping of a simulated monoped robot. Modeling is done based on the spring loaded inverted pendulum (SLIP) model suggested as the animal and human running gait template. The key idea is to efficiently inject energy to the system so that the monoped can track the desired apex height and forward velocity. The state of the system is observed in the Poincare section at the apex point and the corresponding discrete dynamics is formulated by using available analytical solutions. The goal is then to synthesize an optimal control law which can bring the apex state at any step to the desired state at the next step. We show the controller performance in providing fast and accurate response in the presence of noise and through different scenarios while minimizing the control effort.