# Problem with simple "echo" client/server

**URL:** https://discourse.julialang.org/t/problem-with-simple-echo-client-server/100009
**Category:** New to Julia
**Tags:** networking, sockets
**Created:** [June 7, 2023, 3:04pm UTC](https://discourse.julialang.org/t/problem-with-simple-echo-client-server/100009 "2023-06-07T15:04:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 7, 2023, 3:04pm UTC](https://discourse.julialang.org/t/problem-with-simple-echo-client-server/100009/1 "2023-06-07T15:04:10Z")

</div>

Ok - this is not multithreaded or anything, just a simple “echo” server and client (just baby steps - trying to understand simple before I move on from there). I start the server first, then the client in a different shell. The server gets the message, and seems to send it back, but the client never gets it?  
Server:

```julia
using Sockets

port = 9999
echo_server = listen(port)

while true
    @info "starting server"
    sock = accept(echo_server)
    @info "accepted: $(getsockname(sock))"
    message = read(sock, String)
    @info "read message: '$message'"
    write(sock, message)
    @info "message sent"
    close(sock)
end

```

Client:

```julia
using Sockets

s = connect(9999)
@info "connected"
write(s, "hi there")
@info "message sent"
m = read(s, String)
println(m)
@info "got back: '$m'"
close(s)

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [June 7, 2023, 3:12pm UTC](https://discourse.julialang.org/t/problem-with-simple-echo-client-server/100009/2 "2023-06-07T15:12:20Z")

</div>

The issue that `read(io, String)` tries to read everything into a `String`. Where is the end?

Try this instead.

Server:

```julia
using Sockets

port = 9999
echo_server = listen(port)

while true
    @info "starting server"
    sock = accept(echo_server)
    @info "accepted: $(getsockname(sock))"
    message = readline(sock)
    @info "read message: '$message'"
    println(sock, message)
    @info "message sent"
    close(sock)
end

```

Client:

```julia
using Sockets

s = connect(9999)
@info "connected"
println(s, "hi there")
@info "message sent"
m = readline(s)
println(m)
@info "got back: '$m'"
close(s)

```

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 7, 2023, 3:18pm UTC](https://discourse.julialang.org/t/problem-with-simple-echo-client-server/100009/3 "2023-06-07T15:18:11Z")

</div>

Thanks, I guess I didn’t consider how the server would figure the end of a `String` … perhaps it would have worked with an integer type, but your solution worked and made sense.
