SocketImplementation problem with PUN+


Purchased the PUN+ plugin and very disappointed cause it does not work :(

The standard code:

_peer = new PhotonPeer(Instance, ConnectionProtocol.Tcp);
_peer.Connect(serverAddress, applicationName);

Runtime results:

Connect() failed, because SocketImplementation or socket was null. Set PhotonPeer.SocketImplementation before Connect().

NullReferenceException: Object reference not set to an instance of an object
ExitGames.Client.Photon.EnetPeer.DispatchIncomingC ommands ()
ExitGames.Client.Photon.PhotonPeer.DispatchIncomin gCommands ()
ExitGames.Client.Photon.PhotonPeer.Service ()

And googling "PhotonPeer.SocketImplementation" yielded nothing. I've tried a piece of code from NetworkingPeer.cs below, it helps to avoid runtime error but also Connect() fails silently.

Debug.Log("Using C# Socket");
this.SocketImplementation = typeof(SocketUdp);
Debug.Log("Using class SocketUdpNativeDllImport");
this.SocketImplementation = typeof(SocketUdpNativeDllImport);
Debug.Log("Using class SocketUdpNativeDynamic");
this.SocketImplementation = typeof(SocketUdpNativeDynamic);
Debug.Log("Using class SocketUdpNativeStatic");
this.SocketImplementation = typeof(SocketUdpNativeStatic);


  • I will take a look immediately. I'm sure this is a minor issue only.
    Which platform gives you this issue?
  • Err. Please note: TCP is not supported by PUN+ at the moment.
    We use a reliable UDP protocol, to get as good or even better results and only this is supported in PUN+ so far.
  • Hey Tobias,

    I've tried _peer = new PhotonPeer(Instance, ConnectionProtocol.Udp) with no luck.
    Unity Editor, WebPlayer, Android and iOS - same error.
  • PUN and PUN+ handle creation of the PhotonPeer for you. Everything you usually need is in the class PhotonNetwork.
    Please let me know what you try to achieve. Maybe this is a misunderstanding.

    Also: Please verify things are working by creating a empty, new project. Import PUN+ into it and export our demos to either platform.
    It should work this way.
    I can help you with your code and ideas but it would be easier if I understood why you create a PhotonPeer. This is quite low level.

    Check out the PUN Marco Polo Tutorial to get a start how PUN is usually used: ... o_Tutorial
  • We have a game project in development and has been using Photon from the beginning. As we don't need Lobby, Rooms, players management etc. we do not use high-level wrappers. Just Peer on the client side and custom deserialization on the server. Everything works fine, except for mobile export requiring Unity Pro. I've bought PUN+ as it said that Pro is not required anymore.

    I've checked the demo and it's a generic MMORPG, we're in a different genre (multiplayer puzzle) and do not need all those high-level things. PhotonNetwork contains all those MMORPG abstractions. The closest match I've found is RPC, however it still depends on those rooms and player Id's?

    We'd like to keep using Peer and our current codebase, but with Android/iOS builds.
  • MMORPG? I don't know which demos you look at. If you say so.

    PUN+ was built to run Photon Unity Networking primarily, so it's not tested without that layer. Due to that, no clear example was provided to just get a PhotonPeer running with it.
    I think it should not be a big issue but I have to find the time to do it.

    For the time being, you should assume we get this working, too.
  • The demo you've suggested - Marco Polo, it's an excellent framework for MMORPG game - with characters moving around interacting with the world. But we're making very different kind of game.

    The peer itself is running ok, it seems some socket-related initialization code is missing. I've blindly copy/pasted some init code from NetworkingPeer.cs and it deals with the SocketImplementation error, but unfortunately afterwards the peer silently fails to connect to the server. After call to peer.Connect(serverAddress, applicationName) it shows no error but in ~10 seconds timeouts.

    My guess is that this.SocketImplementation = typeof(SocketUdpNativeDynamic) should be changed to the correct PUN+ implementation, but I have no clue how it's called.
  • Maybe the attached file helps you already.
    From what I can see, all that's needed to setup the PhotonPeer is in Start().
    Edit: I added OnApplicationQuit() with a Disconnect() call. I am not sure if it was my Unity being pretentious but in worst case missing the disconnect seems to lock up the Editor. I'll look into that.

    Let me know how it does for you.
    In your project, you could remove anything PUN and keep only the "Photon" labelled stuff in Assets\Plugins\.
  • Cool, made it work!

    The attached script is helpful, and think it should be covered in the docs that this script, ConnectionProtocol.Udp and UDP-related photon server, crossdomain and firewall options should be tweaked properly.

    Thanks, PUN+ is a real money saver for indie team.
  • Thanks for confirming it works not only on my machine :)
    I think this script could get a place in the UtilityScripts folder or so. This can be improved for sure.

    Oh. One more thing: This assembly causes an exception when you call DispatchIncomingCommands before you connected. This will be fixed in the next release. At the moment, make sure to call Service or DispatchIncomingCommands only after connecting.

    Let us know what your team is up to in the Any Topic and Chat forum.
  • Tobias, could you please also confirm that PhotonPeer(Instance, ConnectionProtocol.Udp) creates a reliable UDP peer, essentialy emulating TCP?
  • WildMaN wrote:
    Tobias, could you please also confirm that PhotonPeer(Instance, ConnectionProtocol.Udp) creates a reliable UDP peer, essentialy emulating TCP?
    I very much doubt that he would confirm that, because its simply not true.
    It creates a UDP peer, that is capable of sending messages reliably, but its not a reliable UDP peer, as in opposition to a TCP one is still able to send messages unreliably. You can decide on a per operation base if you want to send a certain operation reliably or unreliably and you could even send the same operation sometimes unreliably and sometimes reliably. So a PhotonPeer that uses UDP is capable of sending both, unreliable AND reliable messages, without any need to reconnect or even recreate the peer.
  • That's what I was asking about, thank you. So it means that the method:
    OpCustom (byte customOpCode, Dictionary<byte, object> customOpParameters, bool sendReliable)
    works with PUN+ the same way it does in vanilla Photon.
  • Yes, that's correct.
  • Hello,

    I am doing Turn Based Multiplayer Game using Photon. Initially I am using the free Package of Photon that is PUN. But there is coming some problem while joining the room.

    That is :- Join failed on Game Server. Changing back to MasterServer photon unity

    As I am a Developer so I test the Game many time, means i create rooms number of times with different user accounts. So i after doing some RnD work i got some little bit success. I got 2 points for failure of of this issue.
    One is that there may be number of users or rooms is created thats why this issue comes
    Second one is when i left the room i do not call any method to disconnect the Client and i set the limit of max player 2 and when i rejoin the room it failed.

    For the 2nd Problem i find the solution that i have increased the max players value. It works fine for few days but again same issue comes. Then i come back to first reason. So finally i purchased a PUN+

    I import the PUN+ Package first in empty Project then export package of that and import in my own project. There is no error comes and i was happy but when i start a game it does not work at all. Error comes :- Connect() failed, because SocketImplementation or socket was null. Set PhotonPeer.SocketImplementation before Connect().

    Then i find this form and i downloaded the script setup and connect and import in the Plugins. But again this problem is not solved but one more issue come that was of sockettype invalid.

    Now i was totally stucked in this Game due to Photon Server. My deadline of the Projects is coming in 2 days. Please suggest me some way.

    Thanks in advance

  • Sumeet:
    I think the issue with the SocketImplementation is due to an issue with importing PUN+.
    Do you need PUN+? It helps you export from Unity Free to iOS and Android. If you have Unity Pro for both platforms, you can make your life easier by not importing PUN+.
    If you need it, please follow this guide and install the plugins (.dll, .pdb, .a and .so files) over what you have. Read: "PUN Free to PUN+ Upgrade" on: ... own-issues

    The "Join Failes on Game Server" is unrelated to the sockets. You clearly have a connection and get an error response from the server.
    The case that a game is full on the game server can happen but it should not happen so often. It happens WAY MORE when all clients which look for a game go into the lobby, select one of the first few listed rooms and try to join that. This is logical: The room fills quickly and a lot of concurrent Join requests are produced. Not all can succeed.
    Switch to Random Matchmaking. Use JoinRandomRoom. This way, the server can pick a suitable room and avoid racing conditions for your join cases.

    The Boxes Demo in PUN uses this approach and I never have that issue. It could happen that 2 players try to find a game at the same time and both fail if none is available. This is a typical testing scenario where both clients end up in different rooms. This should not be tragic though and in real life conditions, this is highly improbable.
    Don't join the lobby in that case. Set PhotonNetwork.autoJoinLobby = false in all clients in some script's Start() method.