gpt4 book ai didi

c# - GUI 在 C# 套接字中卡住

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:24 25 4
gpt4 key购买 nike

我使用套接字编程在 C# 中制作了一个文件接收服务器。我做了一个图形用户界面。有一个名为 'connect' 的按钮,单击它会启动服务器,还有一个文本框会在服务器启动时显示一条消息。但是当我点击按钮时,GUI 卡住了。

这是我的示例代码:

using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

class sampleserver : Form
{

private TextBox newText;
public TcpClient tcpClient;
public TcpListener tcpListener;
public sampleserver()
{
Text = " TCP Server";
Size = new Size(400, 380);
newText = new TextBox();
newText.Parent = this;
newText.Size = new Size(200, 2 * Font.Height);
newText.Location = new Point(10, 55);

Button connect = new Button();
connect.Parent = this;
connect.Text = "Connect";
connect.Location = new Point(295, 20);
connect.Size = new Size(6 * Font.Height, 2 * Font.Height);
connect.Click += new EventHandler(ButtonConnectOnClick);

}


void ButtonConnectOnClick(object obj, EventArgs ea)
{
tcpListener = new TcpListener(IPAddress.Any, 1234);
tcpListener.Start();
newText.Text = "Server started"; //**This line is not working**

//Infinite loop to connect to new clients
while (true)
{
// Accept a TcpClient
TcpClient tcpClient = tcpListener.AcceptTcpClient();
string address = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();

byte[] data = new byte[1024];
NetworkStream ns = tcpClient.GetStream();
int recv = ns.Read(data, 0, data.Length);
StreamReader reader = new StreamReader(tcpClient.GetStream());

// The first message from the client is the file size
string cmdFileSize = reader.ReadLine();


int length = Convert.ToInt32(cmdFileSize);
byte[] buffer = new byte[length];
int received = 0;
int read = 0;
int size = 1024;
int remaining = 0;

// Read bytes from the client using the length sent from the client
while (received < length)
{
remaining = length - received;
if (remaining < size)
{
size = remaining;
}

read = tcpClient.GetStream().Read(buffer, received, size);
received += read;
}

}

}
public static void Main()
{
Application.Run(new sampleserver());
}
}

我需要进行哪些更改才能正常运行?

最佳答案

我建议您使用异步套接字,但您也可以只使用按钮点击方法异步,就像这样。

async void ButtonConnectOnClick(object obj, EventArgs ea)
{
tcpListener = new TcpListener(IPAddress.Any, 1234);
tcpListener.Start();
newText.Text = "Server started"; //**This line is not working**
await Task.Run(() =>
{
//Infinite loop to connect to new clients
while (true)
{
// Accept a TcpClient
TcpClient tcpClient = tcpListener.AcceptTcpClient();
string address = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();

byte[] data = new byte[1024];
NetworkStream ns = tcpClient.GetStream();
int recv = ns.Read(data, 0, data.Length);
StreamReader reader = new StreamReader(tcpClient.GetStream());

// The first message from the client is the file size
string cmdFileSize = reader.ReadLine();


int length = Convert.ToInt32(cmdFileSize);
byte[] buffer = new byte[length];
int received = 0;
int read = 0;
int size = 1024;
int remaining = 0;

// Read bytes from the client using the length sent from the client
while (received < length)
{
remaining = length - received;
if (remaining < size)
{
size = remaining;
}

read = tcpClient.GetStream().Read(buffer, received, size);
received += read;
}

}

});
}

这将使整个方法异步,现在您可以从套接字读取数据而无需卡住主 UI。

祝你好运。

关于c# - GUI 在 C# 套接字中卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com