因此,我试图制作一个可以执行从客户端发送的功能的应用程序,它工作正常,但 UI 在侦听来自客户端的消息时冻结,我必须更改什么才能使此代码异步运行?已经尝试将 public void ExecuteServer(string pwd) 更改为 public async task ExecuteServer(string pwd) 但它只是告诉我我缺少等待
//Where im calling it
public Form2()
{
InitializeComponent();
(ExecuteServer("test"));
}
//The Network Socket im trying to run Async
public static void ExecuteServer(string pwd)
{
// Establish the local endpoint
// for the socket. Dns.GetHostName
// returns the name of the host
// running the application.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
// Creation TCP/IP Socket using
// Socket Class Costructor
Socket listener = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
// Using Bind() method we associate a
// network address to the Server Socket
// All client that will connect to this
// Server Socket must know this network
// Address
listener.Bind(localEndPoint);
// Using Listen() method we create
// the Client list that will want
// to connect to Server
listener.Listen(10);
while (true)
{
//Console.WriteLine("Waiting connection ... ");
// Suspend while waiting for
// incoming connection Using
// Accept() method the server
// will accept connection of client
Socket clientSocket = listener.Accept();
// Data buffer
byte[] bytes = new Byte[1024];
string data = null;
while (true)
{
int numByte = clientSocket.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,
0, numByte);
if (data.IndexOf("<EOF>") > -1)
break;
}
斯蒂芬大帝
BIG阳
慕姐8265434
随时随地看视频慕课网APP
相关分类