--服务器端   
声明变量:

  private System.Windows.Forms.TextBox tb_port;
  private System.Windows.Forms.Button bt_start;
  private System.Windows.Forms.RichTextBox richTextBox1;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.Button bt_stop;
  private Socket   socket1;
  private Socket   socket2;
  private int       port=0;
  private IPEndPoint ipep; 
  private Thread      thread_conn;       
  private Thread      thread_rcv;
  private System.Windows.Forms.Button bt_send;
  private System.Windows.Forms.StatusBarPanel statusBarPanel1;
  private System.Windows.Forms.StatusBarPanel statusBarPanel2;
 启动服务按钮事件:
  private void bt_start_Click(object sender, System.EventArgs e)
  {
   start();     
  }
  //
  //启动服务函数
  //
  protected void start()
  {
   this.bt_start.Visible=false;
   this.bt_stop.Visible=true;

   //初始化
   try
   {
     socket1.Close();
   }
   catch
   {}

   //设置端口
   port  = Int32.Parse(this.tb_port.Text);
   //设置本地主机
   ipep = new IPEndPoint(GetServerIP(),port);
   //创建socket
   socket1 = new   Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
   //绑定主机
   socket1.Bind(ipep);
   //帧听数量
   socket1.Listen(5);   
   //启动连接线程
   thread_conn  = new  Thread(new ThreadStart( conn_fun ));
   thread_conn.Start();
  
  }

 //
  //获取主机ip
  //
  protected IPAddress GetServerIP()
  {
   //获取主机信息
   IPHostEntry iphostentry = Dns.GetHostByName(Dns.GetHostName());
   //获取主机地址
   IPAddress ipaddr   = iphostentry.AddressList[0];
   return ipaddr;
  }
  //
  //等待连接线程函数
  //
  protected void conn_fun()
  {
   try
   {    
    this.statusBarPanel1.Text="启动等待接受中。。";
    //连接
    socket2=socket1.Accept();--待解决的问题:  这里如何能得到远程连接机子(即客户端)的ip地址呢?
  if(socket2.Connected)
    {
     //显示
      this.statusBarPanel1.Text="已经连接";
     //启动接受数据线程 
     thread_rcv  = new Thread(new ThreadStart(rcv_fun));
     thread_rcv.Start(); 
    }   
   }
   catch
   {}
  }
  //
  //接受数据线程函数,死循环
  //
  protected void rcv_fun()
  {
   while(true)
   {
    //断开连接时候退出死循环
    //设置接受缓存空间
    byte[]  buffer = new byte[1024];
    try
    {
     int i = socket2.Receive(buffer,0,buffer.Length,SocketFlags.None);
     if(i==0)
     {
      this.statusBarPanel1.Text="断开连接";
      break;
     }
     else
     {

      string data="";
      //接受数据     
      data = System.Text.Encoding.BigEndianUnicode.GetString(buffer);
      this.richTextBox1.AppendText("\r\n"+data);
      this.statusBarPanel1.Text="已经接受数据";
     }
    }
    catch   //对方断开连接后 socket2.receive出现异常
    {     
      this.statusBarPanel1.Text="对方断开连接";
      socket2.Close();
     conn_fun();    
    }
   }  
  }
 停止服务按钮事件:
 private void bt_stop_Click(object sender, System.EventArgs e)
  {
   this.bt_start.Visible=true;
   this.bt_stop.Visible=false;
   this.statusBarPanel1.Text="正在停止。。";

   try
   {
    //停止接受数据线程
    if(thread_rcv.IsAlive)
     thread_rcv.Abort();
   }
   catch
   {}

   //停止连接线程
   if(thread_conn.IsAlive)
    thread_conn.Abort();

   try
   {
    //socket1.Shutdown(SocketShutdown.Both);
    socket1.Close();
   }
   catch
   {}

   try
   {  
    //socket2.Shutdown(SocketShutdown.Both);
    socket2.Close();
   }
   catch
   {}
   
   this.statusBarPanel1.Text="已经停止服务";
  }
//
  //发送数据线程函数
  //
  protected void send_fun()
  {
   byte[] buffer = new byte[1024];  
   buffer = System.Text.Encoding.BigEndianUnicode.GetBytes(this.richTextBox1.Text );
   socket2.Send(buffer,0,buffer.Length,SocketFlags.None);
   
  }
发送数据按钮事件:
  private void bt_send_Click(object sender, System.EventArgs e)
  {

   //启动发送数据线程
   thread_rcv  = new  Thread(new  ThreadStart(send_fun));
   thread_rcv.Start();
  }

  

posted on 2005-09-13 22:18  DAVID'S BLOG  阅读(1373)  评论(0)    收藏  举报