This is a creation in Article, where the information may have evolved or changed.
The general go language is to write the server, is mainly the easy way to write the thread of the process.
and Unity3d to communicate.
Main code
func main() {Addr, _ := net.ResolveTCPAddr("tcp4", ":6000")listener, err := net.ListenTCP("tcp", Addr)if err != nil {fmt.Fprintf(os.Stderr, "错误: %s", err.Error())os.Exit(1)}for {conn, err := listener.Accept()if err != nil {continue}go handleClient(conn)}}func handleClient(conn net.Conn) {defer conn.Close()var buf [128]bytefor {n, err := conn.Read(buf[0:])if err != nil {return}rAddr := conn.RemoteAddr()fmt.Println("接受的消息:", rAddr.String(), string(buf[0:n]))_, err2 := conn.Write([]byte("6*0.5"))if err2 != nil {return}}}
In U3d, you write code to accept the parameters you receive:
ClientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);string ip = GetIP ().ToString ();//服务器ipDebug.Log (ip);IPAddress ipa = IPAddress.Parse (ip);IPEndPoint iep = new IPEndPoint (ipa, 6000);try {ClientSocket.Connect (iep);//连接到服务器thread = new Thread (new ThreadStart (ClientReceive));thread.Start ();} catch (Exception ex) {Debug.Log (ex.Message);clientReceiveValue = ex.Message;}
Then write two threads, one to accept, one to send, the parameters to receive the game or program to draw.