The Echo program

The Echo program is the most basic example of network programming. After the network connection is established, the client sends a line of text to the server. After receiving the text, the server sends it back to the client.

Here Echo program is divided into two parts: client and server. The client part is implemented in Unity. For uniformity, the server side is implemented in C#.

The client

Make a simple UGUI interface in Unity.

The required UI includes:

①ConnectButtion button (used to establish a connection with the server)

(2) the InputField input box

③SendButton SendButton

④Text box (used to display the Text returned by the server)

The code for the Echo. Cs script is shown below

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net.Sockets; using UnityEngine.UI; public class Echo : MonoBehaviour { Socket socket; public InputField InputField; public Text text; public void Connection() { //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Socket. Connect (" 127.0.0.1 ", 8888); } public void Send() { //Send string sendStr = InputField.text; byte[] sendBytes = System.Text.Encoding.Default.GetBytes(sendStr); socket.Send(sendBytes); //Recv byte[] readBuff = new byte[1024]; int count = socket.Receive(readBuff); string recvStr = System.Text.Encoding.Default.GetString(readBuff, 0 , count); text.text = recvStr; //Close socket.Close(); }}Copy the code

Connection()

The client connects to the server through socket.connect(remote IP address, remote port).

Send()

The client sends data through socket.send (). This method takes a byte[] parameter to specify what to send. So you need to use System. Text. Encoding. The Default. GetBytes () to the output to the input box into a Byte string [] array, then sending data.

Receive()

The client receives server data through socket.receive (). After using the System. Text. Encoding. The Default. Get string (readBuff, 0, count) will byte [] array into a string to display to the screen.

Close()

The client closes the connection with socket.close ().

Then you just drag the Echo. Cs script to any object in the scene and assign values to the Inputfield and Test properties. Add click events to the two buttons and invoke the corresponding methods in the Echo component.

For example, a ConnectButton click event calls the Connection method of the Echo component.

The service side

Create a Console application in a tool such as MonoDevelop or Visual Studio.

The server code is as follows (in C#)

using System; using System.Net; using System.Net.Sockets; namespace EchoServer { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello world!" ); Socket listenfd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Parse("127.0.0.1"); //Bind IPAddress ipAdr = ipaddress.parse ("127.0.0.1"); IPEndPoint ipEp = new IPEndPoint(ipAdr, 8888); listenfd.Bind(ipEp); //Listen listenfd.Listen(0); Console.WriteLine("[server] started successfully "); while(true) { //Accept Socket connfd = listendfd.Accept(); Console.WriteLine("[server]Accept"); //Receive byte[] readBuff = new byte[1024]; int count = connfd.Receive (readBuff); string readStr = System.Text.Encoding.Default.GetString(readaBuff, 0 , count); Console.WriteLine("[server receive]"+readStr); //Send byte[] sendBytes = System.Text.Encoding.Default.GetBytes(readStr); connfd.Send(sendBytes); }}}}Copy the code

Binding to Bind

Listenfd.bind (ipEp) binds the IP and port to the Listenfd socket. The 127.0.0.1 used here is the loopback address, which refers to the local machine and is generally used for testing.

Listen to Listen

The server uses listenfd.Listen(backlog) to enable listening and waits for the client to connect. Backlog represents the maximum number of connections that a given queue can hold waiting for acceptance. 0 indicates no limit.

Reply the Accept

After listening is enabled, the server accepts the client connection via listenfd.accept ().

IPAddress and IPEndPoint

Use the former to specify the IP address and the latter to specify the IP address and port.

test

Run the server and client programs. Click the connect button on the client, enter text in the text box and click the send button. The server will return what you typed into the Text. I will try game development later, hey hey!