Comments (1)
according to the documentation for io.Reader
If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
This library does not follow that convention. It appears to block until it is able to fill the buffer.
This program demonstrates the issue
package main
import (
"fmt"
"io"
"log"
"net"
"time"
"github.com/reiver/go-telnet"
)
// start a TCP server that
// - prints "hello world"
// - waits 60 seconds
// - then hangs up
func DemoServer() {
listener, err := net.Listen("tcp", ":1111")
if err != nil {
panic(err)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
conn.Write([]byte("hello world\n"))
time.Sleep(10 * time.Second)
conn.Close()
}
}
// ConnectWithBufferSize connects to the server at the given address and
// and prints the output of each call to Read() using the given buffer size.
func ConnectWithBufferSize(bufferSize int) {
telnetClient, err := telnet.DialTo("localhost:1111")
if err != nil {
panic(err)
}
defer telnetClient.Close()
buff := make([]byte, bufferSize)
for {
n, err := telnetClient.Read(buff)
log.Printf("EOF:%t> %#v\n", err == io.EOF, string(buff[:n]))
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
}
}
func main() {
go DemoServer()
fmt.Println("=== Buffer Size 7 ===")
ConnectWithBufferSize(7)
fmt.Println("=== Buffer Size 1 ===")
ConnectWithBufferSize(1)
}
Output:
=== Buffer Size 7 ===
2022/03/30 15:45:06 EOF:false> "hello w"
2022/03/30 15:45:16 EOF:true> "orld\n"
=== Buffer Size 1 ===
2022/03/30 15:45:16 EOF:false> "h"
2022/03/30 15:45:16 EOF:false> "e"
2022/03/30 15:45:16 EOF:false> "l"
2022/03/30 15:45:16 EOF:false> "l"
2022/03/30 15:45:16 EOF:false> "o"
2022/03/30 15:45:16 EOF:false> " "
2022/03/30 15:45:16 EOF:false> "w"
2022/03/30 15:45:16 EOF:false> "o"
2022/03/30 15:45:16 EOF:false> "r"
2022/03/30 15:45:16 EOF:false> "l"
2022/03/30 15:45:16 EOF:false> "d"
2022/03/30 15:45:16 EOF:false> "\n"
2022/03/30 15:45:26 EOF:true> ""
Note that with a buffer size of 7 you immediately get hello w
then you have to wait the full 10 seconds to get orld\n
. A quick test with a debugger shows that it is hanging here
As a work around, you can use a buffer size of 1
I am not able to re-produce the behavior OP describes where the connection ever becomes interactive.
I have confirmed that this is fixed by either of PR #22 or PR #9.
from go-telnet.
Related Issues (16)
- How to Read data unblock? HOT 1
- how to set timeout for client? HOT 1
- Server response log
- Log IPs that connect to the telnet server
- how to get client terminal size?
- can support Cluster address?
- Is there a ReadString ('\ n')? HOT 2
- TELNET Shell Server Example build error HOT 6
- Not only connecting Telnet Server port, connecting other ports
- Redundant c.Close()
- telsh command with arguments
- sample script HOT 2
- Server - store list of established connections. HOT 6
- How can the server detect conn is closed? HOT 1
- A bit of help
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-telnet.