Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Establishing new TCP Socket connection (http://www.velocityreviews.com/forums/t822786-establishing-new-tcp-socket-connection.html)

Joe Van Dyk 07-05-2005 09:03 PM

Establishing new TCP Socket connection
 
Hi,

If I want to establish a new TCP connection every time I need to
access data (the server doesn't want to keep old connetions around),
is there any reason why I can't do something like:

def send_message_and_recv_response
server =3D TCPSocket.new @server_ip, @server_port
server.puts "the message"
result =3D server.recv
end



Joe Van Dyk 07-05-2005 11:39 PM

Re: Establishing new TCP Socket connection
 
No response needed on this... it all seems to work ok.

On 7/5/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> Hi,
>=20
> If I want to establish a new TCP connection every time I need to
> access data (the server doesn't want to keep old connetions around),
> is there any reason why I can't do something like:
>=20
> def send_message_and_recv_response
> server =3D TCPSocket.new @server_ip, @server_port
> server.puts "the message"
> result =3D server.recv
> end
>




Eric Hodel 07-05-2005 11:49 PM

Re: Establishing new TCP Socket connection
 
On 05 Jul 2005, at 14:03, Joe Van Dyk wrote:

> If I want to establish a new TCP connection every time I need to
> access data (the server doesn't want to keep old connetions around),
> is there any reason why I can't do something like:
>
> def send_message_and_recv_response
> server = TCPSocket.new @server_ip, @server_port
> server.puts "the message"
> result = server.recv
> end


You could be more friendly to the other end by adding an explicit close:

def do_the_stuff
sock = TCPSocket.new @ip, @port
sock.puts "message"
return sock.recv
ensure
sock.close unless sock.nil?
end

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04




Adam P. Jenkins 07-06-2005 12:05 AM

Re: Establishing new TCP Socket connection
 
Joe Van Dyk wrote:
> Hi,
>
> If I want to establish a new TCP connection every time I need to
> access data (the server doesn't want to keep old connetions around),
> is there any reason why I can't do something like:
>
> def send_message_and_recv_response
> server = TCPSocket.new @server_ip, @server_port
> server.puts "the message"
> result = server.recv
> end


Sure, except you should explicitly close the connection as well at the
end of your block rather than letting the socket object's finalizer do
it whenever the GC gets around to calling it. So, something like:

def send_message_and_recv_response
server = nil
server = TCPSocket.new @server_ip, @server_port
server.puts "the message"
result = server.recv
ensure
server.close if server
end



All times are GMT. The time now is 08:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57