Skip to content

Instantly share code, notes, and snippets.

@jamiltron
Created November 23, 2015 00:30
Show Gist options
  • Save jamiltron/f27516f54dc1b3ab14fc to your computer and use it in GitHub Desktop.
Save jamiltron/f27516f54dc1b3ab14fc to your computer and use it in GitHub Desktop.
Unity LLAPI networking client/server
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkClient : MonoBehaviour {
public string host = "127.0.0.1";
public int port = 8000;
private int hostId;
private int connectionId;
private ConnectionConfig config;
private HostTopology hostTopology;
private byte channelId;
void Start() {
NetworkTransport.Init();
}
public void Connect() {
byte error;
config = new ConnectionConfig();
channelId = config.AddChannel(QosType.ReliableSequenced);
hostTopology = new HostTopology(config, 1);
hostId = NetworkTransport.AddHost(hostTopology, port);
connectionId = NetworkTransport.Connect(hostId, host, port, 0, out error);
NetworkError networkError = (NetworkError) error;
if (networkError != NetworkError.Ok) {
Debug.LogError(string.Format("Unable to connect to {0}:{1}, Error: {2}", host, port, networkError));
} else {
Debug.Log(string.Format("Connected to {0}:{1} with hostId: {2}, connectionId: {3}, channelId: {4},", host, port, hostId, connectionId, channelId));
}
}
public void SendSocketMessage() {
byte error;
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, "Hello");
int bufferSize = 1024;
NetworkTransport.Send(hostId, connectionId, channelId, buffer, bufferSize, out error);
NetworkError networkError = (NetworkError) error;
if (networkError != NetworkError.Ok) {
Debug.LogError(string.Format("Error: {0}, hostId: {1}, connectionId: {2}, channelId: {3}", networkError, hostId, connectionId, channelId));
} else {
Debug.Log("Message sent!");
}
}
}
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkServer : MonoBehaviour {
public int port = 8000;
private HostTopology topology;
private ConnectionConfig config;
private int connectionId;
private int hostId;
void Start() {
NetworkTransport.Init();
config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);
topology = new HostTopology(config, 10);
hostId = NetworkTransport.AddHost(topology, port);
Debug.Log("Server started on port" + port + " with id of " + hostId);
}
void Update() {
int recHostId;
int recConnectionId;
int recChannelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType networkEvent = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
NetworkError networkError = (NetworkError) error;
if (networkError != NetworkError.Ok) {
Debug.LogError(string.Format("Error recieving event: {0} with recHostId: {1}, recConnectionId: {2}, recChannelId: {3}", networkError, recHostId, recConnectionId, recChannelId));
}
switch (networkEvent) {
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
Debug.Log(string.Format("incoming connection event received with connectionId: {0}, recHostId: {1}, recChannelId: {2}", recConnectionId, recHostId, recChannelId));
break;
case NetworkEventType.DataEvent:
Stream stream = new MemoryStream(recBuffer);
BinaryFormatter formatter = new BinaryFormatter();
string message = formatter.Deserialize(stream) as string;
Debug.Log("incoming message event received: " + message);
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("remote client " + recConnectionId + " disconnected");
break;
}
}
}
@jamiltron
Copy link
Author

I have the server and client running in two different instances of Unity. The server starts up, and the client has two buttons: "Connect", and "Send" that perform the functions Connect and SendSocketMessage. The host I am using in the client is the internal ip of the instance running the server, and both are one the same network.

When I hit "Connect" in the client I see a valid connection message on both the server and the client (the connectionId is listed as 1, which I then use later in the SendSocketMessage method). When I hit "Send" afterwards, I get the following messages on the client:

Attempt to send to not connected connection {1}
UnityEngine.Networking.NetworkTransport:Send(Int32, Int32, Int32, Byte[], Int32, Byte&)
NetworkClient:SendSocketMessage() (at Assets/Scripts/NetworkClient.cs:48)
UnityEngine.EventSystems.EventSystem:Update()

Error: WrongConnection, hostId: 0, connectionId: 1, channelId: 0
UnityEngine.Debug:LogError(Object)
NetworkClient:SendSocketMessage() (at Assets/Scripts/NetworkClient.cs:52)
UnityEngine.EventSystems.EventSystem:Update()

Afterwards if I close out of the client I see the proceeding disconnect message on the server, telling me that they are indeed connected.

What am I doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment