- Home /
Trouble Connecting Unity with Labview
Im working on creating a game that uses a labview-based goniometer to control the vertical movement of the player in unity. However since the two programs are on separate computers I am attempting to use a direct TCP/IP connection to allow a labview server to write to a unity client. But I cant figure out how to get the server to recognize the client. My code is as follows:
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class tcpclient : MonoBehaviour
{
TcpClient gamesocket;
StreamReader thereader;
StreamWriter thewriter;
NetworkStream thestream;
String goniometer;
string Host = "192.168.2.1";
Int32 Port = 0;
internal Boolean socketready = false;
void Start()
{
setupSocket();
Debug.Log("socket is set up");
writeSocket();
readSocket();
}
void Update()
{
}
public void setupSocket()
{
try
{
gamesocket = new TcpClient(Host, Port);
thestream = gamesocket.GetStream();
thereader = new StreamReader(thestream);
thewriter = new StreamWriter(thestream);
socketready = true;
Debug.Log("socket is sent");
}
catch (Exception e)
{
Debug.Log("Error: " + e);
}
}
public String writeSocket()
{
if (!socketready)
thewriter.Write(true);
else { Debug.Log("could not write a value"); };
return "";
}
public String readSocket()
{
if (!socketready)
return "";
if (thestream.DataAvailable)
{
string goniometer = thereader.ReadLine();
Debug.Log(goniometer);
return thereader.ReadLine();
}
else { Debug.Log("No value"); };
return "";
}
public void closeSocket()
{
if (!socketready)
return;
thereader.Close();
gamesocket.Close();
socketready = false;
}
}
What am I doing wrong? or is there a better method to connect the two without TCP? I'm very green when it comes to networking so any advice is greatly appreciated
Comment