1 Socket方式

 

连接按钮:

服务器: 新建一个socket-->.Bind,绑定IPEndPoint-->.Listen,监听 -->.Accept

 

客户端:新建一个socket-->.Connect

新建一个线程thread对应receive函数

thread.Start()

//receive函数:接收代码用一个while循环起来。

if(AcceptSocke连接上)
  
try{接收}
  
catch{报警;断开;重新监听}

 


{
        //主程序
    ChatServer=new IPEndPoint(IPAddress.Parse(textBox4.Text);Int32.Parse(textBox5.Text));
    ChatSocket
=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        
    ChatSocket.Bind(ChatServer);
//绑定,监听
    ChatSocket.Listen(50);
    statusBar2.Text
=textBox1.Text+" has logoned on.Now begin to listenC#网络通信 同步方式总结";
                
        AcceptedSocket 
= ChatSocket.Accept();

    Thread thread
=new Thread(new ThreadStart(ChatProcess));//相当于receive
    thread.Start();

}
catch(Exception ee)
    {statusBar2.Text
=ee.Message;}

 


        {
            ChatServer=new IPEndPoint(IPAddress.Parse(textBox4.Text),Int32.Parse(textBox5.Text));
            ChatSocket
=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            ChatSocket.Connect(ChatServer);
            statusBar1.Text
=textBox1.Text+" has logoned on.Now begin to connectC#网络通信 同步方式总结";
            Thread thread
=new Thread(new ThreadStart(ChatProcess));
            thread.Start();
        }
        
catch(Exception ee)
            {statusBar1.Text
=ee.Message;}

 

发送按钮:

.Send;

 


            {
                Byte[] SentByte=new Byte[64];
                
string SentStr=textBox1.Text+"::::"+textBox3.Text+"\r\n";
                SentByte
=System.Text.Encoding.BigEndianUnicode.GetBytes(SentStr.ToCharArray());
                AcceptedSocket.Send(SentByte,SentByte.Length,
0);
                textBox3.Clear();
            }
            
catch
            {statusBar2.Text
="The connection has not existed!";}

 

断开按钮:

socket.Close();

 


            {
               
                ChatSocket.Close();
                AcceptedSocket.Close();
                statusBar2.Text="The connection is disconnected!";
                
            }
            
catch{statusBar2.Text="The connection has not existed!";}

 

2 TcpClient方式

连接按钮:

服务器:Tcplistener()--->listener.Start();

客户端:TcpClient()--->tcpClient.Connect();

新建一个线程thread,绑定receive函数

thread.Start();

 


           {
               listener = new TcpListener(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
               listener.Start();

                toolStripStatusLabel1.Text 
= "等待连接C#网络通信 同步方式总结";

           Thread thread 
= new Thread(new ThreadStart(Receive));
            thread.Start();
           }
            
catch(Exception ee)
           {
              toolStripStatusLabel1.Text 
=ee.Message;
            }

 


            {
                tcpClient = new TcpClient();
                tcpClient.Connect(textBox1.Text, Int32.Parse(textBox2.Text));
                toolStripStatusLabel1.Text 
= "连接上。";
                Thread thead 
= new Thread(new ThreadStart(receive));
                thead.Start();
            }
            
catch (Exception ee)
            {
                toolStripStatusLabel1.Text 
= ee.Message;
            }

 

//receive函数

服务器:listener.AcceptSocket();

          if(acceptSocket连接上)

           {while循环接收,networkstream(acceptsocket),netstream.Read}

          catch{报警,断开,重新监听}

客户端:

         netstream = tcpClient.GetStream

                       .Read

 

 listener.AcceptSocket();

           try
               {
                    
if(AcceptSocket.Connected)
                    {
                        toolStripStatusLabel1.Text 
= "建立连接";
                        
while(!needControl)
                        {
                            netStream 
= new NetworkStream(AcceptSocket);
                            
byte[] bytes = new byte[64];
                            netStream.Read(bytes, 
0, bytes.Length);

                            
string str = System.Text.Encoding.ASCII.GetString(bytes);
                            Invoke(
new Action<string>(delegate(string aa) { richTextBox1.AppendText(aa); })
                           , str);
                        }
                     }
                }
           
catch (Exception ee)
                {

                    Invoke(
new Action<string>(delegate(string aa)
                   { toolStripStatusLabel1.Text 
=aa; })
                            , ee.Message);

                }

 


                {
                   if (tcpClient.Connected)
                    {
                 
                        
while (!needControl)
                        {

                            
byte[] bytes = new byte[64];
                            netStream 
= tcpClient.GetStream();
                            netStream.Read(bytes, 
0, bytes.Length);

                            
string str = System.Text.Encoding.ASCII.GetString(bytes);
                            Invoke(
new Action<string>(delegate(string aa) { richTextBox1.AppendText(aa); })
                                  , str);
                        }
                    }
               }
                
catch(Exception ee) 
                {
                    Invoke(
new Action<string>(delegate(string aa) { toolStripStatusLabel1.Text = aa; })
                          , ee.Message);
                        
                }

 

发送按钮:

   通过networkstream发送

   服务器来自acceptsocket,客户端来自tcpClient.GetStream()

 

 


            {
                netStream = new NetworkStream(AcceptSocket);
                
string str = richTextBox2.Text + "\r\n";
                
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);
                netStream.Write(bytes, 
0, bytes.Length);
                netStream.Flush();
                richTextBox2.Text 
= null;
            }
            
catch (Exception ee)
            {
                toolStripStatusLabel1.Text 
= ee.Message;
 
            }

 


            {
                netStream = tcpClient.GetStream();
                
string str = richTextBox2.Text + "\r\n";
                
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);

                netStream.Write(bytes, 
0, bytes.Length);
                netStream.Flush();
                richTextBox2.Text 
= null;
            }
            
catch (Exception ee)
            {
                toolStripStatusLabel1.Text 
= ee.Message;
            }

 

断开按钮:

   服务区关闭acceptsocket,listener

   客户端关闭tcpClient

         

 

相关文章: