Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey! Well I am naive to C# Programming. I had tried a Chat Server in C# comprising of three applications..

1 Server- TCP Server with port no. 8085

2 Service Class- Providing the services

3 Client- accessing Services

<color>The error is that in Client Application whenever i run it, the exe file gets hang & not responding message is generated.. by windows. It doesnot provide me any exceptions.. So dont know where the error is! PLease help me!!! Its Urgent!! THanks in advance..!!

Server is running ok.. no problem in it.

I am pasting the code also.. for each of the 3 applications i have build.


1 Server Code

I had used TCP server to provide the necessary settings with port 8085 & its running properly.

2<color> SERVICE CLASS

namespace ServiceClass
{       public class Class1 : MarshalByRefObject
        {
            Dictionary<string, string> Dc = new Dictionary<string, string>();
            public void adduser(string name)
            {
                Dc.Add(name, " ");
            }
            public void setmessage(string from, string to, string message)
            {
                Dc[from] = Dc[from] + from + ":" + message + Environment.NewLine;
                Dc[to] = Dc[to] + from + ":" + message + Environment.NewLine;
            }
            public string getmessage(string u)
            {
                return Dc[u];
            }
            public ICollection<string> users()
            {
                return Dc.Keys;
            }   }}

3 <color> CLIENT PART----


FORM 1


namespace Trial
{
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string UserName;
        private void button1_Click_1(object sender, EventArgs e)
        {
            UserName = textBox1.Text;
            Form2 ob = new Form2();
            ob.Show();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ServiceClass.Class1 obj = (ServiceClass.Class1)Activator.GetObject(typeof(Class1), "Tcp://localhost:8085/ServiceClass");} } }


FORM2

namespace Trial
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        ServiceClass.Class1 obj = (ServiceClass.Class1)Activator.GetObject(typeof(Class1), "Tcp://localhost:8085/ServiceClass");
       
        private void Form2_Load_1(object sender, EventArgs e)
        {
            
            label1.Text = Form1.UserName;
            obj.adduser(Form1.UserName);
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            obj.setmessage(label1.Text, comboBox1.SelectedItem.ToString(), textBox1.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ICollection<string> ic = obj.users();
            foreach (string s in ic)
            {
                if (comboBox1.Items.Contains(s))
                {
                    comboBox1.Items.Add(s);
                }
                if (comboBox1.SelectedItem != null)
                {
                    textBox1.Text = obj.getmessage(Form1.UserName);
                }
            }
        }

INTERFACE IMPLEMENTED IN CLIENT PART
namespace Trial
{
  public   interface Class1
    {
        void adduser(string name);

        void setmessage(string from, string to, string message);

        string getmessage(string u);

        ICollection<string> users();
    }
}


[edit]Code blocks sorted out - OriginalGriff[/edit]
Posted
Updated 13-Apr-11 23:52pm
v2

1 solution

Run client under debugger and detect the line where it hangs. Report it properly.
If it is difficult to do, log all important step using System log using the class System.Diagnostics.EventLog.

Where are your threads?
Don't even play with the idea of making networking application without threading. All networking should be in a separate threads: one on clients, at least two on server (one to accept new connections, one for communications).
Please see my design sketch for this in my past Answers:
Multple clients from same port Number[^].

Here you will find a collection of Answer on threading, a lot of useful details:
How to get a keydown event to operate on a different thread in vb.net[^].

For more convenient use of logging, use this:
How to create event log under a folder[^].

You can debug client and server part of the same machine (with more then one client, if you want), the logging in the same machine will put all logs together, so you will see exact sequence of all steps, which is very convenient.

A side note: use Microsoft naming conventions. Interface must be named something line "IChatClient", not "Class1". Never leave any of the auto-generated names, especially like "Form1", "Form2" or "Label1", rename then and give semantic names. Don't hard-code anything with immediate constant as you do, especially strings (for report of a problem at CodeProject it is acceptable, used for brevity).

Are you sure this is client interface? If "AddUser" (again, follow naming conventions!), maybe, this is a server's?

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900