Winforms帮助,建立聊天 [英] Winforms help, building a chat

查看:144
本文介绍了Winforms帮助,建立聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是创建一个聊天室!

我主要是一个接收信息的控制台应用程序.

what i am trying to do is create a chat!

my main is a console application that receives information.

<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            string word = "";
            savedObject saved = new savedObject();

            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");
                ThreadPool.QueueUserWorkItem(saved.ProssecClient, connection);
            }
        }
    }
}





class savedObject
{
    string word;

    public string AllWords(string sit)
    {

        word += sit + " ";

        return word;
    }


    public string word2()
    {
        return word;
    }
    StreamReader[] sr1 = new StreamReader[100];
    StreamWriter[] sw1 = new StreamWriter[100];
    int m;
    int a;
    Dictionary<string, TcpClient> con = new Dictionary<string, TcpClient>();
    Dictionary<string, StreamReader> conReader = new Dictionary<string, StreamReader>();
    Dictionary<string, StreamWriter> conWriter = new Dictionary<string, StreamWriter>();
    
   
    public void ProssecClient(object o)
    {
        ///Useless Variables
        string name = "";
        string word2 = "";
        TcpClient connection = o as TcpClient;
        if (connection == null)
        {
            return;
        }

        // Streams I/O
        StreamReader sr = new StreamReader(connection.GetStream());
        sr1[a++] = new StreamReader(connection.GetStream());
        sw1[m++] = new StreamWriter(connection.GetStream());
        //Console.WriteLine("Type in Your Name: ");
       // name = Console.ReadLine();

        name = sr.ReadLine();
        Console.WriteLine(name+" has joined the server");

        //Info----> Dictionary


        con.Add(name, connection);
        conReader.Add(name, sr1[a]);
        conWriter.Add(name, sw1[m]);


        //Read & Write to everyone!!!!!
        try
        {
            while (true)
            {
                int i = 0;
                
                word2 = AllWords(sr.ReadLine());

                for (i = 0; i < 10; i++)
                {
                    if (sw1[i] != null)
                    {
                        sw1[i].WriteLine(name+" : "+word2);

                        sw1[i].Flush();

                    }

                }

            }
        }
        catch { Console.WriteLine(  name+ " client left"); }
    }

}

</code> 



此代码与客户代码一起使用.这是客户端代码.它能够从
发送和接收消息
其他用户:



This code works with the clients code. here is the clients side code. it is capable of sending and receiving messages from

other users:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
       
            string name2 = "";
            while (true)
            {
                Console.WriteLine("Enter your name and press submit");
                name2=Console.ReadLine();
                if (name2 != "")
                {
                    sw.WriteLine(name2);
                    break;
                }
            }
            Console.WriteLine("Loop is over");
            Thread t2 = new Thread(Reader);
           
            t2.IsBackground = true;
            t2.Start(connection);

            while (true)
            {
                sw.WriteLine(Console.ReadLine());
                sw.Flush();
            }
        }
  


        public static void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
                return;
            StreamReader sr = new StreamReader(con.GetStream());
            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine();
                }

                Console.WriteLine(sr.ReadLine());
                Console.WriteLine();
            }
        }
    }
}



我的目标是将客户端代码转换为winform.到目前为止,我做到了,但是当我按下按钮时它卡住了:



My goal here is to convert the client side code to a winform. so far i did that, but it gets stuck when i press the button:

<pre lang="msil">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.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient connection;
        StreamReader sr;
        StreamWriter sw;
        public Form1()
        {
            InitializeComponent();
            try
            {
                connection = new TcpClient("127.0.0.1", 5000);
                sr = new StreamReader(connection.GetStream());
                sw = new StreamWriter(connection.GetStream());
            }
            catch
            {
                MessageBox.Show("Couldnt form a connection");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                sw.WriteLine(richTextBox1.Text);
                sw.Flush();
                richTextBox2.Text = sr.ReadLine();
            }
            catch
            {
                MessageBox.Show("Cant Type Message");
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
        }
    }
}







我不需要我不理解的代码示例,只需要使winform发送和接受数据即可.







i dont need a sample of a code that i dont understand, i just need to put make the winform send and accept data.

推荐答案

WinForms完全不同东西:

控制台应用程序通常是过程性的:循环和明显的执行流程.

WinForms不是:它是事件驱动的.这意味着事情发生的顺序在很多时候都超出了您的控制范围.此外,您将无法再循环运行,因为其他事件将不会发生.

因此,当您使用诸如sr.ReadLine之类的程序例程时,必须确保那里有要接收的数据.如果没有,一切都将挂起,直到有为止.我的意思是一切.按钮不起作用,显示不更新,等等.

您需要重新开始,忘记您的聊天应用程序一段时间,然后设法掌握WinForms应用程序中发生的情况.
WinForms is completely different way of doing things:

A console app is generally procedural: loops, and an obvious execution flow.

WinForms isn''t: it is event driven. That means that the order in which things happen is outside your control much of the time. Additionally, you can''t run in loops any more, because other events can''t happen.

So when you use a procedural routine such as sr.ReadLine you have to be sure there is data there to receive. If there isn''t, everything will hang until there is. And I mean everything. Buttons won''t work, displays won''t update, etc.

You need to go back to the beginning, forget your chat application for a while, and try to get your head around what happens in WinForms apps.


我认为您需要将数据转换为作为字节数组发送/接收.这就是通过网络发送/接收数据时它的工作方式.下面的文章清楚地说明了这一点.

使用C#的TCP/IP聊天应用程序 [
I think you need to convert the data to be send/receive as byte array.That''s how it works when you send/receive data over the network.The below article clearly explains this.Take a look.

TCP/IP Chat Application Using C#[^]


这篇关于Winforms帮助,建立聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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