Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我需要一个关于winforms的建议_C#_Winforms - Fatal编程技术网

C# 我需要一个关于winforms的建议

C# 我需要一个关于winforms的建议,c#,winforms,C#,Winforms,基本上,我在控制台上制作了一个发送和接收信息的应用程序。它像聊天一样工作(它确实工作,就像我聊天一样,我检查过) 现在我在这里做了什么。我将一些代码转移到winform中,我很难理解如何将接收到的输出(readLine)打印在标签上,并通过textfield发送输入 你能给我指点方向,告诉我哪里出了错,我怎样才能让聊天有效 using System; using System.Collections.Generic; using System.ComponentModel; using Syst

基本上,我在控制台上制作了一个发送和接收信息的应用程序。它像聊天一样工作(它确实工作,就像我聊天一样,我检查过)

现在我在这里做了什么。我将一些代码转移到winform中,我很难理解如何将接收到的输出(readLine)打印在标签上,并通过textfield发送输入

你能给我指点方向,告诉我哪里出了错,我怎样才能让聊天有效

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();
        }

        private void connectServer()
        {
             connection = new TcpClient("127.0.0.1", 5000);
             sr = new StreamReader(connection.GetStream());
             sw = new StreamWriter(connection.GetStream());
        }

        void button_Click(object sender, EventArgs e)
        {

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
                sw.WriteLine(richTextBox1.Text);
                sw.Flush();            
        }

        private void label1_Click(object sender, EventArgs e)
        {
            label1.Text = sr.ReadLine();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
所以

你可以从哪里聊天到哪里相同的机器相同的网络互联网

您需要创建一个服务来处理对话,一个用于所有聊天的全局点,用于获取并保存消息

然后,您需要从每个客户机连接到该全局点,创建一个“房间”,并将该客户机作为可观察的对象连接起来,每当该“房间”中有新消息时,所有可观察的客户机都将收到传入消息的警告并更新其文本框

如果您想避免“全局点”,可以使用sockets,但是,如果客户端位于NAT后面,则可能无法使用

以下是在WCF中的完整应用程序


当用户单击标签时,它将填充标签。您希望在什么时候/什么时候将数据返回到标签?您发布的代码中缺少很多内容。例如,您从不调用
connectServer
方法。这就是问题所在,还是您只是没有发布所有代码?您可以进行点对点聊天。服务器是有用的,但不是必需的。我在“如果你想避免”之后告诉过你。。。不知道为什么投票失败,我从来没说过你不能。。。我会双向解释。