C#聊天程序

页面导航:首页 > 软件编程 > vb.net > C#聊天程序

C#聊天程序

来源: 作者: 时间:2016-02-05 12:32 【

/*===================================================================== 文件: Wintalk.cs 摘要: 演示如何使用 .NET创建聊天程序=======
 

/*=====================================================================
  文件:      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...] "+
            "  /L  [port] -- Listens on a port.  Default:  5150 "+
            "  /C  [address] [port] -- Connects to an address and port. "+
            "Example Server - "+
            "Wintalk /L "+
            "Example Client - "+
            "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();       

Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <