1024programmer Asp.Net Unity uses socket to transmit information (synchronization)

Unity uses socket to transmit information (synchronization)

Unity uses socket to transmit information (synchronization)

Client:

Create two Buttons in UGUI, one is to connect to the server, and the other is a button to send messages, and bind the Connection and SendMessage functions respectively

And create an InputField and a Text text. The Text text is used to display the message returned by the server, and the InputField is used to enter the message to be sent

The following is the code:

 1 using System.Collections;
  2 using  span> System.Collections.Generic;
  3 using  span> System.Net.Sockets;
  4 using  span> UnityEngine;
  5 using  span> UnityEngine.UI;
  6
  7 public class Client : MonoBehaviour
  8 {
  9  Socket client;
 10 public  span> InputField inputField;
 11 public  span> Text text;
 12
 13 public void Connection()
 14  {
 15 client = new   Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 16 client.Connect("127.0.0.1",10086  );
 17  }
 18
 19 public void SendMessage()
 20  {
 21 //Send data message
 22 string messageToServer =   inputField.text;
 23 byte  span>[] sendMessage = System.Text.Encoding.UTF8.GetBytes(messageToServer);
 24  client.Send  (sendMessage);
 25 //Receive data message
 26 byte[]  messageToClient = new byte[1024];
 27 int  span> count = client.Receive(messageToClient);
 28 string  span> messageFromServer = System.Text.Encoding.UTF8.GetString(messageToClient,0,count);
 29 text.text = messageFromServer;
 30  client.Close  ();
 31  }
 32 }

Client code

Server:

The server uses the WFP window provided by VS as the server. You can add some controls to beautify the GUI interface

The following is a simple GUI interface I made

 1 using System;
  2 using  span> System.Collections.Generic;
  3 using  span> System.Linq;
  4 using  span> System.Net;
  5 using  span> System.Net.Sockets;
  6 using  span> System.Reflection.Emit;
  7 using  span> System.Text;
  8 using  span> System.Threading.Tasks;
  9
 10 namespace Synchronous Chat Server
 11 {
 12 public  span> static class   Server
 13  {
 14 public  span> static string  showMessage="";
 15 public  span> static void   OpenServer()
 16  {
 17 Socket server =   new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 18 IPAddress ip = IPAddress.Parse("127.0.0.1");
 19 IPEndPoint serverAddress =   new IPEndPoint(ip, 10086);
 20  server.Bind  (serverAddress);
 21 server.Listen(10);//If zero, it means no limit
 22 showMessage += "Server started successfully";
 23 Socket oneToOnePort =  server.Accept();
 24 byte  span>[] messageFromClient = new byte  [1024];
 25 int  span> count = oneToOnePort.Receive(messageFromClient);
 26 string  span> message = Encoding.UTF8.GetString(messageFromClient, 0, count);
 27 showMessage +=  message;
 28 byte  span>[] messageToClient = Encoding.UTF8.GetBytes(message);
 29  oneToOnePort.Send  (messageToClient);
 30  }
 31
 32  }
 33 }

Server-side code

Translation

Search

Copy

le=”color: rgba(128, 0, 0, 1)”>”);
19 IPEndPoint serverAddress = new IPEndPoint(ip, 10086);
20 server.Bind (serverAddress);
21 server.Listen(10);//If zero, it means no limit
22 showMessage += Server started successfully;
23 Socket oneToOnePort = server.Accept();
24 byte span>[] messageFromClient = new byte [1024];
25 int span> count = oneToOnePort.Receive(messageFromClient);
26 string span> message = Encoding.UTF8.GetString(messageFromClient, 0, count);
27 showMessage += message;
28 byte span>[] messageToClient = Encoding.UTF8.GetBytes(message);
29 oneToOnePort.Send (messageToClient);
30 }
31
32 }
33 }

Server-side code

Translation

Search

Copy

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/unity-uses-socket-to-transmit-information-synchronization/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索