Edit: forgot to ask, what defenses were subs given against other subs? Did subs even encounter other subs often enough for this to be a problem?
More than a few times. In the Pacific theater, USN submarines sank the Japanese Ro-45 (although this claim is disputed), I-28, I-29, I-73, I-168, and I-183 as well as the Monsun-Gruppe's U-537 and U-183. Combinedfleet.com 's Sensuikan! page has the tabular record of movement (TROM) for each of these submarines that gives details for each sinking. British submarines managed to sink, or claim to have sunk, some 31 U-boats in both World Wars, as seen in this list. They also sank the Japanese I-34 and the Italian submarines Michele Bianchi, Acciaio, and Pietro Micca. The WWI submarine U-7 has the dubious distinction of being sunk by another U-boat in a case of friendly fire, but was topped by the WWII boats USS Tang and USS Tullibee which sank themselves after torpedo malfunctions caused them to circle back and sank both boats. The Japanese I-176 sank USS Corvina, which was the only US boat sunk by a Japanese submarine and her loss was fictionalized in the book Run Silent, Run Deep (still one of the best sub novel titles). U-boat.net has a list of Allied warships sunk by German submarines, including several Allied submarines like HMS Spearfish.
There were other incidents with minor navies either sinking submarines or themselves being sunk. With one exception, all of these sinkings occurred when the submarine was on the surface. WWI and II submarines were not true submarines, but rather submersibles. They spent most of their time on the surface of the ocean cruising along on their diesel or conventional engines. They used used batteries while underwater which not only could propel the submarine at slower speeds than diesels, but also had a limited running time. Submarines' defenses against attack by other submarines was much like any other threat: dive or somehow get out of the area.
The one known exception to these surface attacks is U-864, which was a Type IX U-boat used as a cargo submarine for Japan. Using ULTRA decripts, the British were able to place HMS Venturer in a position to intercept. The British sub picked up the German boat on its hydrophones and tracked it in the hopes that she would surface. When she did not, Venturer fired a spread of eight torpedoes in a shotgun-type blast based on estimates from her hydrophone data as to where U-864 would be. One of these torpedoes managed to hit its target, and she sank to the bottom with her crew and cargo, including considerable amounts of mercury. To sink a submarine underwater during WWII required a lot of elements to work together ranging from good passive sonar work, intelligence locating the sub's position, as well as a great deal of luck. It was only in the postwar period that submarines gained the upgraded sonar suites and torpedoes that would allow them to hunt down this quarry.
Going to focus on:
what defenses were subs given against other subs?
I haven't figured out how I want to go about this, so it's likely just going to ramble and bounce around. Most of the defenses a submarine has is how the ship is built and used, however there are some active defenses they can use.
Early torpedoes weren't guided and just shot straight out or at an angle using a gyro (but still straight running), so they had to be aimed pretty carefully. This provided some defense for the submarine being attacked because the attacker had to have a good firing solution to hit to begin with. As kieslowskifan provided an example for, one method used to make hitting with torpedos more reliable was firing a spread of shots helping to ensure a hit.
Acoustic torpedoes were later developed which either actively or passively hunt for targets. Passive methods involve the torpedo just listening and then seeking the source of the sound pg. 36. These torpedos are effective against surface targets that make a lot of noise, but the primary defense a submarine has against these is silence. Silence is one of a submarine's biggest defenses to this day; you can't shoot a target that you don't know is there or can't find. I can't speak for early submarines, but more modern submarines (still older than 20 years though) operate at a noise level that passive acoustic devices would not detect unless the crew was really going against protocol. Silent running is the bread and butter of submarine operations and large amounts of research is dedicated to it. Crew behavior, sound isolation of equipment, ship design including the screw, exterior coatings, etc. all contribute to this.
Active methods involve the torpedo sending out sonar signals and then reacting to the received feedback pg. 38. Later advancements include even crazier stuff that I won't go into to try and keep it historical. Defenses to these are similar to defenses that planes might employ against missiles. Expendable countermeasures that make noise, create bubbles, and use jammers can be launched by the submarine to "confuse and defeat the threat". These previous links are to suppliers of modern systems, but they are just more advanced versions of older types of countermeasures needed to defeat more advanced torpedos.
Torpedoes and the firing process make noise, and operators can tell when they have been fired which allows for the defending submarine to take evasive actions pg. 4 like diving, changing depth, changing course, or even attempting to outrun it. Obviously this was easier when torpedos couldn't track, but are still part of the defensive package a submarine would use today. Diving deeper allows a submarine to run faster without cavitating the screw (this causes noise) which means there is a better chance of survival. Evasive maneuvers can also help confuse the torpedo when combined with countermeasures.