M
Marc Soda
Hey all,
While testing some edge cases in an app I came across a strange socket
issue. If I have a server that dies unexpectedly, and then try to
write to it from an already connected client, it doesn't raise an
exception until the next time I try to write it.
Example:
Server
---------
require 'socket'
include Socket::Constants
sock = Socket.new(AF_INET, SOCK_STREAM, 0)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, true)
sockaddr = Socket.pack_sockaddr_in(7000, 'localhost')
sock.bind(sockaddr)
sock.listen(0)
client, client_sockaddr = sock.accept
print "Connected\n"
print ">>> #{client.readline}"
abort
Client
--------
require 'socket'
include Socket::Constants
$sock = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(7000, 'localhost')
$sock.connect(sockaddr)
def send_msg
begin
$sock.write("Hello\n")
rescue StandardError => err
p err
end
end
puts "1"
send_msg
sleep 0.5
puts "2"
send_msg # Should error here.
sleep 0.5
puts "3"
send_msg
Any suggestions? I'm running on Linux, BTW.
Thanks,
Marc
While testing some edge cases in an app I came across a strange socket
issue. If I have a server that dies unexpectedly, and then try to
write to it from an already connected client, it doesn't raise an
exception until the next time I try to write it.
Example:
Server
---------
require 'socket'
include Socket::Constants
sock = Socket.new(AF_INET, SOCK_STREAM, 0)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, true)
sockaddr = Socket.pack_sockaddr_in(7000, 'localhost')
sock.bind(sockaddr)
sock.listen(0)
client, client_sockaddr = sock.accept
print "Connected\n"
print ">>> #{client.readline}"
abort
Client
--------
require 'socket'
include Socket::Constants
$sock = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(7000, 'localhost')
$sock.connect(sockaddr)
def send_msg
begin
$sock.write("Hello\n")
rescue StandardError => err
p err
end
end
puts "1"
send_msg
sleep 0.5
puts "2"
send_msg # Should error here.
sleep 0.5
puts "3"
send_msg
Any suggestions? I'm running on Linux, BTW.
Thanks,
Marc