简体   繁体   English

单击按钮发送网络数据-C#中的线程之间发生冲突

[英]Sending network data on button click - conflicts between threads in c#

so i've got a Visual Studio C# WindowsForms program. 所以我有一个Visual Studio C#WindowsForms程序。 It has the ability to open up a socket in the background to let tcp clients join. 它具有在后台打开套接字以让tcp客户端加入的能力。 they can then exchange data. 然后他们可以交换数据。 the problem is that writing data to that tcp socket is a problem for me. 问题是将数据写入该tcp套接字对我来说是个问题。 reading data is successfull. 读取数据成功。

so my form1: 所以我的form1:

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private TcpListener listener;
    private Socket socket;

    ...

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.SelectedIndex = 0;
        webBrowser1.GoHome();

        const int LIMIT = 1; //5 concurrent clients

        listener = new TcpListener(IPAddress.Any, 8080);
        listener.Start();

        for (int i = 0; i < LIMIT; i++)
        {
            Thread t = new Thread(() => {
               socket = EmployeeTCPServer.accepting(listener, this);
            });

            t.Start();
        }

        toolStripStatusLabel1.Text = "Waiting for Connection";
        listBox1.Items.Add("Waiting");
    }


    ...


    private void button3_Click(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter(new NetworkStream(socket));
        sw.WriteLine("-> Stuff");
    }

The accepting of the client must be threaded in case the form continues working, which should be the source of all evil. 万一表格继续工作,必须对接受客户进行处理,这应该是万恶之源。 And my TCP Server Class: 和我的TCP服务器类:

class EmployeeTCPServer
{

    public static Socket accepting(TcpListener plistener, Form1 parent)
    {
        Socket soc = plistener.AcceptSocket();

        parent.SetText("Connection accepted");

        Stream s = new NetworkStream(soc);
        StreamReader sr = new StreamReader(s);
        StreamWriter swa = new StreamWriter(s);

        swa.AutoFlush = true; // enable automatic flushing
        swa.WriteLine("-> Connected to Iface");

        Service(plistener, parent, soc);

        return soc;
    }

    public static void Service(TcpListener plistener, Form1 parent, Socket psoc)
    {

            bool EMPFANG = true;

            Stream s = new NetworkStream(psoc);
            StreamReader sr = new StreamReader(s);
            StreamWriter swa = new StreamWriter(s);

            swa.AutoFlush = true;


            while (EMPFANG)
            {
                string name = sr.ReadLine();
                parent.LBSetText(name);

                if(name=="exit")
                {
                    swa.WriteLine("-> Exit OK");
                    EMPFANG = false;
                }
            }


            psoc.Close();

    }
}

The problem crashes when i click on "Antworten" (Button3). 当我单击“ Antworten”(Button3)时,该问题崩溃。 It says that sw is NULL, which for me means that the returning of the socket descriptor doesn't work. 它说sw为NULL,这对我来说意味着套接字描述符的返回不起作用。 I also tried to make a new method in the tcp server class (something like "send_msg_to_client(string pmessage)") but then i dont know how to call that method. 我也试图在tcp服务器类中创建一个新方法(类似“ send_msg_to_client(string pmessage)”),但后来我不知道如何调用该方法。 So which method is the right one and how can i send data on button click? 那么哪种方法是正确的,我如何在单击按钮时发送数据?

Thanks in advance for help! 在此先感谢您的帮助!

It sounds like you want a simple "chat" system with specific messages send between the client and the host. 听起来好像您想要一个简单的“聊天”系统,在客户端和主机之间发送特定的消息。

Try to have a look at this project: http://www.eng.northampton.ac.uk/~espen/CSY2026/CSY2026CSharp5.htm 尝试看看这个项目: http : //www.eng.northampton.ac.uk/~espen/CSY2026/CSY2026CSharp5.htm

Maybe it can be to some inspiration. 也许可以是一些灵感。

Let me know if it helps! 让我知道是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM