Can ships be defended from Iranian attacks in the Gulf?

No real attempt has been made to protect tankers and cargo ships, making them vulnerable.

An oil tanker loads gas in Assaluyeh seaport at the Persian Gulf, 1,400 km (870 miles) south of Tehran, Iran May 27, 2006. (photo credit: MORTEZA NIKOUBAZI/ REUTERS)
An oil tanker loads gas in Assaluyeh seaport at the Persian Gulf, 1,400 km (870 miles) south of Tehran, Iran May 27, 2006.
Iran’s attacks on ships in May and June 2019, as well as the recent incident involving an Israeli-owned ship in the Gulf of Oman, continue to raise questions about the vulnerability of shipping to attacks from both states and non-state terrorists. 
Tankers and cargo ships are particularly vulnerable, and there has been no real attempt to find a way to protect them or even punish those who might attack these types of vessels.
When six ships were targeted in May and June of 2019, the US and European countries sought to increase maritime security in the Gulf. That seemed to reduce the attacks, although it was unclear if Iran was deterred or merely shifted tactics, not wanting too much evidence of its involvement to come to light.
Although the US blamed Iran, many countries held back from punishing Iran for the actions of those two months. It appears this sent a message that Iran continued to enjoy impunity. It is not clear if the February 26 incident involving the Israeli-owned Helios Ray has had any consequences for the perpetrators.
Attacks on shipping are not a new phenomenon. During the Iran-Iraq war in the 1980s, there was a “tanker war” that developed between the two countries that threatened to suck the Gulf and the US into conflict.
Some 40% of the world’s oil came through the Straits of Hormuz in the 1980s, leading the US to launch Operation Ernest Will in 1987 in response to the tanker war, to protect the oil supply and reflag tankers with US flags to stop Iran’s attacks.
Later ships were plagued by piracy off the coast of Somalia, leading to another round of security initiatives to protect shipping. 
The most well-known example was the Maersk Alabama hijacking in 2009, when the vulnerability of ships was revealed once again as pirates in relatively small boats were able to take over many large ships.
A decade later it is unclear if much has been learned, despite better technologies to protect shipping. Militaries have better sensors and longer-range drones that can aid in protecting naval vessels. However, providing thousands of cargo ships and tankers with better defense systems is impractical. Positioning a paucity of naval assets off various shores or in waterways to provide security is also often not possible. 
Israel, for instance, has taken delivery of the Sa’ar 6 corvette, which is designed to protect the exclusive economic zone off the coast. But even advanced vessels like this take years to build and equip, and even then there are only a handful of them.
It appears that for the foreseeable future the kinds of threats that Iran, or Iran’s allies such as the Houthis, are developing will make ships vulnerable to various kinds of attacks. In addition, it means that countries like Iran can exploit the vastness of the sea to continue to move illicit cargo and conduct ship-to-ship transfers of oil and other products.