国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Chat Server with Client implemented in C#

2024-07-21 02:20:17
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • chat server with client implemented in c#

    submitted by date of submission
    nanu  01/19/2000

      
    download: workingserver.zip 11 kb
    the main heart of the program is taken from the sample program of gopalan suresh raj  modified as per requirement & presented in front of you.... hope it would be helpful for somebody

    how to run the chat server and the client?

    (1) first start the server.exe
    (2) next start formclient.exe
    (3) give you nick name "xyz" & click the connect button .... you will be connected
    to the server & a message will be displaed in the yellow box
    (4) run formclient.exe again with start-->run-->formclient.exe & give another
    nick name for trial & error.








    the server code: the server code is implemented in newserver.cs.

    /**
    * article: channels and .net remoting
    * copyright (c) 2000, gopalan suresh raj. all rights reserved.
    * file: newserver.cs
    * ... thanks saurabh for great help to make it lively your knowledge rules ....
    * ... thanks mahesh for loading it on site .... it rocks
    * ... thanks to you for reading it you know better who you are :)
      
    * ... the main heart of the program is taken from the sample program of gopalan suresh raj
    * ... modified as per requirement & presented in front of you.... hope it would be helpful for somebody
    */
    using system;
    using system.io;
    using system.collections;
    using system.runtime.remoting;
    using system.winforms ;
    using system.runtime.remoting.channels.http;
    namespace com.icommware.publishsubscribe {
      
    public class topic : marshalbyrefobject {
      
      
    arraylist userlist = new arraylist() ;
    arraylist textboxlist = new arraylist() ;
    arraylist listboxlist = new arraylist() ;
    ///<summary>
    /// input : string username (by value) and richtextbox , listbox (by refrence)
    /// output: bool 'true' if username is unique else 'false '
    ///</summary>
    public bool adduser(string user, ref richtextbox textbox1, ref listbox listbox1)
    {
    //check is username is present in the userlist 'arraylist'
    bool test=userlist.contains(user) ;
    if(test)
    {
    //if present then give the message to use and return 'false'
    textbox1.text+="this nick has already been taken, try changing your nick /n" ;
    return false ;
    }
    else{
    //user is unique hence add him to the userlist
      
    userlist.add(user) ;
    //add the richtextbox reference to textboxlist
    textboxlist.add(textbox1);
    //add to existing users list
    foreach(listbox lb in listboxlist)
    {
    lb.items.add(user) ;
    }
    //add the listbox reference to listboxlist
    listboxlist.add(listbox1) ;
    //send to message only to the client connected
    textbox1.text+="connected to server... /n" ;
    //send message to everyone .
    sendmessage(user+" has joined chat") ;
    //add all the usernames in the listbox of the client
    foreach(string users in userlist)
    {
    listbox1.items.add(users) ;
    }
      
    return true ;
    }
      
    }
      
    ///<summary>
    /// input: string username
    /// it is called when the user quits chat
    ///</summary>
    public void removeuser(string user)
    {
    //check is the user is present in the userlist
    try{
    if(userlist.contains(user))
    {
    //get the position of user in userlist
    int i=userlist.indexof(user) ;
    //remove user from userlist
    userlist.remove(user) ;
    //remove user's richtextbox reference from textboxarray
    textboxlist.removeat(i) ;
    //remove user's listbox refrence from listboxarray
    listboxlist.removeat(i) ;
    //inform all users about user quiting
    sendmessage(user+" has quit chat") ;
    //emove the user from all users listbox
    foreach(listbox lb in listboxlist)
    {
    lb.items.remove((object)user) ;
      
    }
    }
    }
    catch(exception ed)
    {
    console.writeline(ed) ;
    }
    }
    ///<summary>
    /// input: string message
    /// it sends the message to all users connected to the server
    ///<summary>
    public void sendmessage(string message)
    {
    //write the message on the server console
    console.writeline ("added message : {0}", message);
    //for each user connected, send the message to them
    foreach(richtextbox rf in textboxlist)
    {
    rf.text+=message+"/n" ;
    }
      
    }
    }
      
    public class theserver {
      
    public static void main () {
      
    int listeningchannel = 1099;
    // create a new http channel that listens on port listeningchannel
    // tcpchannel channel = new tcpchannel (listeningchannel);
    httpchannel channel = new httpchannel (listeningchannel);
    // register the channel with the runtime
    channelservices.registerchannel (channel);
    // expose the calculator object from this server
    remotingservices.registerwellknowntype ("server",
    "com.icommware.publishsubscribe.topic",
    "topic.soap",
    wellknownobjectmode.singleton);
    // keep the server running until the user presses enter
    console.writeline ("the topic server is up and running on port {0}", listeningchannel);
    console.writeline ("press enter to stop the server...");
    console.readline ();
    }
    }
    }
    // that's all ... it's all easy if you are willing understand !
    // make a printout of it & open your .net sdk for better understanding :)



    the client code: the client code reside in newformclient.cs.

    /**
    * article: channels and .net remoting
    * copyright (c) 2000, gopalan suresh raj. all rights reserved.
    * ... thanks saurabh for great help to make it lively .... thanks :)
    * ... thanks mahesh for loading it on site .... it rocks :)
    * ... thanks to you for reading it you know better who you are :)
      
    * ... the main heart of the program is taken from the sample program of gopalan suresh raj
    * ... modified as per requirement & presented in front of you.... hope it would be helpful for somebody
    */
      
    // win32form1.cs
    namespace win32form1namespace {
      
    using system;
    using system.drawing;
    using system.componentmodel;
    using system.winforms;
    using system.runtime.remoting;
    using system.runtime.remoting.channels.http;
    using com.icommware.publishsubscribe;
    using system.collections;
      
      
    /// <summary>
    /// summary description for win32form1.
    /// </summary>
    public class win32form1 : system.winforms.form {
    /// <summary>
    /// required by the win forms designer
    /// </summary>
    private system.componentmodel.container components;
    private system.winforms.button nickname_btn;
    private system.winforms.textbox nickname_txtbox;
    private system.winforms.label nickname;
    private system.winforms.button btnlistname;
    private system.winforms.button button1;
    private system.winforms.textbox textbox2;
    private system.winforms.label label1;
    private system.winforms.groupbox groupbox1;
    //useing richtextbox insted of textbox since it supports many more enhancements
    private system.winforms.richtextbox textbox1;
    private system.winforms.listbox listbox1;
    private topic topic ;
    private string username ;
    private bool connected=false ;
    private httpchannel channel ;
    public win32form1() {
    // required for win form designer support
    initializecomponent();
    // todo: add any constructor code after initializecomponent call
    }
    /// <summary>
    /// clean up any resources being used
    /// </summary>
    public override void dispose() {
    quitclient() ;
    base.dispose();
    components.dispose();
    }
    /// <summary>
    /// the main entry point for the application.
    /// </summary>
    public static void main(string[] args) {
    application.run(new win32form1());
    }
      
    /// <summary>
    /// required method for designer support - do not modify
    /// the contents of this method with an editor
    /// </summary>
    private void initializecomponent()
    {
    this.components = new system.componentmodel.container();
    this.button1 = new system.winforms.button();
    this.listbox1 = new system.winforms.listbox();
    this.groupbox1 = new system.winforms.groupbox();
    this.label1 = new system.winforms.label();
    this.nickname_btn = new system.winforms.button();
    this.textbox2 = new system.winforms.textbox();
    this.nickname = new system.winforms.label();
    this.nickname_txtbox = new system.winforms.textbox();
    this.btnlistname = new system.winforms.button();
    this.textbox1 = new system.winforms.richtextbox();
      
    //@design this.trayheight = 0;
    //@design this.traylargeicon = false;
    //@design this.trayautoarrange = true;
    button1.location = new system.drawing.point(136, 64);
    button1.size = new system.drawing.size(72, 24);
    button1.tabindex = 4;
    button1.text = "send";
    button1.click += new system.eventhandler(button1_click);
      
    listbox1.location = new system.drawing.point(320, 0);
    listbox1.size = new system.drawing.size(152, 329);
    listbox1.tabindex = 0;
      
    groupbox1.location = new system.drawing.point(16, 232);
    groupbox1.tabindex = 0;
    groupbox1.tabstop = false;
    groupbox1.text = "add message";
    groupbox1.size = new system.drawing.size(240, 104);
      
      
    label1.location = new system.drawing.point(16, 32);
    label1.text = "message";
    label1.size = new system.drawing.size(56, 23);
    label1.tabindex = 0;
      
    nickname_btn.location = new system.drawing.point(16, 392);
    nickname_btn.size = new system.drawing.size(224, 24);
    nickname_btn.tabindex = 2;
    nickname_btn.text = "connect";
    nickname_btn.click += new system.eventhandler(nickname_btn_click);
      
    textbox2.location = new system.drawing.point(72, 32);
    textbox2.tabindex = 3;
    textbox2.size = new system.drawing.size(160, 20);
    textbox2.click += new system.eventhandler(button1_click);
      
    nickname.location = new system.drawing.point(16, 360);
    nickname.text = "nick name";
    nickname.size = new system.drawing.size(72, 16);
    nickname.tabindex = 0;
      
    nickname_txtbox.location = new system.drawing.point(88, 360);
    nickname_txtbox.tabindex = 1;
    nickname_txtbox.size = new system.drawing.size(152, 20);
    nickname_txtbox.click += new system.eventhandler(nickname_btn_click);
      
    btnlistname.location = new system.drawing.point(344, 336);
    btnlistname.size = new system.drawing.size(96, 32);
    btnlistname.tabindex = 5;
    btnlistname.text = "disconnect";
    btnlistname.enabled=false ;
    btnlistname.click += new system.eventhandler(btnlistname_click);
      
    textbox1.location = new system.drawing.point(24, 8);
    textbox1.multiline = true;
    textbox1.tabindex = 0;
    textbox1.size = new system.drawing.size(288, 208);
    textbox1.backcolor = system.drawing.systemcolors.info;
    this.text = "client";
    this.autoscalebasesize = new system.drawing.size(5, 13);
    this.clientsize = new system.drawing.size(496, 425);
      
      
    groupbox1.controls.add(button1);
    groupbox1.controls.add(textbox2);
    groupbox1.controls.add(label1);
    this.controls.add(nickname_btn);
    this.controls.add(nickname_txtbox);
    this.controls.add(nickname);
    this.controls.add(btnlistname);
    this.controls.add(groupbox1);
    this.controls.add(textbox1);
    this.controls.add(listbox1);
    }
    ///<summary>
    /// does the cleaning up when client diconnects
    ///</summary>
    private void quitclient()
    {
    if(topic!=null)
    {
    try{
    //remove the user from the server
    topic.removeuser(username) ;
    connected=false ;
    //set the buttons
    btnlistname.enabled=false;
    nickname_txtbox.enabled=true ;
    nickname_btn.enabled=true ;
    listbox1.items.clear() ;
    //unregister to channel so we can connect some other place
    channelservices.unregisterchannel(channel) ;
    }
    catch(exception ed)
    {
    //this exception will occur when a 2 clients are running on the same machine and
    //one try's to disconnect.
    //since both are using the same port (i.e. '0') to conect to server
    //if you see above we are calling 'channelservices.unregisterchannel(channel)'
    //this will try to close the port '0' but since another client is
    //using this port a exception will occur
    console.writeline(ed) ;
    }
    }
    }
      
    protected void nickname_btn_click(object sender, system.eventargs e)
    {
    try{
      
      
    // codeing manipulation starts here ...
    if(!connected)
    {
    int listeningchannel = 0;
    // create and register a channel to communicate to the server
    // the client will use the port passed in as args to listen for callbacks
    channel = new httpchannel (listeningchannel);
    channelservices.registerchannel (channel);
      
    // create an instance on the remote server and call a method remotely
    topic = (topic) activator.getobject (typeof (topic), // type to create
    "http://localhost:1099/topic.soap" // uri
    );
      
    username = nickname_txtbox.text ;
    //add the user to the server
    bool check = topic.adduser(nickname_txtbox.text, ref textbox1 ,ref listbox1) ;
      
    if(!check)
    {
    //if server rejected user then do cleanup
    connected=false ;
    btnlistname.enabled=false;
    nickname_txtbox.enabled=true ;
    nickname_btn.enabled=true ;
    channelservices.unregisterchannel (channel);
    topic=null ;
    nickname_txtbox.text="" ;
    }
    else{
    //set up variables and buttons
    connected=true ;
    btnlistname.enabled=true;
    nickname_txtbox.enabled=false ;
    nickname_btn.enabled=false ;
    this.text="client "+username+" connected" ;
    }
    }
      
      
    }
    catch(exception ed)
    {
    messagebox.show(this, "exception occured"+ed.tostring());
    }
      
      
    }
      
    protected void btnlistname_click(object sender, system.eventargs e)
    {
    //call the mothod to quit rom server
    quitclient() ;
    }
    protected void button1_click(object sender, system.eventargs e)
    {
    //send the message to everyone
    if(textbox2.text!=""){
    topic.sendmessage(username+": "+textbox2.text) ;
    textbox2.text="";
    }
    }
      
    }
    }
    // that's all ... it's all easy if you are willing understand !
    // make a printout of it & open your .net sdk for better understanding :)



    if you have any comments or doubts or find something terrible wrong in program or comments. please feel free to e-mail me !


    --------------------------------------------------------------------------------

    about the author: everyday i need to satisfy my desire of learning especially in the computer world, and in the same time i'm really satisfied to share with  others what i've learnt. unfortunately not all programmers today think like me....but a lot of us keep  the learning and teaching activity up! hey don't forget to e-mail me [email protected]

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 库车县| 久治县| 东乌| 调兵山市| 静宁县| 烟台市| 定日县| 图片| 咸阳市| 克山县| 剑河县| 博野县| 太保市| 涿州市| 海伦市| 嘉峪关市| 吴江市| 濮阳市| 淮南市| 芦山县| 广南县| 奉新县| 台安县| 乌兰县| 通辽市| 博白县| 松原市| 新丰县| 玛纳斯县| 田林县| 枣阳市| 平潭县| 六安市| 龙岩市| 鄂尔多斯市| 鄂托克前旗| 唐山市| 开平市| 亚东县| 丰原市| 阿拉善左旗|