We investigate packet flow in computer networks within the framework of statistical physics by using numerical simulations. As mathematical models for packet routing, we present a spin model with lattice gas spins and the one with Ising spins. Then we propose dynamic programming for optimal routing control of packet flow by using the two spin models. This is a kind of goal-directed learning for taking into account of time-dependent environment for the packets. Next we investigate a congestion problem by using the model with lattice gas spins when the packets are not sent to nodes at which their buffers are already full up with packets. Since we have found serious congestion in the packet flow, we then propose reinforcement learning for avoiding the congestion and have performed simulations on several networks including small world networks, scale free networks and so on.