我正在做一个简单的项目,涉及单个服务器程序和单个客户端程序。它需要检查客户端是否已连接(从服务器端),反之亦然。
当客户端失去互联网时,服务器需要知道它已断开连接。然后,客户端需要在重新连接互联网时重新连接到服务器
当客户端失去互联网然后重新获得互联网时,我无法使用相同的端口重新连接。
我尝试让服务器监听而不关闭套接字,但这也不起作用。在重用方面,我为套接字尝试了很多属性,我也尝试了挥之不去的东西。
我已经看到它可能会停留在TIME_WAIT
操作系统在注册表中设置的某些属性上(在 Windows 的情况下)。重申一下,我的问题是能够使用相同的套接字(更重要的是相同的端口)在客户端丢失并重新获得互联网后重新连接,并且仍在监听等待重新连接。
就像我说的,我可以检测到它何时与服务器端和客户端断开连接,但是当我尝试使用相同的端口和相同的套接字或重新启动的套接字重新连接时,它仍然无法连接,也不会出现在所有。有什么建议可以帮助解决这个问题吗?我一直在寻找很长时间才能弄清楚这个问题。
设想:
TL;DR 在客户端 - 服务器模型上丢失并重新获得互联网,但无法使用相同的套接字和端口连接到服务器。
private void button2_Click(object sender, EventArgs e)
{
// This will stop the threads/connections and toggle the button back to its original state
if (button2.Text == "Stop Listening")
{
listener.Close();
stop = true;
threadsActive = false;
button2.Text = "Start Listening";
textBox1.AppendText("Manually Closed Threads/Connections" + Environment.NewLine);
}
else
{
listenThread = new Thread(listenLoop);
listenThread.IsBackground = true;
status = new Thread(checkIfOnline);
status.IsBackground …
Run Code Online (Sandbox Code Playgroud) 到目前为止,这是主要代码的一部分(网络是单独完成的):
void Update()
{
if (Network.connections.Length > 0)
{
audioFloat = new float[audioInfo.clip.samples * audioInfo.clip.channels];
audioInfo.clip.GetData (audioFloat, 0);
networkView.RPC("PlayMicrophone", RPCMode.Others, ToByteArray(audioFloat), audioInfo.clip.channels);
}
}
[RPC]
void PlayMicrophone(byte[] ba, int c)
{
float[] flar = ToFloatArray (ba);
audio.clip = AudioClip.Create ("test", flar.Length, c, 44100, true, false);
audio.clip.SetData (flar, 0);
audio.loop = true;
while (!(Microphone.GetPosition("") > 0)) {}
audio.Play();
}
void OnConnectedToServer()
{
audioInfo.clip = Microphone.Start(null, true, 100, 44100);
}
// When you are the server and the client connected to
void …
Run Code Online (Sandbox Code Playgroud) c# network-programming microphone audio-streaming unity-game-engine