This paper is concerned with the finite and infinite horizon optimal control issue for a class of networked control systems with stochastic communication protocols. Due to the limitation of networked bandwidth, only the limited number of sensors and actuators are allowed to get access to network mediums according to stochastic access protocols. A discrete-time Markov chain with a known transition probability matrix is employed to describe the scheduling behaviors of the stochastic access protocols, and the networked systems are modeled as a Markov jump system based on the augmenting technique. In such a framework, both the approaches of stochastic analysis and dynamic programming are utilized to derive the optimal control sequences satisfying the quadratic performance index. Moreover, the optimal controller gains are characterized by solving the solutions to coupled algebraic Riccati equations. Finally, a numerical example is provided to demonstrate the correctness and effectiveness of the proposed results.