2017-12-03 1 views
1

루비 소켓에 새로운입니다. 나는 서버에서 프로젝트를하고 난 다음과 같은 서버에 소켓을 토 :루비 소켓 - socker 오버 방법 실행

require 'socket'  

hostname = 'localhost' 
port = 2000 

s = TCPSocket.open(hostname, port) 

while line = s.gets  
    puts line.chop  
    puts "hey" 

    # call helloWorldMethod() 
end 

s.close     

답변

0
require 'socket'    
server = TCPServer.open(2000)  
loop do       
    Thread.start(server.accept) do |client| 
    client.puts(Time.now.ctime) 
    client.puts "Closing the connection. Bye!" 
    client.close     
    end 
end 

I :

require 'socket'    
server = TCPServer.open(2000)  
loop {       
    Thread.start(server.accept) do |client| 
    client.puts(Time.now.ctime) 
    client.puts "Closing the connection. Bye!" 
    client.close     
end 

내가 클라이언트에서 내 프로젝트에 내 방법 중 하나를 실행하고 싶습니다 솔루션이 나는이 문서에 대한 ruby socket programming

을 발견 TCPServer class

에 생각

작은 웹 브라우저

우리는 소켓 라이브러리를 사용하여 인터넷 프로토콜을 구현할 수 있습니다. 여기에, 예를 들어, 당신은 URL을 적용 할 수 있습니다 라우팅에 따라 웹 페이지

의 컨텐츠를 취득 할 수있는 코드가, 그것은 이것은이다 www.yourwebsite.com/users

require 'socket' 

host = 'www.tutorialspoint.com'  # The web server 
port = 80       # Default HTTP port 
path = "/index.htm"     # The file we want 

수 chould HTTP 요청을 우리는 당신이 HTTP post 또는 put 요청

에 요청을 변경할 수 있습니다

파일을 가져 보내

request = "GET #{path} HTTP/1.0\r\n\r\n" 

socket = TCPSocket.open(host,port) # Connect to server 
socket.print(request)    # Send request 
response = socket.read    # Read complete response 
# Split response at first blank line into headers and body 
headers,body = response.split("\r\n\r\n", 2) 
print body       # And display it 

당신은 HTTP 작업을위한 인터넷 : HTTP와 같은 사전 구축 된 라이브러리를 사용할 수 있습니다, 유사한 웹 클라이언트를 구현합니다. 이전 코드에 해당하는 코드는 다음과 같습니다. -