VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • 实例c#开发TCP/IP聊天室

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

  服务器端原代码:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Threading;
usingSystem.Net.Sockets;
namespaceWin_Talk
{
  publicpartialclassForm1:Form
  {
    publicForm1()
    {
      InitializeComponent();
    }
    privatevoidForm1_Load(objectsender,EventArgse)
    {
    }
    #region//定义变量
    IPAddressHostIP=IPAddress.Parse("127.0.0.1");
    IPEndPointpoint;
    Socketsocket;
    boolflag=true;
    SocketacceptedSocket;
    #endregion
    #region//声名委托
    delegatevoidSetTextCallback(stringtext);
    privatevoidSetText(stringtext)
    {
      textBox2.AppendText(text+"rn");
    }
    #endregion
    #region//进程方法
    privatevoidProccess()
    {
      if(acceptedSocket.Connected)
      {
        while(flag)
        {
          byte[]receiveByte=newbyte[64];
          acceptedSocket.Receive(receiveByte,receiveByte.Length,0);
          stringstrInfo=Encoding.BigEndianUnicode.GetString(receiveByte);
          this.Invoke(newSetTextCallback(SetText),newobject[]{strInfo});
        }
      }
    }
    #endregion
    privatevoidbutton3_Click(objectsender,EventArgse)
    {
      try
      {
        socket.Close();
        acceptedSocket.Close();
      }
      catch
      {
      }
      finally
      {
        Close();
      }
    }
    privatevoidbutton2_Click_1(objectsender,EventArgse)
    {
      try
      {
        Byte[]sendByte=newByte[64];
        stringsendStr=this.textBox1.Text+":"+this.textBox3.Text+"rn";
        sendByte=Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
        acceptedSocket.Send(sendByte,sendByte.Length,0);
      }
      catch{}
    }
    privatevoidbutton1_Click_1(objectsender,EventArgse)
    {
      HostIP=IPAddress.Parse("127.0.0.1");
      try
      {
        point=newIPEndPoint(HostIP,Int32.Parse("11000"));
        socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        socket.Bind(point);
        socket.Listen(50);
        acceptedSocket=socket.Accept();
        Threadthread=newThread(newThreadStart(Proccess));
        thread.Start();
      }
      catch(Exceptioney)
      {
        MessageBox.Show(ey.Message);
      }
    }
  }
}

 

  客户端程序代码:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Threading;
usingSystem.Net.Sockets;
namespacewin_talkClient
{
  publicpartialclassForm1:Form
  {
    #region//声名变量
    IPAddressHostIP=IPAddress.Parse("127.0.0.1");
    IPEndPointpoint;
    Socketsocket;
    boolflag=true;
    #endregion
    
   #region//声名委托
    delegatevoidSetTextCallback(stringtext);
    privatevoidSetText(stringtext)
    {
     textBox2.AppendText(text+"rn");
   }
    #endregion
   #region//进程
   privatevoidProccess()
    {
      if(socket.Connected)
      {
        while(flag)
        {
          byte[]receiveByte=newbyte[64];
          socket.Receive(receiveByte,receiveByte.Length,0);
          stringstrInfo=Encoding.BigEndianUnicode.GetString(receiveByte);
          this.Invoke(newSetTextCallback(SetText),newobject[]{strInfo});
          
        }
      }
    }
   #endregion
    privatevoidbutton2_Click(objectsender,EventArgse)
    {
      try
      {
        Byte[]sendByte=newByte[64];
        stringsendStr=this.textBox1.Text+":"+this.textBox3.Text+"rn";
        sendByte=Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
        socket.Send(sendByte,sendByte.Length,0);
      }
      catch{}
    }
    
    publicForm1()
      {
        InitializeComponent();
      }
    privatevoidForm1_Load(objectsender,EventArgse)
      {
      }
    privatevoidbutton1_Click(objectsender,EventArgse)
      {
        HostIP=IPAddress.Parse("127.0.0.1");
        try
        {
          point=newIPEndPoint(HostIP,Int32.Parse("11000"));
          socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
          socket.Connect(point);
          Threadthread=newThread(newThreadStart(Proccess));
          thread.Start();
        }
        catch(Exceptioney)
        {
          MessageBox.Show("服务器没有开启rn"+ey.Message);
        }
      }
    privatevoidbutton3_Click(objectsender,EventArgse)
    {
      try
      {
        socket.Close();
      }
      catch
      {
      }
      finally
      {
        Close();
      }
    }
  }
}

 

  客户端程序代码:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Threading;
usingSystem.Net.Sockets;
namespacewin_talkClient
{
  publicpartialclassForm1:Form
  {
    #region//声名变量
    IPAddressHostIP=IPAddress.Parse("127.0.0.1");
    IPEndPointpoint;
    Socketsocket;
    boolflag=true;
    #endregion
    
   #region//声名委托
    delegatevoidSetTextCallback(stringtext);
    privatevoidSetText(stringtext)
    {
     textBox2.AppendText(text+"rn");
   }
    #endregion
   #region//进程
   privatevoidProccess()
    {
      if(socket.Connected)
      {
        while(flag)
        {
          byte[]receiveByte=newbyte[64];
          socket.Receive(receiveByte,receiveByte.Length,0);
          stringstrInfo=Encoding.BigEndianUnicode.GetString(receiveByte);
          this.Invoke(newSetTextCallback(SetText),newobject[]{strInfo});
          
        }
      }
    }
   #endregion
    privatevoidbutton2_Click(objectsender,EventArgse)
    {
      try
      {
        Byte[]sendByte=newByte[64];
        stringsendStr=this.textBox1.Text+":"+this.textBox3.Text+"rn";
        sendByte=Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
        socket.Send(sendByte,sendByte.Length,0);
      }
      catch{}
    }
    
    publicForm1()
      {
        InitializeComponent();
      }
    privatevoidForm1_Load(objectsender,EventArgse)
      {
      }
    privatevoidbutton1_Click(objectsender,EventArgse)
      {
        HostIP=IPAddress.Parse("127.0.0.1");
        try
        {
          point=newIPEndPoint(HostIP,Int32.Parse("11000"));
          socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
          socket.Connect(point);
          Threadthread=newThread(newThreadStart(Proccess));
          thread.Start();
        }
        catch(Exceptioney)
        {
          MessageBox.Show("服务器没有开启rn"+ey.Message);
        }
      }
    privatevoidbutton3_Click(objectsender,EventArgse)
    {
      try
      {
        socket.Close();
      }
      catch
      {
      }
      finally
      {
        Close();
      }
    }
  }
}



相关教程