• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#socket请求转发

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

服务端:

void Main()
{
    Socket socketA = null;
    Socket socketB = null;
    var maxWaitingRequest = 10;
    var localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    localSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
    localSocket.Bind(new IPEndPoint(IPAddress.Any, 81));
    localSocket.Listen(maxWaitingRequest);
    while (true)
    {
        var remoteSocket = localSocket.Accept();
        if (socketA == null)    //client
            socketA = remoteSocket;
        else if (socketB == null)   //server
        {
            socketB = remoteSocket;
            ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), new Socket[] { socketA, socketB });
            ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), new Socket[] { socketB, socketA });
        }
    }
}

public static void transfer(object obj)
{
    var socketA = ((Socket[])obj)[0];
    var socketB = ((Socket[])obj)[1];
    while (true)
    {
        try
        {
            var data = new byte[10240];
            int read = socketA.Receive(data);
            if(read > 0)
                socketB.Send(data.Take(read).ToArray());
        }
        catch
        {
            socketA.Close();
            socketA.Close();
            break;
        }
    }
}

客户端:

static Queue<string> msgList = new Queue<string>();
static readonly object msgLocker = new Object();
static int localPort = 20000;
static string msg = "I am A";

void Main()
{
    var localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    var localEP = new IPEndPoint(IPAddress.Any, localPort);
    localSocket.Bind(localEP);
    localSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 81));
    ThreadPool.QueueUserWorkItem(recieveHandler, localSocket);
    
    Thread.Sleep(10000);
    localSocket.Send(System.Text.Encoding.Default.GetBytes(msg));
    
    while (true)
    {
        lock (msgLocker)
        {
            if(msgList.Count >0)
                Console.WriteLine(msgList.Dequeue());
        }
        
        Thread.Sleep(1000);
    }
}

public static void recieveHandler(object p)
{
    var localSocket = (Socket)p;
    while (true)
    {
        var data = new byte[1024];
        var read = localSocket.Receive(data);
        if (read > 0)
        {
            Console.WriteLine("recieved " + read.ToString() + " bytes");
            lock (msgLocker)
                msgList.Enqueue(System.Text.Encoding.Default.GetString(data.Take(read).ToArray()));
        }
    }
}
// Define other methods and classes here

先启动服务端,在10秒内,启动两个客启端,两个客户端就能收到来自对方的 msg


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
跟我学做c#皮肤美化(一)发布时间:2022-07-14
下一篇:
Excel操作类C#版发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap