I may be missing something, but based on my reading of the Seat61 website and a look at the generally very accurate Deutsche Bahn website, I'm not sure there is a direct rail link between Belgrade and Sofia. DB shows that you would actually go through Thessaloniki on the way to Sofia!
On the other hand, Rome2Rio does show a train link between Belgrade and Sofia, with changes in Dimitrovgrad and Nis. If those trains are running, you should be able to find the individual legs on the DB website. I haven't checked, but Rome2Rio (not always accurate about this, or about fares!) indicates that two of the three trains run only once a day, so if something went wrong, this very long trip would become interminable.
Rome2 Rio indicates that a bus would be faster, at least between Sofia and Belgrade, and during my 2015 trip through the Balkans I ended up on a lot of buses. They were modern and comfortable. Toilet/smoking breaks were taken about every 2 to 2-1/2 hours. I found it somewhat difficult to find complete, accurate bus schedules on line. On two occasions I stopped in at the local tourist office to check on a future travel leg (not departing from the city where I was currently staying), and the staffer picked up the telephone and called the bus station in the next city, rather than looking for information online. It helps to have time and be flexible. You might run into problems on Sundays and holidays, maybe even on Saturdays.
I figured I covered about 30 mph, whether traveling by bus or train, so be prepared for that. I generally bought my bus or train tickets out of town when I arrived in town, or two days ahead if I was staying somewhere for a while. I've been on some pretty full cross-border buses, so I recommend getting the ticket as soon as you know what bus you want to take.
By the standards of western Europe, both the buses and trains in the Balkans are reasonably priced. I did not do any night trains so cannot comment on the cost of couchettes or sleepers.
Just in case it helps, I'll list the pertinent legs of my trip and the mode of transportation I used. I'm skipping over the short legs within each country.
Belgrade - Timisoara (Romania): Train, changing in Vrsac (800 dinar, probably higher now)
Bucharest (Rom) - Veliko Tarnovo (Bulgaria): Train (88 lei)
Sofia - Nis (Serbia): Bus (24 lev)
I spent just one night in Sofia (not the most interesting place in Bulgaria), at the Hotel Budapest. I have absolutely no memory of that hotel, which means it was at least acceptable by my budget-hotel standards. Google Maps suggests it's just over 1/2 mile from the train station.
I also spent the night in Nis, Serbia, at the Best Western My Place. This spot is considerably farther from the train station, a bit over 1-1/2 miles. The BW, as you would imagine, is a western-style hotel. Google shows one hotel about a mile from the train station (Hotel Veneda).
In Belgrade I stayed at the Beograd City Hotel very near the train station and a short walk from the bus station. It was acceptable but not fancy.