gpt4 book ai didi

VB.NET 套接字发送和接收

转载 作者:行者123 更新时间:2023-12-01 15:34:43 25 4
gpt4 key购买 nike

我正在尝试向服务器发送一条消息,然后返回一条消息,其中包含服务器上玩家总数的数值。不幸的是,除非我删除下面看到的一段代码,否则下面的代码会卡住。

Public Shared Function SocketSendReceive(server As String, port As Integer) As String
'Set up variables and String to write to the server.
Dim ascii As Encoding = Encoding.ASCII
Dim request As String = (DoubleChar(Len(Chr(35))) + Chr(CheckSum(Chr(35)) * 20 Mod 194) + Chr(0) + Chr(35))
Dim bytesSent As [Byte]() = ascii.GetBytes(request)
Dim bytesReceived(255) As [Byte]

' Create a socket connection with the specified server and port.
Dim s As Socket = ConnectSocket(server, port)

If s Is Nothing Then
Return "0"
End If
' Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0)

' Receive the server home page content.
Dim bytes As Int32

' Read the first 256 bytes.
Dim page As [String] = "0"

' The following will block until the page is transmitted.
Do
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes)
Loop While bytes > 0
Return page
End Function

当我删除这部分代码时:

    Do
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes)
Loop While bytes > 0

ping 通过但不返回值。有人可以指出我做错了什么吗?

最佳答案

你的循环导致它崩溃,我遇到了类似的问题。你要做的就是让你的服务器多线程。您在这里所做的是无限次地循环代码,这会使当前线程卡住。如果您添加另一个线程,它将起作用。谷歌(多线程套接字编程)

关于VB.NET 套接字发送和接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832656/

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