- Home /
Question by
Mojo-Game-Studio · Jan 09, 2019 at 08:16 PM ·
shadernetworkingstreamreadertcpclient
I have a simple tcpClient and a server, both connect ok and work in unity editor but not as standalone
Hi all, I have two project that communicate via sockets, when I run them both from 2 unity editor projects, the communication works fine between them, but as standalone they don't, netstat doesn't even show them as 'listening'. Ports are configured correctly.
They both run on the local machine, basically connecting to 127.0.0.1:11000
need help fast please, anybody have any ideas?
Comment
Best Answer
Answer by Mojo-Game-Studio · Jan 09, 2019 at 08:50 PM
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
public class s_TCP : MonoBehaviour
{
internal Boolean socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "localhost";//CLUDGE TO BE READ FROM file
Int32 Port = 11000;
public bool initialised = false;
public void Initialise(string h,int p)
{
Host = h;
Port = p;
setupSocket();
initialised = true;
SendCommand("5"); //initialse command to server
}
void Update()
{
if (!initialised) return;
//theStream.Flush();
string str = readSocket();
Debug.Log("Socket bufffer = " + str);
if (str.Length > 0) SendMessage("DoCommand", str);
}
private void OnDestroy()
{
closeSocket();
}
// **********************************************
void setupSocket()
{
try
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theStream.ReadTimeout = 1;
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e)
{
Debug.LogError("Socket error: " + e);
}
}
public void SendCommand(string command)
{
if (!socketReady)
return;
//String foo = theLine + "\r\n";
theWriter.Write(command);
theWriter.Flush();
}
public String readSocket()
{
if (!socketReady)
return "";
try
{
// Check to see if this NetworkStream is readable.
if (theStream.CanRead)
{
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = theStream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while (theStream.DataAvailable);
return myCompleteMessage.ToString();
}
//return theReader.ReadLine();
}
catch (Exception e)
{
return "";
}
return "";
}
public void closeSocket()
{
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}
} // end class s_TCP
sorry forgot to include the code in the question
Your answer
Follow this Question
Related Questions
Problems with TLS handshake 0 Answers
StreamReader & Unity still does weird things 1 Answer
Unity tcp_client only working on localhost 1 Answer
This script freezes unity. Anyone knows answer? 1 Answer
Unity networking tutorial? 6 Answers