tcp助手类是在socket层上建立的,可以直接通过client或server属性设置或获取。

 

 

 Server
{
    
    public partial class TalkServer : Form
    {
        
#region Parameters

        IPEndPoint remoteIpep;
        TcpClient tcpc,tcpcb;
        TcpListener tcpl;
        Thread threadL;
        NetworkStream ns;
        
delegate void AppendItem(object o);
        AppendItem AddItem;
        Socket connectSocket;
        
void addItem(object o)
        {
            listBox.Items.Add(o);
        }

        
#endregion

        
#region Events

        
public TalkServer()
        {
            InitializeComponent();
            AddItem 
= new AppendItem(addItem);
        }

        
private void btServerStart_Click(object sender, EventArgs e)
        {
            tcpl 
= new TcpListener(IPAddress.Any, 2008);
            
try
            {
                tcpl.Start(
10);
                threadL 
= new Thread(new ThreadStart(acceptCnns));
                threadL.IsBackground 
= true;
                threadL.Start();
                listBox.Items.Add(
"Server start success");
            }
            
catch (Exception exc)
            {
                listBox.Items.Add(exc.Message);
                
throw;
            }

        }

        
#endregion

        
#region Methods

        
private void acceptCnns()
        {
            
string welcome = "welcome to server!";
            
byte[] buf = Encoding.Unicode.GetBytes(welcome);
            
while (true)
            {
                
try
                {
                    tcpc 
= tcpl.AcceptTcpClient();
                    remoteIpep 
= (IPEndPoint ) tcpc.Client.RemoteEndPoint;
                    ns 
= tcpc.GetStream();
                    ns.Write(buf, 
0, buf.Length);
                    ThreadPool.QueueUserWorkItem(
new WaitCallback(receiveData));
                }
                
catch (Exception)
                {

                    
throw;
                }
            }
        }

        
private void receiveData(object o)
        {
            
int len = tcpc.Available;
            
byte[] buf = new byte[1024];
            
string data = string.Empty;
            
while (true)
            {
                len 
= tcpc.Available;
                
if (len != 0)
                {
                    ns.Read(buf, 
0, len);
                    data 
= Encoding.Unicode.GetString(buf, 0, len);
                    listBox.Invoke(AddItem, data);
                }
            }
        }
        
#endregion

        
private void btConnect_Click(object sender, EventArgs e)
        {
            tcpcb 
= new TcpClient();
            tcpcb.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 
true);
            
try
            {
                tcpcb.Connect(remoteIpep);
                
//ns = tcpcb.GetStream(); 
                listBox.Invoke(AddItem, "Connected!");
            }
            
catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
    }
}

 


 Client
{
    public partial class TalkClient : Form
    {
        
#region Parameters

        TcpClient tcpc,tcpc2,tcpcb,tcpcr;
        TcpListener tcpl;
        NetworkStream ns;
        Thread threadR;
        Thread threadL;
        
delegate void AppendItem(object o);
        AppendItem AddItem;
        
void addItem(object o)
        {
            listBox.Items.Add(o);
        }

        
#endregion

        
#region Events

        
public TalkClient()
        {
            InitializeComponent();
            AddItem 
= new AppendItem(addItem);
        }

        
private void btListen_Click(object sender, EventArgs e)
        {

        }

        
private void TalkClient_Load(object sender, EventArgs e)
        {

        }

        
private void btCnnect_Click(object sender, EventArgs e)
        {
            
string target = txtTarget.Text.Trim();
            IPAddress ipa 
= Dns.GetHostAddresses(target)[0];
            IPEndPoint ipep 
= new IPEndPoint(ipa, 2008);
            tcpc 
= new TcpClient();
            
try
            {
                tcpc.Connect(ipep);
                listBox.Items.Add(
"connected!");
                ns 
= tcpc.GetStream();
                tcpcr 
= tcpc;
                threadR 
= new Thread(new ThreadStart(receiveData));
                threadR.IsBackground 
= true;
                threadR.Start();
                listBox.Items.Add(
"thread receive started!");
            }
            
catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            tcpc2 
= new TcpClient();
            tcpc2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 
true);
            
try
            {
                tcpc2.Connect(ipep);
                listBox.Items.Add(
"Connection 2 connected!");
            }
            
catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        
private void btSend_Click(object sender, EventArgs e)
        {
            
string data = txtMessage.Text.Trim();
            
try
            {
                
byte[] buf = Encoding.Unicode.GetBytes(data);
                ns.Write(buf, 
0, buf.Length);
            }
            
catch (Exception exc)
            {
                listBox.Items.Add(exc.Message);
                
throw;
            }
        }

        
#endregion

        
#region Methods

        
void receiveData()
        {
            
string data = string.Empty;
            
byte[] buf = new byte[1024];
            
int len = 0;

            
while (true)
            {
                len 
= tcpcr.Available;
                
if ( len != 0)
                {
                    ns.Read(buf, 
0, len);
                    data 
= Encoding.Unicode.GetString(buf, 0, len);
                    listBox.Invoke(AddItem, data);
                }
            }
        }


        
#endregion



    }
}

相关文章:

  • 2021-06-26
  • 2021-10-23
  • 2022-12-23
  • 2021-08-20
  • 2021-12-11
  • 2022-01-30
  • 2021-07-21
  • 2021-06-05
猜你喜欢
  • 2021-07-02
  • 2021-10-15
  • 2021-09-16
  • 2021-08-24
  • 2021-10-03
  • 2021-07-04
  • 2021-08-05
相关资源
相似解决方案