聊天应用程序C#中的错误 [英] Error in Chat Application C#

查看:69
本文介绍了聊天应用程序C#中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!好吧,我对C#编程很幼稚.我曾尝试过使用由三个应用程序组成的C#聊天服务器..

1个服务器-端口号为TCP的TCP服务器8085

2服务类别-提供服务

3客户访问服务

< color>错误是每当我运行客户端应用程序时,exe文件都会挂起& Windows没有生成响应消息.它不提供任何例外..所以不知道错误在哪里!请帮我!!!这很紧急!!提前谢谢.. !!

服务器运行正常..没问题.

我也在粘贴代码..对于我已构建的3个应用程序中的每一个.


1个服务器代码

我曾使用TCP服务器通过端口8085&提供必要的设置.它运行正常.

2 < color>服务等级

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>客户部分----


表格1



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



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]整理出代码块-OriginalGriff [/edit]



[edit]Code blocks sorted out - OriginalGriff[/edit]

推荐答案

在调试器下运行客户端并检测其挂起的行.正确举报.
如果很难做到,请使用类System.Diagnostics.EventLog的系统日志"记录所有重要步骤.
您的线程在哪里?
甚至不要玩不用线程就可以创建网络应用程序的想法.所有网络都应放在单独的线程中:一个在客户端上,至少两个在服务器上(一个用于接受新连接,一个用于通信).
请查看我过去的设计草图.答案:
来自同一端口号的多个客户端 [如何获取keydown事件在vb.net中的不同线程上操作 [如何在文件夹下创建事件日志 [ ^ ].

您可以调试同一台计算机上的客户端和服务器部分(如果需要,可以调试一个以上的客户端),在同一台计算机上的日志记录会将所有日志放在一起,因此您将看到所有步骤的确切顺序,这非常方便.

附带说明:使用Microsoft命名约定.接口必须命名为"IChatClient"行,而不是"Class1"行.切勿留下任何自动生成的名称,尤其是"Form1","Form2"或"Label1"之类的名称,然后重命名并提供语义名称.不要像使用硬常量那样对任何东西进行硬编码,尤其是字符串(为了简短起见,请在CodeProject上报告问题,为了简洁起见).

您确定这是客户端界面吗?如果是"AddUser"(再次,遵循命名约定!),也许这是服务器的?

—SA
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


这篇关于聊天应用程序C#中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆