Last active
November 14, 2024 09:07
-
-
Save unicolet/9b0aaec607dc77c1fcfa962b0ff8c699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"time" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
log.SetFlags(log.LstdFlags | log.Lmicroseconds) | |
listener, err := net.Listen("tcp", ":9876") | |
if err != nil { | |
log.Fatalf("Failed to listen: %v", err) | |
} | |
defer listener.Close() | |
log.Printf("TCP Echo Server listening on :9876 at epoch ms: %d", time.Now().UnixMilli()) | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Printf("Failed to accept connection: %v", err) | |
continue | |
} | |
tcpConn, ok := conn.(*net.TCPConn) | |
if ok { | |
tcpConn.SetNoDelay(true) | |
} | |
go handleConnection(conn) | |
} | |
} | |
func handleConnection(conn net.Conn) { | |
defer conn.Close() | |
remoteAddr := conn.RemoteAddr().String() | |
log.Printf("New connection from %s at epoch ms: %d", remoteAddr, time.Now().UnixMilli()) | |
reader := bufio.NewReader(conn) | |
for { | |
data, err := reader.ReadString('\n') | |
if err != nil { | |
if err != io.EOF { | |
log.Printf("Error reading from connection: %v", err) | |
} | |
return | |
} | |
nowMs := time.Now().UnixMilli() | |
dataMs, atolErr := strconv.ParseInt(strings.TrimSpace(data), 10, 64) | |
if atolErr != nil { | |
fmt.Println(atolErr) | |
fmt.Printf("[%d] From %s: %s\n", nowMs, remoteAddr, data) | |
} else { | |
fmt.Printf("[%d] From %s: %d [%dms]\n", nowMs, remoteAddr, dataMs, dataMs-nowMs) | |
} | |
//_, err = conn.Write([]byte(strconv.FormatInt(nowMs,10)+"\n")) | |
_, err = conn.Write([]byte(data)) | |
if err != nil { | |
log.Printf("Error writing to connection: %v", err) | |
return | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
HOST = 'localhost' | |
PORT = 8080 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.connect((HOST, PORT)) | |
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | |
for i in range(10): | |
current_time = int(time.time() * 1000) | |
s.sendall(str(current_time).encode() + b'\n') | |
print(f"Sent: {current_time} ms") | |
server_time = s.recv(1024).strip().decode() | |
server_time_int = int(server_time) | |
received_time = int(time.time() * 1000) | |
print(f"Receive time: {received_time} ms [{(received_time-current_time)/2} ms]") | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment