C# Unity不接受TCP连接,可能拒绝连接
我受这个困扰太久了 我的脚本不允许任何TCP连接从发送它们的设备通过。我已经用插座测试了我的发射装置,它们可以根据我的需要工作和发射。然而,团结不接受我的信号,我不知道为什么 有人能看一下我的剧本,看看他们能不能找出原因?我省略了大部分调试行C# Unity不接受TCP连接,可能拒绝连接,c#,sockets,tcp,unity3d,C#,Sockets,Tcp,Unity3d,我受这个困扰太久了 我的脚本不允许任何TCP连接从发送它们的设备通过。我已经用插座测试了我的发射装置,它们可以根据我的需要工作和发射。然而,团结不接受我的信号,我不知道为什么 有人能看一下我的剧本,看看他们能不能找出原因?我省略了大部分调试行 public class TCP : MonoBehaviour { string ip_address = "192.168.0.87"; int port = 54321; public GameObject G
public class TCP : MonoBehaviour
{
string ip_address = "192.168.0.87";
int port = 54321;
public GameObject GText;
Thread listen_thread;
TcpListener tcp_listener;
Thread clientThread;
volatile bool should_stop;
public void CloseSocket(TcpListener Client)
{
//Client.Close();
}
public void Start()
{
IPAddress ip_addy = IPAddress.Parse(ip_address);
tcp_listener = new TcpListener(ip_addy, 22);
tcp_listener.Start();
StartAccept();
}
private void StartAccept()
{
tcp_listener.BeginAcceptTcpClient(HandleAsyncConnection, tcp_listener);
GText.GetComponent<GUIText>().text =("Listening Again");
Debug.Log("listeneing again");
}
private void HandleAsyncConnection(IAsyncResult res)
{
Debug.Log("handle async");
byte[] bytes = new byte[1024];
string data;
StartAccept(); //listen for new connections again
TcpClient client = tcp_listener.EndAcceptTcpClient(res);
NetworkStream stream = client.GetStream();
//proceed
int i;
// Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length);
while (i != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
i = stream.Read(bytes, 0, bytes.Length);
}
GText.GetComponent<GUIText>().text =("Second Operation");
//CloseSocket(tcp_listener);
}
void RequestStop()
{
should_stop = true;
}
// Use this for initialization
/**
void Start ()
{
try{
IPAddress ip_addy = IPAddress.Parse(ip_address);
tcp_listener = new TcpListener(ip_addy, port);
listen_thread = new Thread(new ThreadStart(ListenForClients));
listen_thread.Start();
}
catch(Exception e)
{
GText.GetComponent<GUIText>().text =(e.ToString());
}
}
private void ListenForClients()
{
this.tcp_listener.Start();
while(true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcp_listener.AcceptTcpClient();
this.tcp_listener.Stop();
client.Close();
}
this.tcp_listener.Stop();
}
*/
private void HandleClientComm(object client)
{
TcpClient tcp_client = (TcpClient)client;
NetworkStream client_stream = tcp_client.GetStream();
byte[] message = new byte[4096];
int bytes_read;
while(true)
{
bytes_read = 0;
try
{
//blocks until a client sends a message
bytes_read = client_stream.Read(message, 0, 4096);
}
catch (Exception e)
{
//a socket error has occured
GText.GetComponent<GUIText>().text =(e.Message);
Debug.Log(e.Message);
break;
}
if(bytes_read == 0)
{
//client has disconnected
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
}
try
{
clientThread.Abort();
}
catch(Exception e)
{
GText.GetComponent<GUIText>().text =(e.Message);
Debug.Log(e.Message);
}
}
公共类TCP:monobhavior
{
字符串ip_address=“192.168.0.87”;
int端口=54321;
公共游戏对象GText;
线听线;
TcpListener-tcp\u侦听器;
线程clientThread;
应停止挥发性bool;
公共void CloseSocket(TcpListener客户端)
{
//Client.Close();
}
公开作废开始()
{
IPAddress ip_addy=IPAddress.Parse(ip_地址);
tcp_侦听器=新的TcpListener(ip_addy,22);
tcp_listener.Start();
startacept();
}
私有无效StartAccept()
{
tcp_listener.BeginAcceptTcpClient(HandleAsyncConnection,tcp_listener);
GText.GetComponent().text=(“再次侦听”);
Log(“再次侦听”);
}
私有无效HandleAsyncConnection(IAsyncResult res)
{
Log(“handleasync”);
字节[]字节=新字节[1024];
字符串数据;
StartAccept();//再次侦听新连接
TcpClient client=tcp_listener.EndAcceptTcpClient(res);
NetworkStream=client.GetStream();
//进行
int i;
//循环以接收客户端发送的所有数据。
i=stream.Read(字节,0,字节.长度);
而(i!=0)
{
//将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i);
//处理客户端发送的数据。
data=data.ToUpper();
byte[]msg=System.Text.Encoding.ASCII.GetBytes(数据);
//发回回复。
stream.Write(msg,0,msg.Length);
i=stream.Read(字节,0,字节.长度);
}
GText.GetComponent().text=(“第二个操作”);
//CloseSocket(tcp_侦听器);
}
void RequestStop()
{
应该停止=真;
}
//用于初始化
/**
无效开始()
{
试一试{
IPAddress ip_addy=IPAddress.Parse(ip_地址);
tcp_侦听器=新的TcpListener(ip_地址,端口);
listen_thread=新线程(新线程开始(ListenForClients));
听一听;
}
捕获(例外e)
{
GText.GetComponent().text=(e.ToString());
}
}
私有void listenforcients()
{
这个.tcp_listener.Start();
while(true)
{
//阻止,直到客户端连接到服务器
TcpClient client=this.tcp_listener.AcceptTcpClient();
此.tcp_listener.Stop();
client.Close();
}
此.tcp_listener.Stop();
}
*/
私有void HandleClientCommand(对象客户端)
{
TcpClient tcp_客户端=(TcpClient)客户端;
NetworkStream client_stream=tcp_client.GetStream();
字节[]消息=新字节[4096];
读取整数字节;
while(true)
{
字节读取=0;
尝试
{
//阻塞,直到客户端发送消息
bytes\u read=客户端\u stream.read(消息,04096);
}
捕获(例外e)
{
//发生套接字错误
GText.GetComponent().text=(e.Message);
Debug.Log(e.Message);
打破
}
如果(字节数_读取==0)
{
//客户端已断开连接
打破
}
ascienceoding编码器=新的ascienceoding();
}
尝试
{
clientThread.Abort();
}
捕获(例外e)
{
GText.GetComponent().text=(e.Message);
Debug.Log(e.Message);
}
}
我也尝试使用socket test连接到我的unity应用程序,但我收到一个错误消息,说连接被拒绝。有人能看到这段代码中发生了什么吗?我没有检查你的代码,但是如果你使用的是免费版本的unity iOS或unity Android,那么套接字支持会被故意禁用(为了让你购买专业版)@Calvin那里的数据清楚地显示了.NET套接字随免费版本和专业版本一起提供,不是吗?@Bart我发现iOS、Android和Windows Store的免费版本缺少对.NET套接字支持的复选标记。啊@Calvin,你说得对。我默认使用Unity视图。我注意到OP现在谈论的是“设备”.所以你可能是对的。