C#,TCP客户端无法按预期工作(太慢?)
在网络方面,我是初学者。即便如此,我还是只作为一种爱好编程。这是我无法真正解释的问题。我在游戏上附上了一个PacketLogger,这启动了TCP服务器。然后,我在C#中构建了一个TCP客户端(这是VisualStudio 2022中的控制台应用程序),并具有以下代码:
public static void Main(string[] args)
{
const int PORT_NO = 51615;
const string SERVER_IP = "127.0.0.1";
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
const int packetLogger_Recv = 0;
const int packetLogger_Send = 1;
NetworkStream nwStream = client.GetStream();
while (true)
{
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string receivedPacket = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Console.WriteLine("Receive: " + receivedPacket);
}
}
但是,我认为它必须将“接收:”放置在服务器上的每一行。 ,但这只是非常不规则的,不应该是正确的。也许有人可以向我解释这个问题,并给我解决方案。如果有人感兴趣, 是游戏Nostale
还有一个例子,但不幸的是,只有在自动上,我不幸的是0个想法,但是它立即起作用,而且超级快。 [自动示例] [1] [1]:https://gitlab.com/imblackdude/nostale-packetlogger/-/blob/master/packetlogger_api.au3
输出看起来像这样:
0 cond 1 27511 0 0 131 3
0 mv 3 2188 52 162 8312 11 200 0 0 1 98 0 4 0
0 cond 1 27511 0 0 137387
0 cond 1 27511 0 0 131 3
0 mv 3 2111 17 17 863248 131 24
0 mv 2 2006683 78 151 1010 8
0 cond 1 27511 0 0 13 0 0
0 cond 1 27511 0 0 130
0 cond 1 27511 0 0 130 3005
0 cond 1 27511 0 0 137317
0 cond 1 27511 0 0 13 42 125 24
0 mv 1 27491 75 111 12 3005
0 mv 1 27567 76 148 127 75 148 10
0 su 2 2196 3 2033 0 12 11 514 0 0 1 27 19 0 0
0 cond 1 27511 0 0 1311 0 0 13
Receive: 0 mv 1 27491 70 112 12
0 cond 1 27511 0 0 137525
0 mv 1 27426 37 120 241 116 1
0 cond 1 27511 0 0 133 3
0 cond 1 27511 0 0 137101
Receive: 0 mv 1 27426 35 118 24
0 mv 3 2188 51 162 8312 11 514 0 0 1 3 0 4 0
0 cond 1 27511 0 0 132525
0 mv 3 2181 41 165 850
0 cond 1 27511 0 0 131 3
Receive: 0 dm 3 2053 0
0 cond 1 27511 0 0 1311 0 0 13
Receive: 0 mv 1 27491 63 110 12
0 mv 3 2188 50 162 8312 11 200 0 0 1 98 0 4 0
0 cond 1 27511 0 0 13116 80 10
0 cond 1 27511 0 0 137378
Receive: 0 mv 1 27491 60 108 12
0 cond 1 27511 0 0 137 3
0 cond 1 27511 0 0 137400
0 mv 3 2187 48 162 8381 115 1
0 cond 1 27511 0 0 130 3
0 mv 3 2186 47 163 85115 78 10
0 cond 1 27511 0 0 13 0 0 0 10 0
Receive: 0 mv 1 27491 57 106 12
Receive: 0 mv 1 27426 32 110 24
0 cond 1 27511 0 0 137387
0 eff 1 26955 3951 134 3
0 mv 3 2183 39 156 8312 11 514 0 0 1 15 19 0 0
0 mv 3 2179 28 163 830
0 cond 1 27511 0 0 13 55 104 12
0 mv 3 2107 15 3 8 6512 11 514 0 0 0 0 19 0 0477.4618.4465.-1.4714.4536.4534.4588.4538 24 100 0 2195 4 2 0 51 0 20 108 108 130 CARTEL 28 0 20 0 18 99 4 1 0|0|0 1 10 80 0
0 mv 1 27426 26 102 243 12 8
0 cond 1 27511 0 0 13 0 0
0 cond 1 27511 0 0 1379 114 1
I am a beginner when it comes to networking. Even so, I have only programmed as a hobby. It's about a problem that I can't really explain. I have a packetlogger attached to a game this starts a tcp server. Then I built a TCP client in C# (It is a console application in VisualStudio 2022) with the following code:
public static void Main(string[] args)
{
const int PORT_NO = 51615;
const string SERVER_IP = "127.0.0.1";
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
const int packetLogger_Recv = 0;
const int packetLogger_Send = 1;
NetworkStream nwStream = client.GetStream();
while (true)
{
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string receivedPacket = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Console.WriteLine("Receive: " + receivedPacket);
}
}
Now I think, however, that it would have to put a "Receive:" in front of every line it gets from the server, but that is only very irregular and should not be correct. Maybe someone can explain me the problem and give me a solution. If anyone is interested, it
is the game NosTale
There is also an example but unfortunately only in AutoIT, of which I have unfortunately 0 idea, but with it it works immediately and also super fast. [AutoIT Example][1]
[1]: https://gitlab.com/ImBlackDude/nostale-packetlogger/-/blob/master/PacketLogger_API.au3
The output looks like this:
0 cond 1 27511 0 0 131 3
0 mv 3 2188 52 162 8312 11 200 0 0 1 98 0 4 0
0 cond 1 27511 0 0 137387
0 cond 1 27511 0 0 131 3
0 mv 3 2111 17 17 863248 131 24
0 mv 2 2006683 78 151 1010 8
0 cond 1 27511 0 0 13 0 0
0 cond 1 27511 0 0 130
0 cond 1 27511 0 0 130 3005
0 cond 1 27511 0 0 137317
0 cond 1 27511 0 0 13 42 125 24
0 mv 1 27491 75 111 12 3005
0 mv 1 27567 76 148 127 75 148 10
0 su 2 2196 3 2033 0 12 11 514 0 0 1 27 19 0 0
0 cond 1 27511 0 0 1311 0 0 13
Receive: 0 mv 1 27491 70 112 12
0 cond 1 27511 0 0 137525
0 mv 1 27426 37 120 241 116 1
0 cond 1 27511 0 0 133 3
0 cond 1 27511 0 0 137101
Receive: 0 mv 1 27426 35 118 24
0 mv 3 2188 51 162 8312 11 514 0 0 1 3 0 4 0
0 cond 1 27511 0 0 132525
0 mv 3 2181 41 165 850
0 cond 1 27511 0 0 131 3
Receive: 0 dm 3 2053 0
0 cond 1 27511 0 0 1311 0 0 13
Receive: 0 mv 1 27491 63 110 12
0 mv 3 2188 50 162 8312 11 200 0 0 1 98 0 4 0
0 cond 1 27511 0 0 13116 80 10
0 cond 1 27511 0 0 137378
Receive: 0 mv 1 27491 60 108 12
0 cond 1 27511 0 0 137 3
0 cond 1 27511 0 0 137400
0 mv 3 2187 48 162 8381 115 1
0 cond 1 27511 0 0 130 3
0 mv 3 2186 47 163 85115 78 10
0 cond 1 27511 0 0 13 0 0 0 10 0
Receive: 0 mv 1 27491 57 106 12
Receive: 0 mv 1 27426 32 110 24
0 cond 1 27511 0 0 137387
0 eff 1 26955 3951 134 3
0 mv 3 2183 39 156 8312 11 514 0 0 1 15 19 0 0
0 mv 3 2179 28 163 830
0 cond 1 27511 0 0 13 55 104 12
0 mv 3 2107 15 3 8 6512 11 514 0 0 0 0 19 0 0477.4618.4465.-1.4714.4536.4534.4588.4538 24 100 0 2195 4 2 0 51 0 20 108 108 130 CARTEL 28 0 20 0 18 99 4 1 0|0|0 1 10 80 0
0 mv 1 27426 26 102 243 12 8
0 cond 1 27511 0 0 13 0 0
0 cond 1 27511 0 0 1379 114 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,我需要在Cr 0x0d(“ \ r”)上拆分字符串。
In this case, I needed to split the string at CR 0x0d ("\r").