Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ruby的聊天应用程序_Ruby_Sockets_Chat - Fatal编程技术网

使用ruby的聊天应用程序

使用ruby的聊天应用程序,ruby,sockets,chat,Ruby,Sockets,Chat,我正在尝试使用ruby on linux构建一个聊天应用程序 ,所以我的客户端不能发送很多消息,它只是在第一次发送一条消息 服务器代码: #!/usr/bin/ruby require 'socket' server = TCPServer.new(2008) while (session = server.accept) && (input = session.gets) puts input end 客户端代码: #!/usr/bin/ruby requir

我正在尝试使用ruby on linux构建一个聊天应用程序 ,所以我的客户端不能发送很多消息,它只是在第一次发送一条消息

服务器代码:

#!/usr/bin/ruby
require 'socket'

server = TCPServer.new(2008)

while (session = server.accept) && (input = session.gets)
    puts input

end 
客户端代码:

#!/usr/bin/ruby
require 'socket'


begin
 clientSession = TCPSocket.new( "localhost", 2008 ) 

rescue StandardError => bang
  puts "Error !! "
else


while !(clientSession.closed?) 

print "Enter message :  "
msg = gets
clientSession.puts msg 
end

end
非常感谢您抽出时间

您的问题在于:

while (session = server.accept) && (input = session.gets)
  puts input
end 
您正在接受while循环中的连接。相反,您需要的是首先接受连接,然后通过该连接进行循环:

connection = server.accept

while (input = connection.gets)
  puts input
end
你的问题是:

while (session = server.accept) && (input = session.gets)
  puts input
end 
您正在接受while循环中的连接。相反,您需要的是首先接受连接,然后通过该连接进行循环:

connection = server.accept

while (input = connection.gets)
  puts input
end

您面临的错误是什么?我没有错误问题是当我想发送示例4消息时,客户端只发送一条消息:(您面临的错误是什么?我没有错误问题是当我想发送示例4消息时,客户端只发送一条消息:(如果您是活动用户,并且它帮助了您。您必须将其标记为已接受。
Ahmed
。谢谢如果您是活动用户,并且它帮助了您。您必须将其标记为已接受。
Ahmed
。谢谢