使用Thread
傾聽並與遠端通信最簡單的例子:
public class ListenerThread
{
// clients list/queue
Queue<ClientConnection> m_Clients;
// thread used to listen for new connections
Thread m_Thread;
Socket m_Socket;
IPEndPoint m_LocalEndPoint;
volatile bool m_IsListening;
public ListenerThread(int port)
{
// get this machine hostname
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
// resolve ip address from hostname
IPAddress ipAddress = ipHostInfo.AddressList[0];
// create local end point object
m_LocalEndPoint = new IPEndPoint(ipAddress, port);
}
void Listen()
{
// reset clients list
m_Clients = new Queue<ClientConnection>();
// initialize socket
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// bind this socket to listen for incomming connections to specified end point
m_Socekt.Bind(localEndPoint);
// start listening with backlog of 1337 connections
m_Socket.Listen(1337);
// dont forget to dispose after socket was used to "unbind" it
using (m_Socket)
{
while (m_IsListening)
{
// while listening just accept connections and start them at another thread
Socket client = m_Socket.Accept();
if (client != null)
{
m_Clients.Enqueue(new ClientConnection(client));
}
}
}
}
// method used to start the listening server
public void Start()
{
if (m_Thread == null)
{
m_Thread = new Thread(Listen);
}
m_IsListening = true;
m_Thread.Start();
}
// method used to stop listening server
public void Stop()
{
m_Listening = false;
m_Thread.Join();
while (m_Clients.Count != 0)
{
m_Clients.Dequeue().Kill();
}
}
}
// class used to communicate with the client
public class ClientConnection
{
Socket m_Socket; // client socket
Thread m_Thread; // communication thread
volatile bool m_IsCommunicating;
// this should start immediately because of the incomming connection priority
internal ClientConnection(Socket socket)
{
m_Socket = socket;
m_Thread = new Thread(Communicate);
m_Thread.Start();
}
// loop in which you should send/receive data
void Communicate()
{
while (m_IsCommunicating)
{
// .. do your communication stuff
}
}
// should be only used by ListenerThread to end communication.
internal void Kill()
{
m_IsCommunicating = false;
try
{
m_Thread.Join(5 * 1000);
m_Thread.Abort();
}
catch(Exception ex) { /*...*/ }
}
}
這確實是最簡單的例子可能的,所以你應該修改此爲您的需求。
要使用此與您的例子只是開始ListenerThread
:
ListenerThread listener = new ListenerThread(8001);
listener.Start();
displayText.AppendText("The server is running at port 8001...\n");
最後一件事是,如果你想在UI電話,我建議使用SynchronizationContext
。爲了使其在ListenerThread
構造函數調用更清楚:
m_Sync = SynchronizationContext.Current;
再拍領域:
SynchronizationContext m_Sync;
然後只是通過這一背景下進入ClientConnection
構造函數new ClientConnection(m_Sync, client);
。
現在你可以使用SynchronizationContext.Post
方法例如。 :
m_Sync.Post(state => { someUITextElement.AppendText((string)state); }, "hello world");
您應該考慮爲網絡通信目的創建專用的「線程」。那麼如果你想從通信線程傳輸數據到UI線程中,你應該使用一些同步上下文。 –
你需要把broadcasttheconnection到一個線程 – BugFinder
@BugFinder - 如果你不介意的話,請你給我一個例子... –