English 中文(简体)
服务器如何知道客户不再生活?
原标题:How does server know that the client is not longer live?

我试图理解结核病的运作方式,而我迄今知道的是,如果“兰多”脱节,一方能够知道另一方是否仍然在那里。 因此,使用一些PING/PONG算法或TCP固定值。 在客户与服务器连接、10秒后发出简单信息的情况下,我创建了一个简单的客户服务器应用程序。 令我困惑的是,服务器如何知道客户在以下两种情况下已不复存在:

  1. I let the program to completely run and finish - in this case the server closes the connection (I don t understand how can server know that the client is no longer there, I didn t indicate anywhere on the client side that I am closing the connection)
  2. I shut down the client process before it complete (before it send a message to the server) - in this case the server prints the following message Error reading: read tcp 127.0.0.1:8080->127.0.0.1:60845: wsarecv: An existing connection was forcibly closed by the remote host. (Again, I don t understand how it can know that the client is no longer there)

我的假设是,本组织(或golang net> Library)在此参与,并在清洁发展机制连接真正关闭或象处理这种情况那样发出额外信息。

任何帮助都是受欢迎的。 下面是我使用的完整代码,因此,你可以在当地或在线游乐场管理。

客户。

package main

import (
    "fmt"
    "net"
    "time"
)

func main() {
    // Connect to the server
    conn, err := net.Dial("tcp", "localhost:8080")
    if err != nil {
        fmt.Println("Error connecting:", err)
        return
    }
    defer conn.Close()

    // Send a message to the server
    message := "Hello from client!"
    time.Sleep(10000 * time.Millisecond)
    conn.Write([]byte(message))

    // Read the response from the server
    buffer := make([]byte, 1024)
    n, err := conn.Read(buffer)
    if err != nil {
        fmt.Println("Error reading:", err)
        return
    }

    // Print the server s response
    fmt.Printf("Received response from server: %s
", buffer[:n])
}

服务器。

package main

import (
    "fmt"
    "net"
)

func handleConnection(conn net.Conn) {
    defer conn.Close()

    // Obtain and print the client s IP address when the connection is established
    clientAddr := conn.RemoteAddr()
    fmt.Printf("Client connected from IP address: %s
", clientAddr)

    for {
        // Read data from the client
        buffer := make([]byte, 1024)
        n, err := conn.Read(buffer)
        if err != nil {
            fmt.Println("Error reading:", err)
            return
        }

        // Print received message
        fmt.Printf("Received message from client (%s): %s
", clientAddr, buffer[:n])

        // Send a response back to the client
        response := "Hello from server!"
        conn.Write([]byte(response))
    }
}

func main() {
    // Start listening on a port
    listener, err := net.Listen("tcp", "127.0.0.1:8080")
    if err != nil {
        fmt.Println("Error listening:", err)
        return
    }
    defer listener.Close()

    fmt.Println("Server listening on port 8080")

    for {
        // Wait for a connection
        conn, err := listener.Accept()
        if err != nil {
            fmt.Println("Error accepting connection:", err)
            continue
        }

        // Handle the connection in a goroutine
        go handleConnection(conn)
    }
}

问题回答

我认识到,我谈论的不是我所熟知的东西,因此我自己测试了。 例如:

监督:sudo tcpdump -tt -i lo Port 8887 (也许需要找到你的地方东道装置,可能的话是lo0,使用sudo tcpdump - D。 监视旗帜领域:. is ACK, the others should be un-evident.

服务器:nc -l 8887

用户:nc localhost 8887

  • Client: SYN
  • Server: SYN ACK
  • Client: ACK

B. 客户和新闻界的类型

  • Client: PSH ACK
  • Server: ACK

服务器和报到门类

  • Server: PSH ACK
  • Client: ACK

a) 停止客户,无论是通过 c或高技能:

  • Client: FIN ACK
  • Server: FIN ACK
  • Client: ACK

b) 停止服务器:

  • Server: FIN ACK
  • Client: ACK

于是停止客户:

  • Client: FIN ACK
  • Server: RST

这项实验是在WSL/Ubuntu进行的。 我没有汇编你的节目,但你可以测试你的情景。

《议定书》的执行取决于这一点;这完全与Go有关,但<条码>conn.Close(中的情况除外。





相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

Listen to a port that is in use [duplicate]

Possible Duplicate: Get connecting IP from specified ports that using by other program. If a port is used by a program, is there any way I can listen that port and get the connected IP on that ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

热门标签