Trouble with NetworkTransport LLAPI Client/Server implementation
Hello,
I have been digging through the Unity LLAPI trying to combine the various bits provided throughout the very sparse documentation as well as blog posts to get a basic Client/Server setup going, but I am running into a pretty frustrating error (I am able to connect, but when I try sending a message I am told I have a WrongConnection).
Here is the code for my client and server: https://gist.github.com/jamiltron/f27516f54dc1b3ab14fc
I explain the situation at the bottom of that gist, but the tl;dr is:
I have 2 instances of Unity running on the same network (on different machines), one running the Server and one running the Client. The Client is pointed to the internal ip of the Server.
When I call the Connect method on the Client, I see a resulting connection message on both sides.
Afterwards, I call SendSocketMessage on the Client, and I get an error telling me that I have a WrongConnection.
After closing the Client I see the resulting disconnection message on the Server.
The documentation leaves me a bit perplexed, so I am not entirely certain what I am doing wrong.
Any help is greatly appreciated!
It's worth noting that NetworkTransport.AddHost seems to be the only way to create a socket. So even though the na$$anonymous$$g of the method is odd, it seems like it must be called on both the client and the server to be able to communicate between the two.
Hmm, it seems as if I have to "burn off" a WrongConnection error. I was able to get my code to work, but I have to call NetworkTransport.GetConnectionInfo with the hostId and connectionId. When I do that I get a WrongConnection error, but then my SendSocket$$anonymous$$essage goes through. I am not sure why this is the case...
I had the same issue as you (and a gargantuous headache). The problem is when you call in your client (Adding a host, topology etc) NetworkTransport.Connect(SocketId, EndPoint, Port, 0, out errorByte);
this return true, but its meaning its not that you are connected, it is that you sent correctly the conect message, you need to wait until you receive a Connect event from the server in your client, and then you are truly connected.
you need to call from the client:
Config = new ConnectionConfig();
int ReliableChannelId = Config.AddChannel(QosType.Reliable);
int NonReliableChannelId = Config.AddChannel(QosType.Unreliable);
Topology = new HostTopology(Config, 10);
SocketId = NetworkTransport.AddHost(Topology);
byte errorByte;
ConnectionId = NetworkTransport.Connect(SocketId, EndPoint, Port, 0, out errorByte);
Then in update call to receive messages from that host (I reccommend this aproach, if you have in the same machine client and server)
int recConnectionId;
int recChannelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
recNetworkEvent = NetworkTransport.ReceiveFromHost(SocketId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
switch (recNetworkEvent)
{
case NetworkEventType.ConnectEvent:
Debug.Log("Here you are truly connected");
break;
I upload some examples to Github:
Answer by Daniel-mayles · Nov 25, 2015 at 11:33 AM
I had the same problem not too long ago. The Client needs this code as well as the server, else the client doesn't know it's connected.
switch (networkEvent)
{
case NetworkEventType.ConnectEvent:
break;
}
Yeah, its interesting to me that this isn't handled under the hood by the llapi, since it is keeping track of things like sets of connection and host ids.
Your answer
Follow this Question
Related Questions
Bullet has wrong direction on client 0 Answers
UNET Creating dynamically created objects on server or on client 0 Answers
Sending NetworkPlayer object with ClientRpc 0 Answers
How do I make an object move smoothly? 1 Answer
Adding Clients after Log in 1 Answer