Click here to Skip to main content
Sign Up to vote bad
good
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;
        }
        
    }
}
 
Posted 9 Nov '12 - 4:28
Edited 13 Nov '12 - 23:07
digimanus22.7K

Comments
Jason Gleim - 9 Nov '12 - 11:17
Just curious... do you have Windows Firewall disabled on your test machines?
Member 9583355 - 9 Nov '12 - 11:29
yes i do, currently I am running it as local host. Launching the app twice on the same machine.
Jason Gleim - 9 Nov '12 - 11:37
My suggestion would be to split the server and the client out from each other. Make the server it's own application and launch it prior to launching your test clients. Isolating the components will help figure out what is going on. Also, since you are using http, have to tried instrumenting it with Fiddler? You can proxy apps like this through fiddler (even if they don't support a proxy) by setting the service endpoint to http://ipv4.fiddler. (instead of localhost) You should be able to see the calls between the client(s) and the server that way.
Member 9583355 - 9 Nov '12 - 12:21
The servers start before the Clients start, I have a hold on the Client GUI loading. wouldnt fiddler be a 3rd party part needing to be installed on another machine if i transfer to the program? Would there be the way for the Servers to forward the Messages on to the other Client? or have the servers talk to each other and combine the messages
Jason Gleim - 9 Nov '12 - 12:30
Why do you have a server for each client? That is probably your issue... I don't see how your servers are talking to each other (that isn't jumping out to me at least). If you create more than one server, you have to deal with cross-process communication and I don't see any of that framework in your app. Plus, just as a general point of design, wouldn't it make a lot more sense to have a single server that all of your clients talked to rather than having each client talk to its own server just to have the servers talk? Seems like a wasted extra layer there. Fiddler is a debugging tool. You use it while you are working on your program and change back the service references when you are done. You can't (and wouldn't) ship it with your program. It is sort of like WireShark but it is designed to spy on http traffic and not packets on the wire like WireShark. You may want to look at the fiddler web site to learn more about it. For anyone doing any type of http stuff, they should have it installed.
Member 9583355 - 9 Nov '12 - 13:07
I got the server with multiple clients working easily. But was attempting to have 1-1 per a launch for private chatting purposes ideas. I thought It would of been easy to just have a Client pass to a server then pass to a client, but seems to be a lot harder and it interested me in how to get it to work

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 233
1 Sergey Alexandrovich Kryukov 208
2 Rohan Leuva 195
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,474
1 OriginalGriff 6,714
2 CPallini 3,603
3 Rohan Leuva 2,853
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid