.net 无法使用SimpleTCP发送数据

yzuktlbb  于 2023-02-17  发布在  .NET
关注(0)|答案(2)|浏览(143)

我正在尝试创建一个TCP/IP客户端/服务器应用程序,该应用程序可以在我的计算机上运行。服务器似乎工作正常,但客户端无法发送任何数据。
下面是我为客户端编写的代码:

using SimpleTCP;
using System.Text;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // implementing simple tcp client
        SimpleTcpClient client;
        //SimpleTcpServer server;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnConnect.Enabled = false;
           // server.Start();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //btnSend.Enabled = false;
            client = new SimpleTcpClient();
            client.StringEncoder = Encoding.UTF8;
            client.DataReceived += Client_DataReceived;
        }

        private void Client_DataReceived(object? sender, SimpleTCP.Message e)
        {
            textStatus.Invoke((MethodInvoker)delegate ()
            {
                textStatus.Text += e.MessageString;
                
            });
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            client.Write(textMessage.Text);
         
        }
    }
}

下面是服务器的代码:

using SimpleTCP;
using System.Net;
using System.Text;
using System.Windows.Forms;

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

        private void button2_Click(object sender, EventArgs e)
        {
            if (server.IsStarted)
            {
                server.Stop();
            }
        }
        // implementing simpletcpserver class
        SimpleTcpServer server;
        
        private void Form1_Load(object sender, EventArgs e)
        {
            server = new SimpleTcpServer();
            server.Delimiter = 0x13;
            server.StringEncoder = Encoding.UTF8;
            server.DataReceived += Server_DataReceived;

                }

        private void Server_DataReceived(object? sender, SimpleTCP.Message e)
        {
            textStatus.Invoke((MethodInvoker)delegate ()
            {
                textStatus.Text += e.MessageString;
                e.ReplyLine(string.Format("You said:{0}",e.MessageString));
            });
            
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            textStatus.Text += "Server starting ...";
            //System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(textHost.Text));
            IPAddress ip = IPAddress.Parse(textHost.Text);
            server.Start(ip, Convert.ToInt32(textPort.Text));

        }
    }
}

我得到的错误如下:
System.Exception:'无法向空TcpClient发送数据(请检查是否调用了Connect)'

flmtquvp

flmtquvp1#

您缺少将客户端连接到服务器的调用,存在一个Connect方法,该方法的参数用于提供地址和端口
https://github.com/BrandonPotter/SimpleTCP/blob/master/SimpleTCP/SimpleTcpClient.cs#L34

8e2ybdfx

8e2ybdfx2#

这个问题在异常中描述得非常清楚,在使用SimpleTcpClient之前,需要在SimpleTcpClient的新示例上调用.Connect(URL, PORT)
例如:

var client = new SimpleTcpClient().Connect("127.0.0.1", 8910);

官方GitHub有更多信息,请查看他们的readme

相关问题