Click here to Skip to main content
See more: C#
I am having problems with my chat program in relaying information between the two clients. I have 1 program that when I launch it creates 1 client and 1 server.
 
2) user inputs port for server and launches server(s)
 
3) then the client GUI launches and the user enters a name and the port of the server they want to connect to.
 
I have it so my Client1 talks to Server2, but cannot figure out how to get the information passed to Server2 to relay to Client2's Chat Screen.
 
Server
 
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;
 
namespace RemoteServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            int channelNum;
            string temp;
 
            temp = textBoxPort.Text;
 
            channelNum = Convert.ToInt32(temp);
 
            HttpChannel ServerChannel = new HttpChannel(channelNum);
            Console.WriteLine("httpChannel Set");
            this.Close();
            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();
            string tempText;
 
            Class1 test = new Class1();
 
            tempText = test.OutputWindow();
 
            Console.WriteLine(tempText);
 
            Console.ReadKey();
            
        }
    }
}
 
Client
 
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 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");
 
            MessageBox.Show("Server connection");
        }
 

        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);
                server.test();
 
                //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)
        {
            if (textBoxUserName.Text != "")
            {
                UserName = textBoxUserName.Text;
                //ClientHttpChannel = new HttpChannel();
                //ChannelServices.RegisterChannel(ClientHttpChannel, false);

                if (textBoxPort.Text == "")
                {
                    textBoxPort.Text = "9000";
                }
 
                ConfigureServer(textBoxPort.Text);
                
                server = new Class1();
                //add user  
                server.addUserName(UserName);
                //server.test();
                //new thread
                thread = new Thread(new ThreadStart(StartServer));
                //start thread
                thread.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();
        }
 
     
        
 
        //private void backgroundWorkerTimer_DoWork(object sender, DoWorkEventArgs e)
        //{

        //}

        
        
    }
}
 
 
 
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
 
namespace ChatRoom
{
 
    public class Class1:MarshalByRefObject
    {
        // initialize Arraylist to store users signed into chat room
        private static ArrayList userNames = new ArrayList();        
        private string chatWindow; // f*** 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;
        }
        
    }
}
 
Closed because the post is not clear, or is incomplete and has not been phrased in a way that allows it to be fully understood.. Reported by RyanDev, ♥…ЯҠ…♥ on Friday, January 17, 2014 3:17am.
Posted 9-Nov-12 5:28am
Edited 14-Nov-12 0:07am
(no name)33.2K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150326.1 | Last Updated 16 Jan 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100