Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9166525
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: WINDOWS

2010-07-14 10:17:20

   C#是微软随着VS.net新推出的一门语言。它作为一门新兴的语言,有着C++的强健,又有着VB等的RAD特性。而且,微软推出C#主要的目的是为了对抗Sun公司的Java。大家都知道Java语言的强大功能,尤其在网络编程方面。于是,C#在网络编程方面也自然不甘落后于人。本文就向大家介绍一下C#下实现套接字(Sockets)编程的一些基本知识,以期能使大家对此有个大致了解。首先,我向大家介绍一下套接字的概念。 

套接字基本概念: 

   套接字是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。可以将套接字看作不同主机间的进程进行双向通信的端点,它构成了单个主机内及整个网络间的编程界面。套接字存在于通信域中,通信域是为了处理一般的线程通过套接字通信而引进的一种抽象概念。套接字通常和同一个域中的套接字交换数据(数据交换也可能穿越域的界限,但这时一定要执行某种解释程序)。各种进程使用这个相同的域互相之间用Internet协议簇来进行通信。 

   套接字可以根据通信性质分类,这种性质对于用户是可见的。应用程序一般仅在同一类的套接字间进行通信。不过只要底层的通信协议允许,不同类型的套接字间也照样可以通信。套接字有两种不同的类型:流套接字和数据报套接字。 

套接字工作原理: 

   要通过互联网进行通信,你至少需要一对套接字,其中一个运行于客户机端,我们称之为ClientSocket,另一个运行于服务器端,我们称之为ServerSocket。 

   根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。 

   所谓服务器监听,是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。 

   所谓客户端请求,是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。 

   所谓连接确认,是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
 C#中的套接字编程实例: 

   通过向大家简单的介绍套接字的基本概念和实现套接字编程的基本原理,我想大家对套接字编程已有了初步的了解。不过,上面介绍的仅仅是基本概念和原理,要真正运用还是需要一定的工作的。对基本概念和原理的真正理解的最好方法莫过于自己动手做一个实例,下面我就向大家介绍一个很好的用C#实现套接字编程的实例――聊天室程序。 

   本程序是基于C/S(服务器/客户端)构架的,程序包含一个服务器端的应用程序和一个客户端的应用程序。首先,在服务器上运行服务器端的应用程序,该程序一运行就开始服务器监听。然后,在客户机上就可以打开客户端的应用程序。程序打开后可以与服务器端应用程序进行连接,即进行客户端请求。在连接确认后,客户端用户可以和其他的客户端用户进行聊天。客户端人数没有限制,同时还支持“悄悄话”聊天模式,支持聊天记录。所以这是一个学习套接字编程的相当不错的例子。而且,程序中为了处理每个客户端的信息还用到了多线程机制。在每个客户端与服务器端连接成功后,它们之间就建立一个线程。这样运用了多线程之后,客户端之间就不会相互影响,即使其中一个出了错误也不会影响到另一个。 

   下面,我就向大家具体介绍该实例: 

   服务器端程序: 

   1. 打开VS.net,新建一个C#的模板为“Windows 应用程序”的项目,不妨命名为“ChatServer”。 

   2. 布置界面。只需在界面上添加一个ListBox控件即可,该控件主要用于显示客户端的用户的一些信息的。图象如下: 



   3. 服务器端程序的代码编写。 

   对于服务器端,主要的作用是监听客户端的连接请求并确认其请求。程序一开始便打开一个StartListening()线程。 

private void StartListening() 

  listener = new TcpListener(listenport); 
  listener.Start(); 
  while (true) 
  { 
   try 
   { 
    Socket s = listener.AcceptSocket(); 
    clientsocket = s; 
    clientservice = new Thread(new ThreadStart(ServiceClient)); 
    clientservice.Start(); 
   } 
   catch(Exception e) 
   { 
    Console.WriteLine(e.ToString() ); 
   } 
  } 

   该线程是一直处于运行状态的。当服务器端接收到一个来自客户端的连接请求后,它就打开一个ServiceClient()线程来服务客户端。当一个连接被建立后,每个客户端就被赋予一个属于它自己的套接字。同时,一个Client类的对象被建立。该对象包含了客户端的一些相关信息,该信息被保存在一个数组列表中。    Client类如下: 

using System; 
using System.Threading; 

namespace ChatServer 

using System.Net.Sockets; 
using System.Net; 

/// 
/// Client 的摘要说明。 
/// 
public class Client 

private Thread clthread; 
private EndPoint endpoint; 
private string name; 
private Socket sock; 

public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock) 

// TODO: 在此处添加构造函数逻辑 
clthread = _thread; 
endpoint = _endpoint; 
name = _name; 
sock = _sock; 


public override string ToString() 

      return endpoint.ToString()+ " : " + name; 


public Thread CLThread 

get{return clthread;} 
set{clthread = value;} 


public EndPoint Host 

get{return endpoint;} 
set{endpoint = value;} 


public string Name 

get{return name;} 
set{name = value;} 


public Socket Sock 

get{return sock;} 
set{sock = value;} 



   程序的主体部分应是ServiceClient()函数。该函数是一个独立的线程,其主要部分是一个while循环。在循环体内,程序处理各种客户端命令。服务器端接收来自客户端的以ASCII码给出的字符串,其中包含了一个“|”形式的分隔符。字符串中“|”以前的部分就是具体的命令,包括CONN、CHAT、PRIV、GONE四种类型。CONN命令建立一个新的客户端连接,将现有的用户列表发送给新用户并告知其他用户有一个新用户加入。CHAT命令将新的信息发送给所有用户。PRIV命令将悄悄话发送给某个用户。GONE命令从用户列表中除去一个已离开的用户并告知其他的用户某某已经离开了。同时,GONE命令可以设置布尔型的变量keepalive为false从而结束与客户端连接的线程。ServiceClient()函数如下: 

private void ServiceClient() 

Socket client = clientsocket; 
bool keepalive = true; 


while (keepalive) 

Byte[] buffer = new Byte[1024]; 
client.Receive(buffer); 
string clientcommand = System.Text.Encoding.ASCII.GetString(buffer); 


string[] tokens = clientcommand.Split(new Char[]{'|'}); 
Console.WriteLine(clientcommand); 


if (tokens[0] == "CONN") 

for(int n=0; n 

Client cl = (Client)clients[n]; 
SendToClient(cl, "JOIN|" + tokens[1]); 

EndPoint ep = client.RemoteEndPoint; 
Client c = new Client(tokens[1], ep, clientservice, client); 
clients.Add(c); 
string message = "LIST|" + GetChatterList() +"\r\n"; 
SendToClient(c, message); 


lbClients.Items.Add(c); 



if (tokens[0] == "CHAT") 

for(int n=0; n 

Client cl = (Client)clients[n]; 
SendToClient(cl, clientcommand); 


if (tokens[0] == "PRIV") 

string destclient = tokens[3]; 
for(int n=0; n 

Client cl = (Client)clients[n]; 
if(cl.Name.CompareTo(tokens[3]) == 0) 
SendToClient(cl, clientcommand); 
if(cl.Name.CompareTo(tokens[1]) == 0) 
SendToClient(cl, clientcommand); 


if (tokens[0] == "GONE") 

int remove = 0; 
bool found = false; 
int c = clients.Count; 
for(int n=0; n 

Client cl = (Client)clients[n]; 
SendToClient(cl, clientcommand); 
if(cl.Name.CompareTo(tokens[1]) == 0) 

remove = n; 
found = true; 
lbClients.Items.Remove(cl); 


if(found) 
clients.RemoveAt(remove); 
client.Close(); 
keepalive = false; 



   这样,服务器端程序就基本完成了。(其他略次要的代码可以参见源代码中的Form1.cs文件)程序运行图示如下: 


   客户端程序: 

   1. 打开VS.net,新建一个C#的模板为“Windows 应用程序”的项目,不妨命名为“ChatClient”。 

   2. 布置界面。往界面上添加一个ListBox控件(用于显示用户列表),一个RichTextBox控件(用于显示聊天消息以及系统消息),一个TextBox控件(用于发送消息),一个CheckBox控件(确定是否为悄悄话),一个StatusBar控件以及四个Button控件(分别为“连接”、“断开连接”、“开始记录”、“发送”)。各个控件的属性设置可以参见源代码中的具体设置,这里从略。界面设计好后的图象如下: 



   3. 客户端程序的代码编写。 

   当客户端试图和服务器端进行连接时,一个连接必须建立而且得向服务器端进行注册。 EstablishConnection()函数运用一个TcpClient来和服务器端取得连接,同时创建一个NetworkStream来发送消息。还有,端口号和服务器端的是保持一致的,均为5555。EstablishConnection()函数如下:

private void EstablishConnection() 

statusBar1.Text = "正在连接到服务器"; 
try 

clientsocket = new TcpClient(serveraddress,serverport); 
ns = clientsocket.GetStream(); 
sr = new StreamReader(ns); 
connected = true; 

catch (Exception) 

MessageBox.Show("不能连接到服务器!","错误", 
MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
statusBar1.Text = "已断开连接"; 


   在和服务器端连接成功后,程序就用RegisterWithServer()函数向服务器端发送一个CONN命令。该命令先是发送该用户的名称,然后从服务器端获得其他所有用户的列表,所有用户列表是在ListBox控件中显示的。该函数如下: 

private void RegisterWithServer() 

try 

string command = "CONN|" + ChatOut.Text; 
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray()); 
ns.Write(outbytes,0,outbytes.Length); 


string serverresponse = sr.ReadLine(); 
serverresponse.Trim(); 
string[] tokens = serverresponse.Split(new Char[]{'|'}); 
if(tokens[0] == "LIST") 

statusBar1.Text = "已连接"; 
btnDisconnect.Enabled = true; 

for(int n=1; n 
lbChatters.Items.Add(tokens[n].Trim(new char[]{'\r','\n'})); 
this.Text = clientname + ":已连接到服务器"; 



catch (Exception) 

MessageBox.Show("注册时发生错误!","错误", 
MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 


   在此之后,当然就是用户之间的聊天了,由ReceiveChat()函数来完成。该函数是一个独立的线程,它处理所有用户获得的消息和用户发送的消息。它主要处理了CHAT、PRIV、JOIN、GONE、QU99v等命令,处理的方法和服务器端的类似。具体函数实现如下: 

private void ReceiveChat() 

bool keepalive = true; 
while (keepalive) 

try 

Byte[] buffer = new Byte[2048]; 
ns.Read(buffer,0,buffer.Length); 
string chatter = System.Text.Encoding.ASCII.GetString(buffer); 
string[] tokens = chatter.Split(new Char[]{'|'}); 

if (tokens[0] == "CHAT") 

rtbChatIn.AppendText(tokens[1]); 
if(logging) 
logwriter.WriteLine(tokens[1]); 

if (tokens[0] == "PRIV") 

rtbChatIn.AppendText("Private from "); 
rtbChatIn.AppendText(tokens[1].Trim() ); 
rtbChatIn.AppendText(tokens[2] + "\r\n"); 
if(logging) 

logwriter.Write("Private from "); 
logwriter.Write(tokens[1].Trim() ); 
logwriter.WriteLine(tokens[2] + "\r\n"); 


if (tokens[0] == "JOIN") 

rtbChatIn.AppendText(tokens[1].Trim() ); 
rtbChatIn.AppendText(" has joined the Chat\r\n"); 
if(logging) 

logwriter.WriteLine(tokens[1]+" has joined the Chat"); 

string newguy = tokens[1].Trim(new char[]{'\r','\n'}); 
lbChatters.Items.Add(newguy); 

if (tokens[0] == "GONE") 

rtbChatIn.AppendText(tokens[1].Trim() ); 
rtbChatIn.AppendText(" has left the Chat\r\n"); 
if(logging) 

logwriter.WriteLine(tokens[1]+" has left the Chat"); 

lbChatters.Items.Remove(tokens[1].Trim(new char[]{'\r','\n'})); 

if (tokens[0] == "QU99v") 

ns.Close(); 
clientsocket.Close(); 
keepalive = false; 
statusBar1.Text = "服务器端已停止"; 
connected= false; 
btnSend.Enabled = false; 
btnDisconnect.Enabled = false; 


catch(Exception){} 


   通过以上的一些函数,客户端程序之间就可以进行自由地聊天了,各个用户之间还可以互相发送悄悄话。所以程序已经实现了聊天室的基本功能了,不过最后各个用户还要正常地退出,那就要用到QuitChat()函数了。该函数的具体实现如下: 

private void QuitChat() 

if(connected) 

try 

string command = "GONE|" + clientname; 
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray()); 
ns.Write(outbytes,0,outbytes.Length); 
clientsocket.Close(); 

catch(Exception) 



if(logging) 
logwriter.Close(); 
if(receive != null && receive.IsAlive) 
receive.Abort(); 
this.Text = "客户端"; 

   到此为止,客户端程序的主要部分都已经介绍完毕。还有一些按钮控件的消息处理函数可以参见源代码。同时,程序中还有一个聊天记录功能,该功能和现在流行的聊天软件的记录功能类似。不过限于篇幅,在这里就不一一介绍了,有兴趣的读者可以研究一下本文后面的源代码。 

   这样,客户端程序就完成了。程序运行图示如下: 

 



总结: 

   本文向大家初步介绍了套接字的基本概念和实现套接字编程的基本原理,还通过一个很好的实例向大家展示了在C#下进行套接字编程的实现方法和一些编程技巧。从中,我们不难发现运用C#进行套接字编程乃至网络编程有许多优越之处。实例程序实现的思路清晰明了而且通俗易懂,是一个相当不错的例子,希望各位能好好研读。同时还希望大家能进一步完善该程序,使之功能更强大、界面更友好。
 
 
完整代码
 

/********************************8ChatServer:*************************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Chat_Server
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  ///
  /// 必需的设计器变量。
  ///

  private System.ComponentModel.Container components = null;

  static int listenport=6666;
  Socket clientsocket;
  private System.Windows.Forms.ListBox lbClients;
  ArrayList clients;
  private System.Windows.Forms.Button button1;
  Thread clientservice;
  private System.Windows.Forms.Label label1;
  Thread threadListen;

  public Form1()
  {
   
   InitializeComponent();

  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    
    if(clientservice != null)
    {
     clientservice.Abort();
    }
    if(threadListen != null)
    {
     try
     {
      threadListen.Abort();
     }
     catch(Exception ex)
     {
      threadListen = null;
     }
    }    

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

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.lbClients = new System.Windows.Forms.ListBox();
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // lbClients
   //
   this.lbClients.ItemHeight = 12;
   this.lbClients.Location = new System.Drawing.Point(16, 24);
   this.lbClients.Name = "lbClients";
   this.lbClients.Size = new System.Drawing.Size(184, 268);
   this.lbClients.TabIndex = 0;
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(272, 56);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(240, 136);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(120, 32);
   this.label1.TabIndex = 2;
   this.label1.Text = "label1";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(368, 309);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.lbClients);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  ///


  /// 应用程序的主入口点。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void StartListening()
  {
    TcpListener listener = new TcpListener(listenport);
    listener.Start();
   label1.Text = "listening....";
   while (true)
   {
    try
    {
    
     Socket s = listener.AcceptSocket();
     clientsocket = s;
     clientservice = new Thread(new ThreadStart(ServiceClient));
     clientservice.Start();
    }
    catch(Exception ex)
    {
     MessageBox.Show("listening Error: "+ex.Message);
    }
   }   
  }
  private void ServiceClient()
  {
   Socket client = clientsocket;
   bool keepalive = true;


   while (keepalive)
   {
    Byte[] buffer = new Byte[1024];
    int bufLen = 0;
    try
    {
     bufLen = client.Available ;
     
     client.Receive(buffer,0,bufLen,SocketFlags.None);
     if(bufLen==0)
      continue;    
    }
    catch(Exception ex)
    {
     MessageBox.Show("Receive Error:"+ex.Message);
     return;
    }
    
    string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0,bufLen);

    string[] tokens = clientcommand.Split(new Char[]{'|'});
    Console.WriteLine(clientcommand);

    if (tokens[0] == "CONN")
    {
     for(int n=0; n     {
      Client cl = (Client)clients[n];
      SendToClient(cl, "JOIN|" + tokens[1]);
     }
     EndPoint ep = client.RemoteEndPoint;
     Client c = new Client(tokens[1], ep, clientservice, client);
     
     string message = "LIST|" + GetChatterList() +"\r\n";
     SendToClient(c, message);

     clients.Add(c);


     lbClients.Items.Add(c);


    }
    if (tokens[0] == "CHAT")
    {
     for(int n=0; n     {
      Client cl = (Client)clients[n];
      SendToClient(cl, clientcommand);
     }
    }
    if (tokens[0] == "PRIV")
    {
     string destclient = tokens[3];
     for(int n=0; n     {
      Client cl = (Client)clients[n];
      if(cl.Name.CompareTo(tokens[3]) == 0)
       SendToClient(cl, clientcommand);
      if(cl.Name.CompareTo(tokens[1]) == 0)
       SendToClient(cl, clientcommand);
     }
    }
    if (tokens[0] == "GONE")
    {
     int remove = 0;
     bool found = false;
     int c = clients.Count;
     for(int n=0; n     {
      Client cl = (Client)clients[n];
      SendToClient(cl, clientcommand);
      if(cl.Name.CompareTo(tokens[1]) == 0)
      {
       remove = n;
       found = true;
       lbClients.Items.Remove(cl);
      }
     }
     if(found)
      clients.RemoveAt(remove);
     client.Close();
     keepalive = false;
    }
   }
  }

  private string GetChatterList()
  {
   string result = "";

   for(int i=0;i   {
    result += ((Client)clients[i]).Name+"|";
   }
   return result;

  }

  private void SendToClient(Client cl,string clientCommand)
  {
   Byte[] message = System.Text.Encoding.ASCII.GetBytes(clientCommand);
   Socket s = cl.Sock;
   if(s.Connected)
   {
    s.Send(message,message.Length,0);
   }
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   clients = new ArrayList();
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   threadListen = new Thread(new ThreadStart(StartListening));
   threadListen.Start();  
  }
 }
}


/***************************** client类 ********************/

/************************** 放于 chatServer 项目中 *********/

using System;
using System.Threading;

namespace Chat_Server
{
 using System.Net.Sockets;
 using System.Net;

 ///
 /// Client 的摘要说明。
 ///
 public class Client
 {
  private Thread clthread;
  private EndPoint endpoint;
  private string name;
  private Socket sock;

  public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock)
  {
   // TODO: 在此处添加构造函数逻辑
   clthread = _thread;
   endpoint = _endpoint;
   name = _name;
   sock = _sock;
  }

  public override string ToString()
  {
   return endpoint.ToString()+ " : " + name;
  }

  public Thread CLThread
  {
   get{return clthread;}
   set{clthread = value;}
  }

  public EndPoint Host
  {
   get{return endpoint;}
   set{endpoint = value;}
  }

  public string Name
  {
   get{return name;}
   set{name = value;}
  }

  public Socket Sock
  {
   get{return sock;}
   set{sock = value;}
  }
 }
}

/***************************** chatClient ************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Chat_Client
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.CheckBox checkBox1;
  private System.Windows.Forms.StatusBar statusBar1;

  NetworkStream ns;
  StreamReader sr;
  TcpClient clientsocket;
  bool connected;
  Thread receive;
  string serveraddress = "219.228.231.85";
  int serverport = 6666;

  private System.Windows.Forms.RichTextBox rtbChatIn;
  private System.Windows.Forms.ListBox lbChatters;
  private System.Windows.Forms.TextBox ChatOut;
  private System.Windows.Forms.Button btnDisconnect;
  private System.Windows.Forms.Button btnSend;
  private System.Windows.Forms.TextBox clientName;

  string clientname;
  private System.Windows.Forms.Button btnConnect;

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   
   InitializeComponent();

  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(receive != null)
    {
     QuitChat();
    }
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.lbChatters = new System.Windows.Forms.ListBox();
   this.rtbChatIn = new System.Windows.Forms.RichTextBox();
   this.checkBox1 = new System.Windows.Forms.CheckBox();
   this.ChatOut = new System.Windows.Forms.TextBox();
   this.btnSend = new System.Windows.Forms.Button();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.btnDisconnect = new System.Windows.Forms.Button();
   this.clientName = new System.Windows.Forms.TextBox();
   this.btnConnect = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // lbChatters
   //
   this.lbChatters.ItemHeight = 12;
   this.lbChatters.Location = new System.Drawing.Point(32, 40);
   this.lbChatters.Name = "lbChatters";
   this.lbChatters.Size = new System.Drawing.Size(112, 172);
   this.lbChatters.TabIndex = 0;
   //
   // rtbChatIn
   //
   this.rtbChatIn.Location = new System.Drawing.Point(160, 40);
   this.rtbChatIn.Name = "rtbChatIn";
   this.rtbChatIn.Size = new System.Drawing.Size(208, 176);
   this.rtbChatIn.TabIndex = 2;
   this.rtbChatIn.Text = "";
   //
   // checkBox1
   //
   this.checkBox1.Location = new System.Drawing.Point(16, 248);
   this.checkBox1.Name = "checkBox1";
   this.checkBox1.TabIndex = 3;
   this.checkBox1.Text = "checkBox1";
   //
   // ChatOut
   //
   this.ChatOut.Location = new System.Drawing.Point(136, 248);
   this.ChatOut.Name = "ChatOut";
   this.ChatOut.Size = new System.Drawing.Size(136, 21);
   this.ChatOut.TabIndex = 4;
   this.ChatOut.Text = "message";
   //
   // btnSend
   //
   this.btnSend.Location = new System.Drawing.Point(336, 248);
   this.btnSend.Name = "btnSend";
   this.btnSend.TabIndex = 5;
   this.btnSend.Text = "send";
   this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 287);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Size = new System.Drawing.Size(464, 22);
   this.statusBar1.TabIndex = 6;
   this.statusBar1.Text = "statusBar1";
   //
   // btnDisconnect
   //
   this.btnDisconnect.Enabled = false;
   this.btnDisconnect.Location = new System.Drawing.Point(392, 112);
   this.btnDisconnect.Name = "btnDisconnect";
   this.btnDisconnect.Size = new System.Drawing.Size(64, 32);
   this.btnDisconnect.TabIndex = 7;
   this.btnDisconnect.Text = "断开";
   this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);
   //
   // clientName
   //
   this.clientName.Location = new System.Drawing.Point(96, 8);
   this.clientName.Name = "clientName";
   this.clientName.TabIndex = 8;
   this.clientName.Text = "name";
   //
   // btnConnect
   //
   this.btnConnect.Location = new System.Drawing.Point(392, 56);
   this.btnConnect.Name = "btnConnect";
   this.btnConnect.Size = new System.Drawing.Size(64, 32);
   this.btnConnect.TabIndex = 9;
   this.btnConnect.Text = "连接";
   this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(464, 309);
   this.Controls.Add(this.btnConnect);
   this.Controls.Add(this.clientName);
   this.Controls.Add(this.btnDisconnect);
   this.Controls.Add(this.statusBar1);
   this.Controls.Add(this.btnSend);
   this.Controls.Add(this.ChatOut);
   this.Controls.Add(this.checkBox1);
   this.Controls.Add(this.rtbChatIn);
   this.Controls.Add(this.lbChatters);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  ///


  /// 应用程序的主入口点。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void EstablishConnection()
  {
   statusBar1.Text = "正在连接到服务器";
   
   try
   {
    clientsocket = new TcpClient(serveraddress,serverport);
    ns = clientsocket.GetStream();
    sr = new StreamReader(ns);
    connected = true;
   
   }
   catch (Exception)
   {
    MessageBox.Show("不能连接到服务器!","错误",
     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    statusBar1.Text = "已断开连接";
   }
  }

  private void RegisterWithServer()
  {
   lbChatters.Items.Clear();

   clientname = clientName.Text;
   try
   {
    string command = "CONN|" + clientname; //+"\r\n";
    Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
    ns.Write(outbytes,0,outbytes.Length);


    string serverresponse = sr.ReadLine();
    serverresponse.Trim();
    string[] tokens = serverresponse.Split('|');
    if(tokens[0] == "LIST")
    {
     statusBar1.Text = "已连接";
     btnDisconnect.Enabled = true;
    }
    if(tokens[1] != "")
    {
     for(int n=1; n      lbChatters.Items.Add(tokens[n].Trim(new char[]{'\r','\n'}));
    }
    this.Text = clientname + ":已连接到服务器";    

   }
   catch (Exception ex)
   {
    MessageBox.Show("注册时发生错误!"+ex.Message,"错误",
     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    connected = false;
   }
  }

  private void ReceiveChat()
  {
   bool keepalive = true;
   while (keepalive)
   {
    try
    {
     Byte[] buffer = new Byte[1024];  // 2048???
     ns.Read(buffer,0,buffer.Length);
     string chatter = System.Text.Encoding.ASCII.GetString(buffer);
     string[] tokens = chatter.Split(new Char[]{'|'});

     if (tokens[0] == "CHAT")
     {
      rtbChatIn.AppendText(tokens[1]);
//      if(logging)
//       logwriter.WriteLine(tokens[1]);
     }
     if (tokens[0] == "PRIV")
     {
      rtbChatIn.AppendText("Private from ");
      rtbChatIn.AppendText(tokens[1].Trim() );
      rtbChatIn.AppendText(tokens[2] + "\r\n");
//      if(logging)
//      {
//       logwriter.Write("Private from ");
//       logwriter.Write(tokens[1].Trim() );
//       logwriter.WriteLine(tokens[2] + "\r\n");
//      }
     }
     if (tokens[0] == "JOIN")
     {
      rtbChatIn.AppendText(tokens[1].Trim() );
      rtbChatIn.AppendText(" has joined the Chat\r\n");
//      if(logging)
//      {
//       logwriter.WriteLine(tokens[1]+" has joined the Chat");
//      }
      string newguy = tokens[1].Trim(new char[]{'\r','\n'});
      lbChatters.Items.Add(newguy);
     }
     if (tokens[0] == "GONE")
     {
      rtbChatIn.AppendText(tokens[1].Trim() );
      rtbChatIn.AppendText(" has left the Chat\r\n");
//      if(logging)
//      {
//       logwriter.WriteLine(tokens[1]+" has left the Chat");
//      }
      lbChatters.Items.Remove(tokens[1].Trim(new char[]{'\r','\n'}));
     }
     if (tokens[0] == "QUIT")
     {
      ns.Close();
      clientsocket.Close();
      keepalive = false;
      statusBar1.Text = "服务器端已停止";
      connected= false;
      btnSend.Enabled = false;
      btnDisconnect.Enabled = false;
     }
    }
    catch(Exception){}
   }
  }

  private void QuitChat()
  {
   if(connected)
   {
    try
    {
     string command = "GONE|" + clientname;
     Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
     ns.Write(outbytes,0,outbytes.Length);
     clientsocket.Close();
    }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
   }
//   if(logging)
//    logwriter.Close();
   if(receive != null && receive.IsAlive)
    receive.Abort();
   this.Text = "客户端";
   
   connected = false;

  }

  private void btnSend_Click(object sender, System.EventArgs e)
  {
   if(connected)
   {
    try
    {
     string command = "CHAT|" + clientname+": "+ChatOut.Text+"\r\n";
     Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
     ns.Write(outbytes,0,outbytes.Length);
     //clientsocket.Close();
    }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
   }
  }

  private void btnConnect_Click(object sender, System.EventArgs e)
  {
   EstablishConnection();
   RegisterWithServer();
   if(connected)
   {   
    receive = new Thread(new ThreadStart(ReceiveChat));
    receive.Start();
   }
  }

  private void btnDisconnect_Click(object sender, System.EventArgs e)
  {
   QuitChat();
  }
 }
}

阅读(2392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~