1
나는 https://play.golang.org/p/zyZJKGFfyTGO 수신 대기로 TCP 서버를 유지하는 가장 좋은 방법은 무엇입니까?
package main
import (
"fmt"
"net"
"os"
)
// echo "Hello server" | nc localhost 5555
const (
CONN_HOST = "localhost"
CONN_PORT = "5555"
CONN_TYPE = "tcp"
)
func main() {
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
reqLen = reqLen
if err != nil {
fmt.Println("Error reading:", err.Error())
}
// Send a response back to person contacting us.
conn.Write([]byte("hello"))
conn.Close()
}
는 "테스트"반향이 예제를 발견 | nc 127.0.0.1 5555
프로덕션 환경에서 수신 대기중인 TCP 서버를 유지하는 가장 좋은 방법은 무엇입니까? 로컬 호스트의 은 정상적으로 작동하지만 생산은
나는 'sudo는 $의 GOPATH/빈/TCP'하지만 난 그게 backgrount입니다 – EdgarAlejandro
내가 "로컬 호스트"에서 CONN_HOST 변경에 실행할 수 있도록하는 방법으로 이동 프로그램을 실행 포트에 어떤 인터페이스에 허용됩니다 것을 의미합니다 "ip_server" – EdgarAlejandro