Junior — Middle
Can you give examples of situations where the TCP protocol is used?
sobes.tech AI
Answer from AI
The TCP (Transmission Control Protocol) is used in situations where reliable, ordered, and guaranteed delivery of data between two network nodes is important. It provides connection establishment, error control, and retransmission of lost packets.
Typical uses of TCP:
- Web servers and browsers (HTTP/HTTPS) for loading pages.
- Email (SMTP, IMAP).
- File transfer (FTP).
- Applications requiring reliable data exchange, such as databases, messengers.
In Go, the net package can be used to work with TCP:
package main
import (
"fmt"
"net"
)
func main() {
conn, err := net.Dial("tcp", "golang.org:80")
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
buf := make([]byte, 1024)
n, _ := conn.Read(buf)
fmt.Println(string(buf[:n]))
}
This example establishes a TCP connection with the server golang.org and sends an HTTP request.