我正在开发一个需要 iPhone 应用程序和服务器之间通信的项目。出于我的目的,我使用 Amazon Web Services 启动了一个 EC2 服务器。我正在尝试启用服务器和 iPhone 之间的 HTTP 通信,以便发出可以通过电话处理的请求。现在我正在服务器端使用 ruby 客户端和 ruby 代码进行测试。我的服务器端代码如下所示:
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
require 'socket'
require 'net/http'
server = TCPServer.open(2000)
loop {
Thread.start(server.accept) do |client|
puts 'Time requested on client side'
lines = []
while line = client.gets and line !~/^\s*$/
lines << line.chomp
for line in lines
puts line
end
end
#socket.print(Time.now.ctime)
client.puts(Time.now.ctime)
client.puts 'Closing connection'
client.close
end
}
该代码可以打印从客户端发送的数据,但是当尝试以其他方式发送数据时,客户端和服务器都会挂断。通信通道是否需要在重新打开之前关闭才能以另一种方式发送数据。我已经在谷歌上搜索了大约一个小时并且没有得到任何地方,即使是从这里的类似问题。另一方面,这是进行请求的最佳方式还是我应该使用其他东西?