Hi,
This happens because your first run makes tcp connection between the openflow switch (in simulation) and your host machine on your listening port (assuming default 6653). When simulation stops, the openflow switch seizes to exist. However, ryu application is unaware of it and keeps the connection (with switch) established for certain amount of time. If you check tcp connections on listening port, you will be able to see it. When you re-run, the openflow switch sends a connection request with same socket (ip and port) as previous connection which is still established at host machine, as a result the connection fails between switch and controller.
As mentioned in the documentation about external controller "Consistent behavior was observed once sufficient time elapses (say 3 to 5 minutes) between any two executions". So you need to wait a certain amount of time to rerun your code. There are two quick fixes that can be helpful if you want to avoid the long wait between runs. Either change the port number of ryu controller, or change the ip address assigned to switch, either will result in a new/different tuple to solve the problem.
Regards,
Sana Mahmood