C# server socket 异步编程

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Net;
using  System.Net.Sockets;
using  System.Threading;
namespace  ScoketProxy
ExpandedBlockStart.gifContractedBlock.gif
{
    
class ZServer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ManualResetEvent clientConnected 
=    new ManualResetEvent(false);
        TcpListener tl;
        
public ZServer(int port)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            tl 
= new TcpListener(port);
        }


        
~ZServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            tl.Stop();
        }


        
public void Listen()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            tl.Start(
5);
            clientConnected.Reset();
            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                tl.BeginAcceptTcpClient(
new AsyncCallback(OnAccept), tl);
                clientConnected.WaitOne();
            }

            
        }


        
void OnAccept(IAsyncResult ar)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            TcpListener listener 
= (TcpListener)ar.AsyncState;
            TcpClient client 
= listener.EndAcceptTcpClient(ar);
            TcpClient tranform 
= new TcpClient("220.181.5.97",80);
            NetworkStream cns 
= client.GetStream();
            NetworkStream tns 
= tranform.GetStream();
            
while (client.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//byte[] cbuf = ReadBlock(client);
                
//tns.Write(cbuf, 0, cbuf.Length);
                
//byte[] tbuf = ReadBlock(tranform);
                
//cns.Write(tbuf,0,tbuf.Length);

                
byte[] cbuf = ReadBlock(client);
                SendBlock(client, cbuf);
                
byte[] tbuf = ReadBlock(tranform);
                SendBlock(tranform, tbuf);

            }

            
//TcpListener listener = (TcpListener)ar.AsyncState;
            
//TcpClient client = listener.EndAcceptTcpClient(ar);
            
//clientConnected.Set();
            
//while(true)
            
//{
            
//    string s = ReadLine(client);
            
//    if (s == "exit")
            
//    {
            
//        Console.WriteLine("bye");
            
//        break;
            
//    }
            
//    byte[] buf = GetBytes(s);
            
//    client.GetStream().Write(buf,0,buf.Length);
            
//}
            
//return;
        }


        
byte[] GetBytes(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return System.Text.Encoding.UTF8.GetBytes(s.ToCharArray());
        }


        
string GetString(byte[] bs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return System.Text.Encoding.UTF8.GetString(bs);
        }


        
string ReadLine(TcpClient client)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            StringBuilder sb 
= new StringBuilder();
            NetworkStream ns 
= client.GetStream();
            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
                
byte[] buf = new byte[client.Available];
                ns.Read(buf, 
0, buf.Length);
                
string s = GetString(buf);
                
if (s == "\r\n")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
break;
                }

                sb.Append(s);
            }

            
return sb.ToString();
        }


        
byte[] ReadBlock(TcpClient client)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            System.Collections.Generic.List
<byte> r = new List<byte>();
            
while (client.GetStream().DataAvailable)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
byte[] buf = new byte[1];
                client.GetStream().Read(buf, 
0, buf.Length);
                r.Add(buf[
0]);
            }

            
            
return r.ToArray();
        }


        
int SendBlock(TcpClient c,byte[] buf)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            c.GetStream().Write(buf, 
0, buf.Length);
            
return buf.Length;
        }



    }

}


转载于:https://www.cnblogs.com/zyip/archive/2009/05/06/1450590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值