1
每當我使用Socket(127.0.0.1, port)
連接到服務器時,服務器都會偵聽並找到客戶端。但問題在於服務器將其註冊爲具有兩個不同ID的兩個客戶端。當我關閉客戶端時,兩個ID都關閉在一起。服務器爲Socket()調用註冊兩次客戶端ID
我似乎無法找到似乎是造成這種雙註冊。我希望這裏有人能找到這個原因,我沒有運氣。 :(
代碼Listener.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.IO;
namespace SERVER {
class Listener
{
Socket sock;
public bool Listening
{
get;
private set;
}
public int Port
{
get;
private set;
}
public Listener(int port)
{
Port = port;
sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}
public void Start()
{
if (Listening)
return;
sock.Bind(new IPEndPoint(0, Port));
sock.Listen(0);
sock.BeginAccept(callback, null);
Listening = true;
}
public void Stop()
{
if (!Listening)
return;
sock.Close();
sock.Dispose();
sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}
void callback(IAsyncResult ar)
{
try
{
Socket sock = this.sock.EndAccept(ar);
SocketAccepted(sock);
if (SocketAccepted != null)
{
SocketAccepted(sock);
}
this.sock.BeginAccept(callback, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public delegate void SocketAcceptedHandler(Socket e);
public event SocketAcceptedHandler SocketAccepted;
}
}
這是問題,我已經檢查了很多次,並且無法發現它!謝謝bud。 –
@WormholeXd不接受任何理由? – doogle
嗯..我不記得不接受?oo,再次接受謝謝讓告訴我知道! @doogle –