怡文圣美的空间

源代码的空间
   ::  :: 新随笔 :: 联系 :: 聚合  :: 管理

以下是MainForm.cs的源代码

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


namespace  MyQ
{
    
///   <summary>
    
///  Form1 的摘要说明。
    
///   </summary>

     public   class  MainForm : System.Windows.Forms.Form
    
{
//         类成员变量
         private  System.ComponentModel.Container components  =   null ;
        
private   bool  isMainComputer;
        
private   string  strUserName;
        
private   string  strIP;
        
private   int  intLocPort;
        
private   int  intRemotePort;
        
private  Thread th;
        
private  TcpListener tlListen1;
        
private   bool  listenRun;
        
private  NetworkStream tcpStream;
        
private  StreamWriter sw;
        
private  TcpClient tcpc;
        
private  System.Windows.Forms.RichTextBox rtbRemoteMessage;
        
private  System.Windows.Forms.RichTextBox rtbLocalMessage;
        
private  System.Windows.Forms.Button buttonClose;
        
private  System.Windows.Forms.Button buttonSend;
        
private  System.Windows.Forms.MainMenu mainMenu1;
        
private  System.Windows.Forms.MenuItem menuItem1;
        
private  System.Windows.Forms.MenuItem menuItem2;
        
private  System.Windows.Forms.Panel panelBottom;
        
private  System.Windows.Forms.Panel panelFill;
        
private  System.Windows.Forms.Splitter splitter1;
        
private  System.Windows.Forms.Panel panelBotton_Right;
        
private  Socket skSocket;
        
//         构造函数
         public  MainForm( string  n, string  ip, string  locport, string  report, bool  loginstyle)
        
{
            
this .strUserName = n;
            
this .strIP = ip;
            
this .intLocPort = Int32.Parse(locport);
            
this .intRemotePort = Int32.Parse(report);
            
this .isMainComputer = loginstyle;
            InitializeComponent();
        }

        
        
protected   override   void  Dispose(  bool  disposing )
        
{
            
try
            
{
                
this .listenRun = false ;
                th.Abort();
                th
= null ;
                
this .tlListen1.Stop();
                
this .skSocket.Close();
                
this .tcpc.Close();
                
this .sw.Close();
            }

            
catch {}
            
if ( disposing )
            
{
                
if  (components  !=   null
                
{
                    components.Dispose();
                }

            }

            
base .Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
        [STAThread]
        
static   void  Main() 
        
{
            
            
try
            
{
                Login f 
= new  Login();
                
if (f.ShowDialog() == DialogResult.OK)
                
{
                    
if (f.loginStyle.SelectedItem.ToString() == " 主机 " )
                    
{
                        f.boolLoginStyle
= true ;
                    }

                    
else
                    
{
                        f.boolLoginStyle
= false ;
                    }

                    MainForm frm 
= new  MainForm(f.textUserName.Text,f.textRemoteIP.Text,f.textLocPort.Text,f.textRePort.Text,f.boolLoginStyle);
                    f.Dispose();
                    Application.Run(frm);
                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }


        
private   void  Listen()
        
{
            
this .listenRun = true ;
            
try
            
{
                
this .tlListen1 = new  TcpListener( this .intLocPort);
                
this .tlListen1.Start();
                
this .skSocket = this .tlListen1.AcceptSocket();
                EndPoint tempRemoteEP
= this .skSocket.RemoteEndPoint;
                
while ( this .listenRun)
                
{
                    Byte[] stream
= new   byte [ 80 ];
                    
int  i  = this .skSocket.ReceiveFrom(stream, ref  tempRemoteEP);
                    
string  strInf = System.Text.Encoding.UTF8.GetString(stream, 0 ,i);
                    
if (strInf == "" )
                    
{
                        
this .rtbRemoteMessage.AppendText( " \n " + " 对方已挂断 " );
                        
this .buttonSend.Enabled = false ;
                        
break ;
                    }

                    
switch (strInf)
                    
{
//                         主机传给辅机的,告诉辅机主机已响应
                         case   " cns " :
                            
this .rtbRemoteMessage.AppendText( " \n " + " 恭喜,主机已成功响应,你们可以聊天了。 " );
                            
this .buttonSend.Enabled = true ;
                            
break ;
//                         辅机对主机进行主动连接
                         case   " cn " :
                            
try
                            
{
                                tcpc
= new  TcpClient( this .strIP, this .intRemotePort);
                                tcpStream
= tcpc.GetStream();
//                                 发送连接通知字符cns
                                 string  cns = " cns " ;
                                sw
= new  StreamWriter( this .tcpStream);
                                sw.Write(cns);
                                sw.Flush();
                                
                            }

                            
catch (Exception)
                            
{
                                
this .rtbRemoteMessage.AppendText( " \n " + " 连接失败!可能对方还未登陆,请告知对方登陆。也可能对方所填端口与您填写的端口不一致! " );
                            }

                            
this .rtbRemoteMessage.AppendText( " \n " + " 恭喜,辅机已成功连接上了主机,你们可以聊天了。 " );
                            
this .buttonSend.Enabled = true ;
                            
break ;
                        
                        
default :
                            
this .rtbRemoteMessage.AppendText( " \n " + " 对方: " + strInf);
                            
break ;                    
                    }
        
                }

            }

            
catch (System.Security.SecurityException)
            
{
                MessageBox.Show(
" 防火墙安全错误! " , " 错误 " ,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }

        }

        
private   void  MainForm_Load( object  sender, System.EventArgs e)
        
{
            
this .listenRun = true ;
            th
= new  Thread( new  ThreadStart(Listen));
            th.Start();
            
if ( ! this .isMainComputer)
            
{
//                 这里是辅机
                 this .rtbRemoteMessage.AppendText( " 正在连接主机 " );
                
this .Text = " MyQ  辅机 " ;
                
try
                
{
                    tcpc
= new  TcpClient( this .strIP, this .intRemotePort);
                    tcpStream
= tcpc.GetStream();
//                     发送连接通知字符cn
                     string  strInf = " cn " ;
                    sw
= new  StreamWriter( this .tcpStream);
                    sw.Write(strInf);
                    sw.Flush();
                }

                
catch (Exception)
                
{
                    
this .rtbRemoteMessage.AppendText( " \n " + " 连接失败!可能对方还未登陆,请告知对方登陆。也可能对方所填端口与您填写的端口不一致! " );
                }

            }

            
else
            
{
                
this .Text = " MyQ  主机 " ;
                
this .rtbRemoteMessage.AppendText( " 正在监听,等待辅机连接 " );
            }

        }


        
private   void  buttonSend_Click( object  sender, System.EventArgs e)
        
{
            
string  strInf = this .rtbLocalMessage.Text;
            
if (strInf != "" )
            
{
                
this .rtbRemoteMessage.AppendText( " \n " + " 自己: " + strInf);
                sw
= new  StreamWriter( this .tcpStream);
                sw.Write(strInf);
                sw.Flush();
                
this .rtbLocalMessage.Clear();
            }

            
else
            
{
                MessageBox.Show(
" 不可以输入空字符串,否则对方会认为已断线! " );
            }

        }

        
        
private   void  menuItem2_Click( object  sender, System.EventArgs e)
        
{
            Application.Exit();
        }


        
private   void  buttonClose_Click( object  sender, System.EventArgs e)
        
{
            Application.Exit();
        }


        
private   void  MainForm_KeyDown( object  sender, System.Windows.Forms.KeyEventArgs e)
        
{
            
if ((Control.ModifierKeys  ==  Keys.Alt) && (e.KeyCode == System.Windows.Forms.Keys.S))
            
{
                
this .buttonSend_Click( this ,e);
            }

        }

    }

}

只有注册用户登录后才能发表评论。