英文:
Is there a function to detect server disconnection on the client side when a TCP connection is made in golang?
问题
在Go语言中,可以使用net.Conn
类型的SetReadDeadline
方法来检测客户端在TCP连接中的断开。当客户端断开连接时,读取操作将超时并返回错误。以下是在Go语言中检测服务器断开连接的示例代码:
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"time"
)
func main() {
log.SetFlags(0)
for {
conn, err := net.Dial("tcp", ":8000")
if nil != err {
fmt.Println("Connecting to server...")
time.Sleep(time.Duration(5) * time.Second)
continue
}
for {
fmt.Println("-to Server")
fmt.Print("-> ")
in := bufio.NewReader(os.Stdin)
s, err := in.ReadString('\n')
if err != nil {
log.Println(err)
}
str := []byte(s)
// If there is no input value, it is not passed to the server.
if len(str) > 2 {
_, err := conn.Write(str)
if err != nil {
log.Println("Connection with the server has been lost.")
break
}
}
}
}
}
在上述代码中,当conn.Write
方法返回错误时,表示与服务器的连接已断开。你可以在此处添加适当的处理逻辑来处理连接断开的情况。
希望对你有所帮助!如果还有其他问题,请随时提问。
英文:
Is there a function to detect server disconnection on the client side when a TCP connection is made in golang?
The server is made in Java, and the client is made in go language.
in java
public class Server {
public final static int PORT = 8000;
public static void main(String[] args) {
try (ServerSocket server = new ServerSocket(PORT)) {
while (true) {
System.out.println("wait for client's request.");
try (Socket client = server.accept()) {
System.out.println("Connected client information : " + "[" + client.getInetAddress().getHostName() + "]");
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
while (true) {
String msg = br.readLine();
System.out.println("Client > Server : " + msg);
} // end of while(in)
}catch(IOException e) {
System.out.println("Connection with the client has been lost.");
}
} // end of while(out)
} catch (IOException e) {
System.out.println("There was a problem establishing the connection.");
}
}
}
in GoLang
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"time"
)
func main() {
log.SetFlags(0)
for {
conn, err := net.Dial("tcp", ":8000")
if nil != err {
fmt.Println("Connecting to server...")
time.Sleep(time.Duration(5) * time.Second)
continue
}
for {
fmt.Println("-to Server")
fmt.Print("-> ")
in := bufio.NewReader(os.Stdin)
s, err := in.ReadString('\n')
if err != nil {
log.Println(err)
}
str := []byte(s)
// If there is no input value, it is not passed to the server.
if len(str) > 2 {
conn.Write(str)
}
}
}
}
or is there a function like JAVA's isConnected in go language?
答案1
得分: 0
// Write方法向连接中写入数据。
// Write方法可以在固定的时间限制内超时并返回错误;请参阅SetDeadline和SetWriteDeadline。
Write(b []byte) (n int, err error)
当查看源代码时,可以清楚地看到在写入连接时忽略了错误。如果发生错误,连接很可能不再有效。当然,你可以编写一个函数来验证连接是否有效,如下所示:
// 注意,我假设连接不会忽略空切片。
// 这可能是不正确的,因此简单的错误处理仍然有效。
func isConnected(conn net.Conn) bool {
_, err := conn.Write(nil)
return err != nil
}
英文:
// Write writes data to the connection.
// Write can be made to time out and return an error after a fixed
// time limit; see SetDeadline and SetWriteDeadline.
Write(b []byte) (n int, err error)
When looking onto source code you can clearly see that you are ignoring error when writing to connection. If error happens the connection is most likely no longer valid. You can of course make a function to verify whether connection is valid like this:
// note that I assume connection will not ignore nil slice.
// this might be untrue and thus simple error handling remains.
func isConnected(conn net.Conn) bool {
_, err := conn.Write(nil)
return err != nil
}
答案2
得分: 0
对于我的用例,我执行以下操作:
connbuf := bufio.NewReader(*r.Conn)
for {
p := make([]byte, 2048)
rlen, err := connbuf.Read(p)
if err != nil {
if err == io.EOF {
fmt.Println("EOF")
r.ConnStatus = false
return
} else {
fmt.Println("Error reading from the TCP Replay Channel", err)
}
}
fmt.Println("Read len :", rlen)
}
基本上,这段代码会查找io.EOF,如果找到,就会认为连接要么丢失,要么从另一端关闭。
英文:
For my use case, I do the following:
connbuf := bufio.NewReader(*r.Conn)
for {
p := make([]byte, 2048)
rlen, err := connbuf.Read(p)
if err != nil {
if err == io.EOF {
fmt.Println("EOF")
r.ConnStatus = false
return
} else {
fmt.Println("Error reading from the TCP Replay Channel", err)
}
}
fmt.Println("Read len :", rlen)
}
Basically this will look for io.EOF and if found it will conclude the connection is either lost or closed from the other side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论