C# 客户端应用程序不接收来自 Linux tcp 服务器的消息

标签 c# linux tcp client

情况: 1.Linux TCP服务器 2 Windows C#客户端应用程序

服务器从客户端接收消息,但是当我尝试从服务器向客户端发送消息时没有任何反应。当我断开连接时,出现错误:

“无法从传输连接读取数据:调用 WSACancelBlockingCall 中断了阻塞操作。”它指向行 ---> string Response = swRead.ReadLine();

这是客户端代码:

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 AsynClient
{
    public partial class Form1 : Form
    {

        private TcpClient client;
        private StreamWriter swSend;
        private StreamReader swRead;

        private bool connected = false;
        string bojaTekst = "", bojaPoz = "";


        private delegate void UpdateLogCallback(string strMessage);

        private Thread thrMessag;

        public Form1()
        {
            Application.ApplicationExit += new EventHandler(OnApplicationExit);
            InitializeComponent();
      }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (connected == false)
            {
                InitializeConnection();
            }
            else
                MessageBox.Show("Vec je uspostavljenja konekcija.");
        }


        private void InitializeConnection()
        {
            client = new TcpClient();
            client.Connect("192.168.100.82", 3456);
            swSend = new StreamWriter(client.GetStream()); //prvo mora da se             konektuje, pa onda ova komanda



            thrMessag = new Thread(new ThreadStart(ReceiveMessages));
            thrMessag.Start();

            connected = true;
            btnSend.Enabled = true;
            btnDisconnect.Enabled = true;
            txtMsg.Enabled = true;
            btnConnect.Enabled = false;

        }

        private void UpdateLog(string strMessage)
        {

            txtServ.AppendText(strMessage + "\r\n");

        }

        private void ReceiveMessages()
        {
            swRead = new StreamReader(client.GetStream());

            string Response = swRead.ReadLine();

            while (connected)
            {

                // Show the messages in the log TextBox

                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] {         swRead.ReadLine() });

            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            if (listBoxTekst.SelectedIndex == -1)
                listBoxTekst.SelectedIndex = 0;

            if (listBoxPozadina.SelectedIndex == -1)
                listBoxPozadina.SelectedIndex = 0;

            bojaTekst = listBoxTekst.Text;
        bojaPoz = listBoxPozadina.Text;


        SendMessage();
    }

    private void SendMessage()
    {
     txtMsg.Text);

        if (txtMsg.Lines.Length >= 1)
        {
            swSend.WriteLine(bojaPoz + "\t" + bojaTekst + "\t" + txtMsg.Text + "\t"); 
            swSend.Flush();                     
            txtMsg.Lines = null;                
        }                                       
        txtMsg.Text = "";
    }

    private void btnDisconnect_Click(object sender, EventArgs e)
    {
        char[] quit = new char[5];
        quit[0] = 'q'; quit[1] = 'w'; quit[2] = 'w'; quit[3] = 'w'; quit[4] = '\0';
        connected = false;

        btnConnect.Enabled = true;
        btnSend.Enabled = false;
        btnDisconnect.Enabled = false;
        txtMsg.Enabled = false;

        swSend.WriteLine(quit);
        swSend.Flush();

        swSend.Close();
        client.Close();
    }

    public void OnApplicationExit(object sender, EventArgs e)
    {
        if (connected == true)
        {
            connected = false;

            swSend.WriteLine("qwww"); //proveri da li radi f-ja kako treba
            swSend.Flush();

            swSend.Close();
            client.Close();
        }
    }      
}

}

服务器端使用 read() 函数接收消息并且工作正常,但是当它使用 write() 发送时,C# 客户端不会收到消息。

最佳答案

只有一件事(可能还有其他问题): 您有一个 connected 变量,在启动监听线程后,将其值设置为 true。线程内的循环甚至不运行一次。


thrMessag = new Thread(new ThreadStart(ReceiveMessages));   
thrMessag.Start();              
connected = true; //here is your first bug, should come before the previos line

关于C# 客户端应用程序不接收来自 Linux tcp 服务器的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11052709/

相关文章:

c# - 通过字符串名称从 JSON 中获取属性(已经反序列化为类)

c# - 了解用户是否已从 C#/.NET 应用程序连接

c# - 如何在控制台应用程序中使用 Quartz.net [显示错误]?

c - 如何在 U-Boot 中添加用户自定义函数?

C: 我怎样才能将文件名列表作为字符串发送到套接字上?

c# - 使用 TCP 发送多个字符串太快了我该如何等待?

c# - YouTube C#API-如何恢复失败的上传

linux - 我可以保护两个进程之间的内存吗

linux - 在主目录中安装库

tcp - 通过 tcp 的 Webrtc 媒体?