Skip to content

Instantly share code, notes, and snippets.

@linadk
Created June 19, 2015 18:37
Show Gist options
  • Save linadk/a3bc0cee6f39bfd80110 to your computer and use it in GitHub Desktop.
Save linadk/a3bc0cee6f39bfd80110 to your computer and use it in GitHub Desktop.
Using the Unity3d Unet LLAPI this will establish a connection and send data. There aren't many examples out there for this currently so I thought I'd write this up.
/// <summary>
/// UNet LLAPI Hello World
/// This will establish a connection to a server socket from a client socket, then send serialized data and deserialize it.
/// </summary>
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class NetworkTest : MonoBehaviour {
int mServerSocket = -1;
int mClientSocket = -1;
int mClientConnection = -1;
int mMaxConnections = 10;
byte mChannelUnreliable;
byte mChannelReliable;
bool mHostClientInitialized = false;
bool mClientConnected = false;
// Use this for initialization
void Start () {
// Build global config
GlobalConfig gc = new GlobalConfig();
gc.ReactorModel = ReactorModel.FixRateReactor;
gc.ThreadAwakeTimeout = 10;
// Build channel config
ConnectionConfig cc = new ConnectionConfig();
mChannelReliable = cc.AddChannel (QosType.ReliableSequenced);
mChannelUnreliable = cc.AddChannel(QosType.UnreliableSequenced);
// Create host topology from config
HostTopology ht = new HostTopology( cc , mMaxConnections );
// We have all of our other stuff figured out, so init
NetworkTransport.Init(gc);
// Open sockets for server and client
mServerSocket = NetworkTransport.AddHost ( ht , 7777 );
mClientSocket = NetworkTransport.AddHost ( ht );
// Check to make sure our socket formations were successful
if( mServerSocket < 0 ){ Debug.Log ("Server socket creation failed!"); } else { Debug.Log ("Server socket creation successful!"); }
if( mClientSocket < 0 ){ Debug.Log ("Client socket creation failed!"); } else { Debug.Log ("Client socket creation successful!"); }
mHostClientInitialized = true;
// Connect to server
byte error;
mClientConnection = NetworkTransport.Connect( mClientSocket , "127.0.0.1" , 7777 , 0 , out error );
LogNetworkError( error );
}
void SendTestData(){
// Send the server a message
byte error;
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter f = new BinaryFormatter();
f.Serialize ( stream , "Hello!" );
NetworkTransport.Send ( mClientSocket , mClientConnection , mChannelReliable , buffer , (int)stream.Position , out error );
LogNetworkError ( error );
}
/// <summary>
/// Log any network errors to the console.
/// </summary>
/// <param name="error">Error.</param>
void LogNetworkError(byte error){
if( error != (byte)NetworkError.Ok){
NetworkError nerror = (NetworkError)error;
Debug.Log ("Error: " + nerror.ToString ());
}
}
// Update is called once per frame
void Update () {
if(!mHostClientInitialized){
return;
}
int recHostId;
int connectionId;
int channelId;
int dataSize;
byte[] buffer = new byte[1024];
byte error;
NetworkEventType networkEvent = NetworkEventType.DataEvent;
// Poll both server/client events
do
{
networkEvent = NetworkTransport.Receive( out recHostId , out connectionId , out channelId , buffer , 1024 , out dataSize , out error );
switch(networkEvent){
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
// Server received disconnect event
if( recHostId == mServerSocket ){
Debug.Log ("Server: Player " + connectionId.ToString () + " connected!" );
}
if( recHostId == mClientSocket ){
Debug.Log ("Client: Client connected to " + connectionId.ToString () + "!" );
// Set our flag to let client know that they can start sending data and send some data
mClientConnected = true;
SendTestData ();
}
break;
case NetworkEventType.DataEvent:
// Server received data
if( recHostId == mServerSocket ){
// Let's decode data
Stream stream = new MemoryStream(buffer);
BinaryFormatter f = new BinaryFormatter();
string msg = f.Deserialize( stream ).ToString ();
Debug.Log ("Server: Received Data from " + connectionId.ToString () + "! Message: " + msg );
}
if( recHostId == mClientSocket ){
Debug.Log ("Client: Received Data from " + connectionId.ToString () + "!" );
}
break;
case NetworkEventType.DisconnectEvent:
// Client received disconnect event
if( recHostId == mClientSocket && connectionId == mClientConnection ){
Debug.Log ("Client: Disconnected from server!");
// Flag to let client know it can no longer send data
mClientConnected = false;
}
// Server received disconnect event
if( recHostId == mServerSocket ){
Debug.Log ("Server: Received disconnect from " + connectionId.ToString () );
}
break;
}
} while ( networkEvent != NetworkEventType.Nothing );
}
}
@AronDavis
Copy link

Thank you for this!

@AronDavis
Copy link

Any idea how to connect from another machine?

@ntwiles
Copy link

ntwiles commented Oct 12, 2015

Seconded, thanks a lot! The documentation wasn't very informative, so this helped quite a bit. I second AronDavis' question though. How does a separate client device get access to the client socket id and channel id to initiate the connection? I feel like I'm missing something obvious here.

@ArmandCloud
Copy link

Hello ,
In this example, "Server Device" is also a "client device"?, or we should connect to server from "server device" as client.

Regards,

@feenhoernchen
Copy link

Just the example I needed! Thanks!

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