CHAT-gpt에게 golang 소켓 코드 요청한 후 결과

package main


import (

 "fmt"

 "net"

 "os"

)


const (

 connHost = "localhost"

 connPort = "8000"

 connType = "tcp"

)


func main() {

 // Connect to the server

 conn, err := net.Dial(connType, connHost+":"+connPort)

 if err != nil {

  fmt.Println("Error connecting:", err.Error())

  os.Exit(1)

 }

 // Send some data

 _, err = conn.Write([]byte("Hello Server!"))

 if err != nil {

  fmt.Println("Error writing:", err.Error())

  os.Exit(1)

 }

 // Receive some data

 buffer := make([]byte, 1024)

 n, err := conn.Read(buffer)

 if err != nil {

  fmt.Println("Error reading:", err.Error())

  os.Exit(1)

 }

 // Print the received data

 fmt.Println("Received:", string(buffer[:n]))

 // Close the connection

 conn.Close()

}


다음 내용이 궁금하다면?

또는

이미 회원이신가요?

2023년 11월 15일 오전 7:35

댓글 2