.NETソケットを使用していますが、ユニティプロジェクトから直接アプリケーションを実行しても、スタンドアロンアプリケーションをエクスポートすると正常に動作しますが、問題は私はユニティウェブプレーヤーを使用します。それはポート、私は推測するので、会話を拒否した。私はポートを私がアプリケーションに使用しているものに変更しましたが、今はクライアント接続を拒否しませんでしたが、それでも動作しません。クライアントからのデータはサーバーに届かず何も起こっていません。ユニティWebプレーヤーはソケットで正常に動作しますか?ユニティとWeb Playerを備えたソケット
public class SocketMain : MonoBehaviour {
System.Threading.Thread SocketThread;
volatile bool keepReading = false;
string alto = "", ancho ="";
int al = 0, an = 0;
// Use this for initialization
void Start() {
Application.runInBackground = true;
startServer();
}
bool bandera = false;
// Update is called once per frame
void Update() {
if(al != 0 && an != 0)
{
Grid grid = gameObject.AddComponent<Grid>();
grid.xSize = al;
grid.zSize = an;
al = 0;
an = 0;
}
}
void startServer(){
SocketThread = new System.Threading.Thread(networkCode);
SocketThread.IsBackground = true;
SocketThread.Start();
}
private string getIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
Socket listener;
Socket handler;
void networkCode()
{
string data;
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// host running the application.
Debug.Log("Ip " + getIPAddress().ToString());
IPAddress[] ipArray = Dns.GetHostAddresses("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);
// Create a TCP/IP socket.
listener = new Socket(ipArray[0].AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
keepReading = true;
// Mientras se espera la conexion de un cliente.
Debug.Log("Esperando Conexxion");
handler = listener.Accept();
Debug.Log("Se ha Desconectado el cluiente");
data = null;
while (keepReading)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
Debug.Log("Received from Server");
if (bytesRec <= 0)
{
keepReading = false;
handler.Disconnect(true);
break;
}
//Datos recibidos y concatenados
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Pintamos data complto
Debug.Log(data);
var definition = new {alto = "", ancho ="" };
//Deserealizamos data
var medidas = JsonConvert.DeserializeAnonymousType(data, definition);
Debug.Log("alto :" + medidas.alto + " , ancho :" + medidas.ancho);
//Termina deserializacion
alto = medidas.alto;
ancho = medidas.ancho;
al = int.Parse(alto);
an = int.Parse(ancho);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
System.Threading.Thread.Sleep(1);
}
System.Threading.Thread.Sleep(1);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
void stopServer()
{
keepReading = false;
//stop thread
if (SocketThread != null)
{
SocketThread.Abort();
}
if (handler != null && handler.Connected)
{
handler.Disconnect(false);
Debug.Log("Disconnected!");
}
}
void OnDisable()
{
stopServer();
}
}
これは、.NETソケットはUnity3D WebPlayerでは動作しませんが、マルチスレッドが動作しないということではない私のサーバークラス
関連するコードを表示できますか? UDPを使用していますか? –
はい、ここにコードを入れて、Tcpを使っています。それはうまく動作するという事実ですが、Web Playerで動作しませんでした。それは動作しません。 – MisaelGaray