Golang and Unity3d for communication plotting ...

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.