Unityアプリがtcpデータを取り込めるように設定できました。ただし、プロジェクトを実行すると、アプリケーション全体がフリーズし、メッセージを受信するまで何もしません。次に、メッセージが受信されると、そのメッセージを出力し、クライアントが再度接続することを許可しません。
このコードは、私の TCP 読み取りクラス全体です。これを引き起こしている愚かなことがここで行われていますか?
TCP データを読み取るためのより良い方法はありますか?
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System;
using System.IO;
public class TCPConnection: MonoBehaviour
{
public string ip_address = "";
public int port_number = 0;
void Start()
{
try
{
IPAddress ipAddress = IPAddress.Parse(ip_address);
TcpListener listener = new TcpListener(ipAddress, port_number);
listener.Start();
print("The server is running at port " + port_number);
print("The local end point is :" + listener.LocalEndpoint);
print("Waiting for connection.....");
Socket socket = listener.AcceptSocket();
print("Connection accepted from " + socket.RemoteEndPoint);
byte[] b = new byte[100];
int k = socket.Receive(b);
print("recieved...");
for (int i = 0; i < k; i++)
print (Convert.ToChar(b[i]));
ASCIIEncoding ascii = new ASCIIEncoding();
socket.Send(ascii.GetBytes("The string was recieved by the server"));
print("\nSent ack");
}
catch (Exception e)
{
print("error...." + e.Message);
}
}
}