Is it possible, legally, for code to be publicly available online, but have copyright protection?
Yes. Having access to source code does not give permission to redistribute. Here is an analogy: Think about photos on Flickr, just because you can view them doesn't mean you can redistribute them.
For example: Several hardware companies will make code available to use with their hardware, but the license will say only their hardware. It doesn't meet the OSI definition of open source, and you can use it in a limited fashion.
Why are you asking? Is this something you've seen or what to do.
You may be conflating open-source with unlicensed. Most open-source code is licensed. As to picking a license, well we have posts for that. Copyright is typically automatic but varies from country to country and is somewhat different from licensing which involves copyright but is not itself a copyright. You're protected by copyright even if your code is publicly available as though it were a book or blog post most of the time. If you want to grant use of your code, you pick a license and go.
under the berne convention all source code has copyright protection in signatory countries. The protections this actually give vary from country to country, but the wholesale copying of the work is forbidden. There is not even a requirement to mark the work with a (c) symbol to obtain this protection.
However, the owner of the copyright can offer a license to people viewing the code granting entitlement to perform actions that otherwise would be reserved to the copyright holder.
This is how the GPL works. copyright is retained by the creator and everyone else is offered a licensed to use and make derived works.
In your specific case, I would question what you are wanting to achieve. Having copyright protection, and enforcing your rights are two very different things. For example, to enforce your rights, you would need to know that copying had occured, and have a way to prove it. Copyright protection does not prevent someone else coming up with the same idea independently. This is particularly problematic in software development, with the common software methodologies, common shared libraries etc, it is highly likely that two software developers when faced with the same problem would create simular code.