سلام بر برنامه نویسان عزیز ایران زمین 😉 در این مطلب کد کار با سوکت سرور: استفاده از ThreadPool و TcpListener در سی شارپ #C را آماده کردم. امیدوارم که این کد برای شما مفید باشد.
کد کار با سوکت سرور: استفاده از ThreadPool و TcpListener در سی شارپ #C :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class MainClass
{
public static void Main()
{
TcpListener client = new TcpListener(9050);
client.Start();
Console.WriteLine("Waiting for clients...");
while(true)
{
while (!client.Pending())
{
Thread.Sleep(1000);
}
ConnectionThread newconnection = new ConnectionThread(client);
}
}
}
class ConnectionThread
{
public TcpListener threadListener;
public ConnectionThread(TcpListener lis){
threadListener = lis;
ThreadPool.QueueUserWorkItem(new WaitCallback(HandleConnection));
}
public void HandleConnection(object state)
{
int recv;
byte[] data = new byte[1024];
TcpClient client = threadListener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class MainClass
{
public static void Main()
{
TcpListener client = new TcpListener(9050);
client.Start();
Console.WriteLine("Waiting for clients...");
while(true)
{
while (!client.Pending())
{
Thread.Sleep(1000);
}
ConnectionThread newconnection = new ConnectionThread(client);
}
}
}
class ConnectionThread
{
public TcpListener threadListener;
public ConnectionThread(TcpListener lis){
threadListener = lis;
ThreadPool.QueueUserWorkItem(new WaitCallback(HandleConnection));
}
public void HandleConnection(object state)
{
int recv;
byte[] data = new byte[1024];
TcpClient client = threadListener.AcceptTcpClient();
NetworkStream ns = client.GetStream();