C#聊天程序

程序

/*=====================================================================
  文件:      Wintalk.cs

  摘要:   演示如何使用 .NET创建聊天程序

=====================================================================*/

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Windows.Forms;

class App{       
    // Entry point
    public static void Main(String[] args){       
        // If the args parse in known way then run the app
        if(ParseArgs(args)){          
            // Create a custom Talker object
            Talker talker = new Talker(endPoint, client);
            // Pass the object reference to a new form object
            TalkForm form = new TalkForm(talker);                  
            // Start the talker "talking"
            talker.Start();

            // Run the applications message pump
            Application.Run(form);
        }       
    }

    // Parsed Argument Storage
    private static IPEndPoint endPoint;
    private static bool client;

    // Parse command line arguments
    private static bool ParseArgs(String[] args){
        try{       
            if(args.Length == 0){
                client = false;
                endPoint = new IPEndPoint(IPAddress.Any,5150);
                return true;
            }

            switch(Char.ToUpper(args[0][1])){
            case 'L':
                int port = 5150;
                if(args.Length > 1){
                   port = Convert.ToInt32(args[1]);   
                }
                endPoint = new IPEndPoint(IPAddress.Any,port);
                client = false;
                break;
            case 'C':
                port = 5150;
                String address = "127.0.0.1";
                client = true;
                if(args.Length > 1){
                    address = args[1];
                    port = Convert.ToInt32(args[2]);                                       
                }               
                endPoint = new IPEndPoint(Dns.Resolve(address).AddressList[0], port);
                break;
            default:
                ShowUsage();
                return false;
            }
        }catch{
            ShowUsage();
            return false;
        }   
   
        return true;
    }

    // Show sample usage
    private static void ShowUsage(){
        MessageBox.Show("WinTalk [switch] [parameters...]\n\n"+
            "  /L  [port]\t\t-- Listens on a port.  Default:  5150\n"+
            "  /C  [address] [port]\t-- Connects to an address and port.\n\n"+
            "Example Server - \n"+
            "Wintalk /L\n\n"+
            "Example Client - \n"+
            "Wintalk /C ServerMachine 5150","WinTalk Usage");
    }
}

// UI class for the sample
class TalkForm:Form {   
    public TalkForm(Talker talker) {
        // Associate for method with the talker object
        this.talker = talker;
        talker.Notifications += new
                Talker.NotificationCallback(HandleTalkerNotifications);

        // Create a UI elements
        Splitter talkSplitter = new Splitter();
        Panel talkPanel = new Panel();       

        receiveText = new TextBox();
        sendText = new TextBox();
       
        // we'll support up to 64k data in our text box controls
        receiveText.MaxLength = sendText.MaxLength = 65536;
        statusText = new Label();
    
        // Initialize UI elements
        receiveText.Dock = DockStyle.Top;
        receiveText.Multiline = true;
        receiveText.ScrollBars = ScrollBars.Both;
        receiveText.Size = new Size(506, 192);
        receiveText.TabIndex = 1;
        receiveText.Text = "";
        receiveText.WordWrap = false;
        receiveText.ReadOnly = true;
       
        talkPanel.Anchor = (AnchorStyles.Top|AnchorStyles.Bottom
                    |AnchorStyles.Left|AnchorStyles.Right);
        talkPanel.Controls.AddRange(new Control[] {sendText,
                    talkSplitter,
                    receiveText});
        talkPanel.Size = new Size(506, 371);
        talkPanel.TabIndex = 0;

        talkSplitter.Dock = DockStyle.Top;
        talkSplitter.Location = new Point(0, 192);
        talkSplitter.Size = new Size(506, 6);
        talkSplitter.TabIndex = 2;
        talkSplitter.TabStop = false;
       
        statusText.Dock = DockStyle.Bottom;
        statusText.Location = new Point(0, 377);
        statusText.Size = new Size(507, 15);
        statusText.TabIndex = 1;
        statusText.Text = "Status:";

        sendText.Dock = DockStyle.Fill;
        sendText.Location = new Point(0, 198);
        sendText.Multiline = true;
        sendText.ScrollBars = ScrollBars.Both;
        sendText.Size = new Size(506, 173);
        sendText.TabIndex = 0;
        sendText.Text = "";
        sendText.WordWrap = false;
        sendText.TextChanged += new EventHandler(HandleTextChange);
        sendText.Enabled = false;

        AutoScaleBaseSize = new Size(5, 13);
        ClientSize = new Size(507, 392);
        Controls.AddRange(new Control[] {statusText,
                    talkPanel});
        Text = "WinTalk";

        this.ActiveControl = sendText;    
    }   

    // When the app closes, dispose of the talker object
    protected override void OnClosed(EventArgs e){
        if(talker!=null){
            // remove our notification handler
            talker.Notifications -= new
                Talker.NotificationCallback(HandleTalkerNotifications);
           
            talker.Dispose();
        }
        base.OnClosed(e);
    }
   
    // Handle notifications from the talker object
    private void HandleTalkerNotifications(
        Talker.Notification notify, Object data){
        switch(notify){
        case Talker.Notification.Initialized:
            break;
        // Respond to status changes
        case Talker.Notification.StatusChange:
            Talker.Status status = (Talker.Status)data;
            statusText.Text = String.Format("Status: {0}", status);
            if(status == Talker.Status.Connected){
                sendText.Enabled = true;
            }
            break;
        // Respond to received text
        case Talker.Notification.Received:
            receiveText.Text = data.ToString();
            receiveText.SelectionStart = Int32.MaxValue;
            receiveText.ScrollToCaret();       
            break;
        // Respond to error notifications
        case Talker.Notification.Error:           
            Close(data.ToString());       
            break;
        // Respond to end
        case Talker.Notification.End:                                   
            MessageBox.Show(data.ToString(), "Closing WinTalk");            
            Close();
            break;
        default:
            Close();
            break;
        }
    }

    // Handle text change notifications and send talk
    private void HandleTextChange(Object sender, EventArgs e){
        if(talker != null){
            talker.SendTalk((sender as TextBox).Text);
        }       
    }  

    // Close with an explanation
    private void Close(String message){  
        MessageBox.Show(message, "Error!");       
        Close();
    }

    // Private UI elements
    private TextBox receiveText;       
    private TextBox sendText;   
    private Label statusText;
    private Talker talker;  
}

// An encapsulation of the Sockets class used for socket chatting
class Talker:IDisposable{
    // Construct a talker
    public Talker(IPEndPoint endPoint, bool client){
        this.endPoint = endPoint;
        this.client = client;

        socket = null;
        reader = null;
        writer = null;

        statusText = prevSendText = prevReceiveText = String.Empty;
    }

    // Finalize a talker
    ~Talker(){
        Dispose();
    }

    // Dispose of resources and surpress finalization
    public void Dispose(){       
        GC.SuppressFinalize(this);
        if(reader != null){
            reader.Close();
            reader = null;
        }
        if(writer != null){
            writer.Close();
            writer = null;
        }
        if(socket != null){
            socket.Close();
            socket = null;
        }       
    }

    // Nested delegat class and matchine event
    public delegate
       void NotificationCallback(Notification notify, Object data);
    public event NotificationCallback Notifications;

    // Nested enum for notifications
    public enum Notification{
        Initialized = 1,
        StatusChange,
        Received,
        End,
        Error
    }

    // Nested enum for supported states
    public enum Status{
        Listening,
        Connected
    }

    // Start up the talker's functionality
    public void Start(){
        ThreadPool.QueueUserWorkItem(new WaitCallback(EstablishSocket));
    }

    // Send text to remote connection
    public void SendTalk(String newText){               
        String send;
        // Is this an append
        if((prevSendText.Length <= newText.Length) && String.CompareOrdinal(
            newText, 0, prevSendText, 0, prevSendText.Length)==0){
            String append = newText.Substring(prevSendText.Length);
            send = String.Format("A{0}:{1}", append.Length, append);
        // or a complete replacement
        }else{
            send = String.Format("R{0}:{1}", newText.Length, newText);
        }  
        // Send the data and flush it out
        writer.Write(send);
        writer.Flush();
        // Save the text for future comparison
        prevSendText = newText;
    }

    // Send a status notification
    private void SetStatus(Status status){
        this.status = status;
        Notifications(Notification.StatusChange, status);
    }

    // Establish a socket connection and start receiving
    private void EstablishSocket(Object state){              
        try{
            // If not client, setup listner
            if(!client){
                Socket listener;
               
                try{
                    listener = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);
                    listener.Blocking = true;
                    listener.Bind(endPoint);
                    SetStatus(Status.Listening);                   
                    listener.Listen(0);
                    socket = listener.Accept();
                    listener.Close();                               
                }catch(SocketException e){
                    // If there is already a listener on this port try client
                    if(e.ErrorCode == 10048){
                        client = true;
                        endPoint = new IPEndPoint(
                            Dns.Resolve("127.0.0.1").AddressList[0], endPoint.Port);
                    }else{
                        Notifications(
                            Notification.Error,
                            "Error Initializing Socket:\n"+e.ToString());                       
                    }
                }                                   
            }

            // Try a client connection
            if(client){
                Socket temp = new
                    Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,ProtocolType.Tcp);
                temp.Blocking = true;
                temp.Connect(endPoint);
                socket = temp;
            }

            // If it all worked out, create stream objects
            if(socket != null){
                SetStatus(Status.Connected);                
                NetworkStream stream = new NetworkStream(socket);
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);
                Notifications(Notification.Initialized, this);               
            }else{
                Notifications(Notification.Error,
                    "Failed to Establish Socket");
            }

            // Start receiving talk
            // Note: on w2k and later platforms, the NetworkStream.Read()
            // method called in ReceiveTalk will generate an exception when
            // the remote connection closes. We handle this case in our
            // catch block below.
            ReceiveTalk();

            // On Win9x platforms, NetworkStream.Read() returns 0 when
            // the remote connection closes, prompting a graceful return
            // from ReceiveTalk() above. We will generate a Notification.End
            // message here to handle the case and shut down the remaining
            // WinTalk instance.
            Notifications(Notification.End, "Remote connection has closed.");
           
        }catch(IOException e){
            SocketException sockExcept = e.InnerException as SocketException;
            if(sockExcept != null && 10054 == sockExcept.ErrorCode){
                Notifications(Notification.End, "Remote connection has closed.");
            }else{
    if (Notifications != null)
     Notifications(Notification.Error, "Socket Error:\n"+e.Message);
            }               
        }catch(Exception e){             
            Notifications(Notification.Error, "Socket Error:\n"+e.Message);
        }
    }

    // Receive chat from remote client
    private void ReceiveTalk(){
        char[] commandBuffer = new char[20];
        char[] oneBuffer = new char[1];
        int readMode = 1;
        int counter = 0;       
        StringBuilder text = new StringBuilder();

        while(readMode != 0){
            if(reader.Read(oneBuffer, 0, 1)==0){
                readMode = 0;
                continue;
            }

            switch(readMode){
            case 1:       
                if(counter == commandBuffer.Length){
                    readMode = 0;
                    continue;
                }
                if(oneBuffer[0] != ':'){
                    commandBuffer[counter++] = oneBuffer[0];
                }else{
                    counter = Convert.ToInt32(
                        new String(commandBuffer, 1, counter-1));
                    if(counter>0){
                        readMode = 2;                           
                        text.Length = 0;
                    }else if(commandBuffer[0] == 'R'){
                        counter = 0;
                        prevReceiveText = String.Empty;
                        Notifications(Notification.Received, prevReceiveText);
                    }
                }
                break;
            case 2:
                text.Append(oneBuffer[0]);
                if(--counter == 0){
                    switch(commandBuffer[0]){
                    case 'R':
                        prevReceiveText = text.ToString();
                        break;
                    default:
                        prevReceiveText += text.ToString();
                        break;
                    }                   
                    readMode = 1;

                    Notifications(Notification.Received, prevReceiveText);                   
                }
                break;
            default:
                readMode = 0;
                continue;
            }           
        }       
    }

    private Socket socket;

    private TextReader reader;
    private TextWriter writer;
   
    bool client;
    IPEndPoint endPoint;

    private String prevSendText;
    private String prevReceiveText;
    private String statusText;

    private Status status;   
}

Microsoft.NET FrameworkSDK带这个例子.

时间: 2024-09-13 14:37:10

C#聊天程序的相关文章

ASP编程入门进阶(十一):Chat 聊天程序

编程|程序 通常的聊天室所采用的程序,也就是Chat程序了,其基本结构原理是不会采用到数据库的.那究竟采用什么技术呢?我们知道ASP变量当中Session变量的作用是记录单个用户的信息,并且能跟踪用户的行为. Application对象的作用则起的全局变量,可以实现站点多个用户之间在页面中共享信息的. 那可以想象,在针对当前聊天程序中,一个聊天成员即为一个Session变量,聊天成员之间的会话则当成Application变量进行共享显示,以使各成员都能看见. 那下面就采用一很经典的实例程序,进行

C#编写简单的聊天程序

程序的分析与设计 1.明确程序功能 如果大家现在已经参加了工作,你的经理或者老板告诉你,"小王,我需要你开发一个聊天程序".那么接下来该怎么做呢?你 是不是在脑子里有个雏形,然后就直接打开VS2005开始设计窗体,编写代码了呢?在开始之前,我们首先需要进行软件的分析与设计.就拿本 例来说,如果只有这么一句话"一个聊天程序",恐怕现在大家对这个"聊天程序"的概念就很模糊,它可以是像QQ那 样的非常复杂的一个程序,也可以是很简单的聊天程序:它可能只有

用WCF做聊天程序

先看一个截图. 上面的图,各位乍一看,可能会觉得是用Socket编写的聊天程序.告诉你吧,这玩意儿不是用Socket实现,呵呵,当然它的底层肯定与Socket有一定关系,我只说我的代码没有用到socket而已. 那么,除了Socket可以用于通信,还有其他技术吗?有啊,首先,如果你足够强大,用HTTP也行,但HTTP初始化的过程貌似比较慢.那么还有吗?当然了,各位还记得.NET以前有一个很X但又很少被关注的技术--Remoting.用过吧?没用过也没关系,因为它已经有替代品了. 这时候大家肯定想

用Delphi编写局域网聊天程序

Internet盛行的今天,网上聊天已成为一种时尚.同时,各单位已建成了自己的局域网:能否在局域网上实现聊天呢?可以,网上到处都有这种工具.当然,我们可以拥有自己版权的聊天工具. User Datagram Protocol (UDP)协议,是一种无连接协议.在Delphi中利用这种协议,可以轻松的编写出聊天程序,以下的程序,在Delphi 5+Pwin98中通过. 打开Delphi,新建Application,放置以下几个控件:Panel1.Panel2,其属性如下: FORM1.captio

Android蓝牙聊天程序的设计和实现

作者在这里介绍的这个实例是Google SDK中提供的一个蓝牙聊天程序,简单但信息量巨大,非常适合初学 者学习蓝牙方面的知识. 在学习这个实例前请读者仔细阅读并理解Socket的工作原理和实现机制,作 者的这篇博客中有详细的介绍: http://blog.csdn.net/dlutbrucezhang/article/details/8577810 在 Android1.x 的时候, 相关 API 非常不完善,还不能简单的使用 Bluetooth 开发,有一个开源项目可以帮助程序员使用.开发蓝牙

利用CB创建用户自己的网上聊天程序

网络是一个激动人心的领域,编写网络上的应用程序更是很多程序员向往的情节,然而编写网络程序需要掌握大量的网络传输协议.编程接口和WinSock32 API 函数,正因为如此,要完成从Windows程序员到Web程序员的转变不是一件易事.最近笔者成功的用C++ Builder 5.0 Enterprise 版编写出了网上聊天程序,特介绍如下: 一.原理:网络聊天工具需要通过TCP/IP协议,因此可以把网络聊天程序分为服务器端和客户器端两部分,其中 服务器端用以把程序转换成一个虚拟的 TCP/IP 服

Netmsg局域网聊天程序

很早就想做一个类似QQ一样的聊天程序,苦于一直没有时间,再加上觉得网络多点通信比较难做,所以这个想法就搁在一旁.最近二个月学校放假闲着无聊看了一些Tcp/ip,多线程的书,再加上以前的一点C++和VC的基础,就着手做了起来,共化了17天完成了这个程序.它包括服务器端程序NetMsgServer和客户端程序NetMsgClient.通过编程还发现原始的socket(像send,recv等)命令比VC封装好的CScoket速度要快.怎么说呢,程序不小,有3000多行的代码,错误和垃圾代码肯定也不少了

asp.net实现QQ在线聊天程序中javascript的窗口注册机制

前段时间给公司做了一个类似QQ的Asp.net在线聊天程序,与服务器之间的通讯是通过前台页面的定时 器到服务器上的临时消息目录中取得相关信息来实现的,消息结构XML,为了避免对服务器造成太大的压 力就只开了一个定时器,也没敢多开,程序完成后由于测试条件限制只在少的可怜的几台机器上测试了一 下,效果还算可以吧,目前能实现发送文字,图片,文件查看历史记录,网络硬盘等基本功能,由于聊天 窗口是嵌套在公司主框架里面的所以得实现聊天主窗体和聊天窗体以及一些其他窗体之间指针的引用问题 ,避免某个聊天窗口被打

控制台聊天程序实例代码

今天在网上找到了个控制台聊天程序实例,感觉不错,稍微做些修改,以作收藏之用. 服务器端程序代码: view plaincopy to clipboardprint? // 2009-06-28 #pragma comment(lib,"ws2_32.lib") #include #include #include using namespace std; string strCurMsg=""; void recvProc(SOCKET sockConnect) {