gpt4 book ai didi

c# - 为什么 Ruby 套接字服务器与其他 Ruby 套接字客户端一起工作,而不是 C# 套接字客户端?

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

所以我有两个 Ruby 程序,它们是客户端和服务器套接字程序,它们一起工作以交换消息。但是 C# 客户端不起作用。我给 MCVE,首先是 ruby​​ 客户端。

#socketClient.rb
#With thanks to https://code.likeagirl.io/socket-programming-in-ruby-f714131336fd
require "socket"

while sum = $stdin.gets.chomp # Read lines from the socket
socket = TCPSocket.open("localhost", 3000)

#puts "Starting the Client..................."

socket.puts sum
while message = socket.gets # Read lines from the socket
puts message.chomp
end
socket.close # Close the socket
end

#puts "Closing the Client..................."

和服务器

#simplestSocketServer.rb
#With thanks to https://code.likeagirl.io/socket-programming-in-ruby-f714131336fd

require "socket"

port = 3000
ipAddress = "127.0.0.1"
server = TCPServer.open(ipAddress, port) # Server would listen on port 3000
loop { # Servers run forever
puts "Starting the Server, accepting connections on port " + port.to_s + "..................."
client_connection = server.accept # Establish client connect connection
begin
clientText = client_connection.gets.chomp
puts clientText

resp = "Acknowledged"
client_connection.puts("#{clientText}" + "#{resp}") # Send the answer to the client
client_connection.puts("Closing the connection with #{client_connection}")
rescue Exception => getException
puts "#{getException}"
end
client_connection.close # Disconnect from the client
}

和C#控制台程序

using System;
using System.Net.Sockets;
using System.Text;


namespace SimplestCSharpRubySocketsClient
{
class Program
{
static void Main(string[] args)
{
try
{
string ipAddress = "127.0.0.1";
Int16 portNumber = 3000;

TcpClient _client; _client = new TcpClient();
_client.Connect(ipAddress, portNumber);

System.Console.WriteLine("we have connected, seemingly ...");

NetworkStream stream;

stream = _client.GetStream();

Byte[] sendBytes = Encoding.UTF8.GetBytes("some text");

System.Console.WriteLine("writing and flushing some bytes ...");
stream.Write(sendBytes, 0, sendBytes.Length);
stream.Flush();

Byte[] recvBytes = new byte[_client.ReceiveBufferSize];
System.Console.WriteLine("_client.ReceiveBufferSize = " + _client.ReceiveBufferSize); // <--- this prints 65536

System.Console.WriteLine("waiting to read bytes ...");
stream.Read(recvBytes, 0, recvBytes.Length); //<--- hangs here

System.Console.WriteLine("comething came back ...");
string result = Encoding.UTF8.GetString(recvBytes);
string result2 = result.Substring(0, result.LastIndexOf("\r\n"));

_client.Close();
_client.Dispose();
_client = null;
}
catch (Exception ex)
{
//TODO figure out a better error handler
throw ex;
}
}
}
}

C# 程序连接并写入字节,但在查找读取字节时它只是挂起。

请注意,我正在 Visual Studio 中以管理员权限运行 C# 控制台程序。这两个 ruby​​ 程序在各自独立的 Windows 控制台窗口中运行。

考虑到一些反馈,我在 ruby​​ 服务器中添加了另一行来输出 clientText。它什么也不打印,表明服务器没有完全接收到字节。是否有要求C#发送的终止信号?

提前致谢。

最佳答案

这里的问题是 C# 客户端不会像 Ruby 版本那样在字符串末尾发送换行符(socket.puts 发送一个在末尾带有换行符的字符串)。

如果您更改 sendBytes 数组以在有效负载中包含 \n,如下所示:

Byte[] sendBytes = Encoding.UTF8.GetBytes("some text\n");

您会看到它在控制台上打印comething came back ...

由于在 Ruby 服务器中有以下 gets,因此需要换行符:

clientText = client_connection.gets.chomp

关于c# - 为什么 Ruby 套接字服务器与其他 Ruby 套接字客户端一起工作,而不是 C# 套接字客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54281832/

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