When I transfer datas between a server and a client using julia1.10.5, Sockets.jl works well, but when I running the same code on julia1.11.5, Sockets.jl gives abnormal results. Here is my code:
using Sockets
const START = "<s>"
const FINAL = "<e>"
const ENCODING = UInt8
const lstart = length(START)
const lfinal = length(FINAL)
ipv4 = IPv4(127, 0, 0, 10)
port = 8000
"""
putdata(sock::TCPSocket, data::Vector{Float32})
data transfer protocol:
<s> * length_of_data * data * <e>
"""
function putdata(sock::TCPSocket, data::Vector{Float32})
try
HEAD = transcode(ENCODING, START)
TAIL = transcode(ENCODING, FINAL)
SIZE = UInt32(sizeof(data))
write(sock, HEAD) # sending head of protocol
write(sock, SIZE) # number of bytes to send
write(sock, data) # sending data
write(sock, TAIL) # sending tail of protocol
return true
catch err
@error "sending errors" err
return false
end
end
function getdata(sock::TCPSocket)
buffer = Vector{UInt8}()
is_start_found = false
# looking for the head of protocol
while !is_start_found
push!(buffer, read(sock, UInt8))
lbuf = length(buffer)
if lbuf ≥ lstart
mark = String(buffer[lbuf+1-lstart : lbuf])
if isequal(mark, START)
is_start_found = true
empty!(buffer)
end
end
end
# read the number of bytes of data
databytes = read(sock, UInt32)
if !iszero(databytes % 4)
@warn "not legal length"
return Float32[]
end
# read data
data = read(sock, databytes)
# check the tail mark
mark = String(read(sock, lfinal))
if !isequal(mark, FINAL)
@warn "end mark mismatch (data may be corrupted)"
return Float32[]
end
return reinterpret(Float32, data)
end
then is the server end:
function run_server(ip::IPv4, port::Int)
server = listen(ip, port)
try
while true
sock = accept(server)
@async begin
while isopen(sock)
# recive and respond
data = getdata(sock)
write(sock, sum(data))
# sending data to client
len = rand(30000:80000)
data = randn(Float32, len)
issent = putdata(sock, data)
# check the sum
if issent
localsum = sum(data)
cloudsum = read(sock, Float32)
delta = localsum - cloudsum
!iszero(delta) && println("error: ", delta)
# println("$localsum $cloudsum")
end
end
end
end
catch err
println("server shut down unexpectedly")
finally
close(server)
end
end
then is the client end
function run_client(ip::IPv4, port::Int, niters::Int=100)
sock = connect(ip, port)
try
for iter ∈ 1 : niters
# send data to server
len = rand(30000:80000)
data = randn(Float32, len)
issent = putdata(sock, data)
# check the sum
if issent
localsum = sum(data)
cloudsum = read(sock, Float32)
delta = localsum - cloudsum
!iszero(delta) && println("error: ", delta)
# println("$localsum $cloudsum")
end
# recive and respond too
data = getdata(sock)
write(sock, sum(data))
end
catch err
println("disconnected")
finally
close(sock)
end
end
finally I run the run_server(ipv4, port)
in one terminal, and run run_client(ipv4, port, 100)
in another terminal, and things happend that julia1.10.5 doesn’t execute println("error ", delta)
while julia1.11.5 execute println("error ", delta)
with delta
very close to zero like
error: -0.0001373291
error: -7.6293945e-5
error: -0.00014686584
...
so is there any difference between 1.10.5 and 1.11.5 ?