0

我正在尝试让我的聊天系统正常工作。每个应用程序都有一个客户端/服务器启动。我让 Client1 与 Server 2 交谈,让 Client2 与 Server1 交谈,但我不知道如何将发送到服务器的信息打印到其相应客户端的聊天窗口。

聊天室

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace ChatRoom
{

public class Class1:MarshalByRefObject
{
    // initialize Arraylist to store users signed into chat room
    private static ArrayList userNames = new ArrayList();        
    private static string chatWindow; // fuck u, now ye be a static var

    public void sendMessage(string message)
    {
        Console.WriteLine(message);
        //if (message != null)
       // {                                
                //add to chat window
            chatWindow += message + Environment.NewLine;                 
        //}
    }
    //public void test()
    //{
    //    Console.WriteLine("pizza");
    //}
    public void addUserName(string UserName)
    {
        String test = UserName;
        Console.WriteLine(test);
        userNames.Add(test);
        Console.WriteLine("0");
    }

    public void deleteUserName(string userName)
    {
        if (userName != null)
        {
            userNames.Remove(userName);
        }
    }

    public ArrayList userList()
    {
        return userNames; // userNames
    }
    public string OutputWindow()
    {
        return chatWindow;
    }

}
}

服务器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using ChatRoom;
using System.Runtime.Remoting.Channels.Http;
using RemoteServer;

namespace RemoteServer
{
    public partial class Form1 : Form
    {
    private int channelNum;
    private static string temp;

    public Form1()
    {
        InitializeComponent();
    }

    private void buttonConfirm_Click(object sender, EventArgs e)
    {

        temp = textBoxPort.Text;

        channelNum = Convert.ToInt32(temp);

        HttpChannel ServerChannel = new HttpChannel(channelNum);
        Console.WriteLine("httpChannel Set");

        this.Close(); // close GUI

        ChannelServices.RegisterChannel(ServerChannel, false);
        Console.WriteLine("channel registered");

        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "ChatRoom",
            WellKnownObjectMode.SingleCall);
        Console.WriteLine("working");
        Console.WriteLine("Port number is " + channelNum);
        Console.ReadKey();


    }

}
}

客户

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChatRoom;
using RemoteServer;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;
using System.Diagnostics;
using System.Timers;

namespace ClientServerGui
{
    public partial class Form1 : Form
    {
    private Class1 server;
    private Thread thread;
    //private Thread thread2;
    //private HttpChannel ClientHttpChannel;
    private string UserName;


    public Form1()
    {
        InitializeComponent();
    }

    public static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }


    private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void ConfigureServer(string port)
    {
        HttpClientChannel ClientChannel = new HttpClientChannel();
        ChannelServices.RegisterChannel(ClientChannel, false);

        RemotingConfiguration.RegisterWellKnownClientType(typeof(Class1), "http://localhost:"
                + textBoxPort.Text + "/ChatRoom");
    }

    private void buttonSend_Click(object sender, EventArgs e)
    {
        //ArrayList displayUsers = server.userList();
        //string send = textBoxSend.Text;
        //textBoxChatWindowDisplay.Text += send;

        //combine User's name with input text
        string send = UserName + ": " + textBoxSend.Text;
        while( textBoxSend.Text != "")
        {
            //send message
            server.sendMessage(send);

            //Clear and focus to start new message
            textBoxSend.Clear();
            buttonSend.Focus();
        }

        textBoxChatWindowDisplay.Text += send;
        textBoxChatWindowDisplay.Text = server.OutputWindow();
        //textBoxChatWindowDisplay.Text += "\n";


        //listBoxUserList.Items.Clear();
        //    foreach (string x in displayUsers)
        //    {
        //        listBoxUserList.Items.Add(displayUsers);                    
        //    }

    }

    public void chatReloadTimer_Tick(object sender, EventArgs e)
    {

        textBoxChatWindowDisplay.Text = server.OutputWindow();
        StartServer();

    }

    private void buttonConnect_Click(object sender, EventArgs e)
    {
        //RemoteServer.Form1 port = new RemoteServer.Form1();


        //string Serverport = port.ServerPort(); 

        if (textBoxUserName.Text != "")
        {
            UserName = textBoxUserName.Text;
            //ClientHttpChannel = new HttpChannel();
            //ChannelServices.RegisterChannel(ClientHttpChannel, false);

            if (textBoxPort.Text == "")
            {
                textBoxPort.Text = "9000";
            }
            //server1
            ConfigureServer(textBoxPort.Text);
            //server2
           // ConfigureServer(Serverport);

            server = new Class1();
            //add user  
            server.addUserName(UserName);
            //server.test();
            //new thread
            thread = new Thread(new ThreadStart(StartServer));
            //start thread
            thread.Start();

            //new thread
            //thread2 = new Thread(new ThreadStart(StartServer2));
            //start thread
            //thread2.Start();

            buttonSend.Focus();

            System.Windows.Forms.Timer chatReloadTimer = new System.Windows.Forms.Timer();
            chatReloadTimer.Interval = (100); // .1 seconds refresh
            chatReloadTimer.Tick += new EventHandler(chatReloadTimer_Tick);
            chatReloadTimer.Start();
        }


    }

    private void StartServer()
    {
        string currentChatWindow;

        //set array list to capture people joining server
        ArrayList usersArrayList = server.userList();

        //** BAD FOR CHATROOM STYLE****
        //if (listBoxUserList.ItemHeight > 0)//if not enmpy kill all
        //{
        //    //empty listbox
        this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Clear(); }));
        //}

        // Add all users to listbox
        //while (usersAL != null)
        //{
        foreach (string x in usersArrayList)
            {
                //listBoxUserList.Items.Add(UserName);
                this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Add(x); }));
            }
        //}

        //display data to user
        currentChatWindow = server.OutputWindow();
        //textBoxChatWindowDisplay.Text = currentChatWindow;
        this.Invoke(new MethodInvoker(delegate() { textBoxChatWindowDisplay.Text = currentChatWindow; }));

        //listBoxUserList.Items.Add(
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        //Boolean on = true;
        Process start = new Process();

        start.StartInfo.FileName = "RemoteServer";
        start.Start();

        MessageBox.Show("Continue?", "Continue", MessageBoxButtons.OKCancel);


        //while (on == true)
        //{
        //    Thread.Sleep(1);
        //    textBoxChatWindowDisplay.Text = server.OutputWindow();
        //}
        buttonSend.Focus();
    }




}
}
4

0 回答 0