C# 类似QQ通讯

using  System; 
using  System.Collections.Generic; 
using  System.Text; 
using  System.Net; 
using  System.IO; 
using  System.Net.Sockets; 
using  System.Threading; 

namespace  MyQQSer 

    
class Serv 
    

        
private TcpListener listenr; 
        
private Thread thread; 
        
private bool isClose = false;    //结束程序是退出循环 
        private Socket[] sock = new Socket[50]; 

        
public Serv() 
        

            Listener(); 
        }
 

        
~Serv() 
        

            isClose 
= true
            
for (int i = 0; i < sock.Length; i++
            

                
if (sock[i] != null
                    sock[i].Close(); 
                listenr.Stop(); 
            }
 
        }
 

        
/// <summary> 
        
/// 开始监听端口 
        
/// </summary> 

        public void Listener() 
        

            
if (listenr == null
                listenr 
= new TcpListener(IPAddress.Parse("127.0.0.1"), 5000); 
            listenr.Start(); 
            
if (thread == null
                thread 
= new Thread(new ThreadStart(ReciverMessage)); 
            thread.IsBackground 
= true
            thread.Start(); 
        }
 

        
/// <summary> 
        
/// 接收数据 
        
/// </summary> 

        private void ReciverMessage() 
        

            
int count; //可用套结字索引 
            count = GetCount(); 
            
bool loop = true
            
if (count == -1
                loop 
= false
            
if (loop) 
            

                
while (true
                

                    
// 判断是否退出循环 
                    if (isClose) 
                        
break;                    
                    sock[count] 
= listenr.AcceptSocket(); 
                    Thread t 
= new Thread(new ThreadStart(ReciverMessage)); 
                    t.IsBackground 
= true
                    t.Start(); 
                    
// 接受客户端数据 
                    while (true
                    

                        
byte[] buffs = new byte[100]; 
                        
if (sock[count].Connected) 
                        

                            
try 
                            

                                sock[count].Receive(buffs); 
                                
string message = Encoding.Default.GetString(buffs); 
                                Console.WriteLine(message); 
                                SendToAll(buffs); 
                            }
 
                            
catch (Exception ex) 
                            


                            }
 
                        }
 
                    }
 
                }
 
            }
 
            Thread.CurrentThread.Abort(); 
        }
 

        
// 得到可用套结字索引 
        private int GetCount() 
        

            
for (int i = 0; i < sock.Length; i++
                
if (sock[i] == null
                    
return i; 
            
return -1
        }
 

        
// 发送消息给所有人 
        private void SendToAll(byte[] buff) 
        

            
for (int i = 0; i < sock.Length; i++
            

                
if (sock[i] != null
                

                    
if (sock[i].Connected) 
                    

                        sock[i].Send(buff); 
                    }
 
                }
 
            }
 
        }
 
    }
 
}
 

using  System; 
using  System.Collections.Generic; 
using  System.ComponentModel; 
using  System.Data; 
using  System.Drawing; 
using  System.Text; 
using  System.Windows.Forms; 
using  System.Net.Sockets; 
using  System.Net; 
using  System.Threading; 
using  System.IO; 

namespace  TestClient 

    
public partial class Form1 : Form 
    

        
private TcpClient client; 
        
private Thread t; 
        
private Socket sock; 
        
private bool close = false

        
public Form1() 
        

            InitializeComponent(); 
        }
 

        
// 连接 
        private void btnConnect_Click(object sender, EventArgs e) 
        

            
if(client == null
                client 
= new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(txtLoalPort.Text))); 

            
if (t == null
                t 
= new Thread(new ThreadStart(ReciverMessage)); 
            t.IsBackground 
= true
            t.Start(); 
            btnSend.Enabled 
= true
            btnConnect.Enabled 
= false
        }
 
        
        
// 接收消息 
        private void ReciverMessage() 
        

            client.Connect(IPAddress.Parse(txtSerIP.Text), 
int.Parse(txtSerPort.Text)); 
            
while (true
            

                
if (close) 
                    
break
                    
//NetworkStream nws = client.GetStream(); 
                    
//StreamReader sr = new StreamReader(nws); 
                    
//string message = sr.ReadToEnd(); 
                    
//sr.Close(); 
                    if(sock == null
                            sock 
= client.Client; 
                    
byte[] buff = new byte[100]; 
                    sock.Receive(buff); 
                    
string message = Encoding.Default.GetString(buff); 
                    
this.lstMessage.Items.Add(message);                
            }
 
            Thread.CurrentThread.Abort(); 
        }
 

        
// 发送消息 
        private void btnSend_Click(object sender, EventArgs e) 
        

            
string message = this.txtInput.Text; 
            
byte[] buff = Encoding.Default.GetBytes(message); 
            sock.Send(buff); 
            txtInput.Text 
= ""
        }
 

        
private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        

            close 
= true
            sock.Close(); 
        }
 
    }
 
}

你可能感兴趣的:(C#)