Over half a day of Unity's API, also did not find the way to handle the socket. Unity itself has sealed a network processing class network, mainly for state synchronization. It seems to be implementing an MMO-level network application that can only be written in C #. (Unity does not resemble as, there is a direct processing packet, byte level of the underlying function.) At least not now. )
Let's take a look at unity first, and see how to write communications with unity from the simplest beginnings.
1, new project. A new project will have a camera inside.
2, write server-side code CS:
Using Unityengine;
Using System.Collections;
public class Server:monobehaviour {
int Port = 10000;
Ongui method, all GUI drawings need to be implemented in this method
void Ongui () {
Network.peertype is the state of the end type:
namely disconnected, connecting, server or client four kinds
Switch (network.peertype) {
Prevent client connections from running, server not initialized
Case networkpeertype.disconnected:
StartServer ();
Break
Run on the server side
Case Networkpeertype.server:
Onserver ();
Break
Run on client
Case Networkpeertype.client:
Break
Attempting to connect to server
Case networkpeertype.connecting:
Break
}
}
void StartServer () {
True when the user clicks on the button
if (Guilayout.button ("Create Server")) {
Initializes the native server port, and the first parameter is how many connections this machine receives
Networkconnectionerror error = Network.initializeserver (12,port,false);
Debug.Log ("error log" +error);
}
}
void Onserver () {
Guilayout.label ("Server is already running, waiting for client to connect");
Network.connections are all connected players, array []
The number of client connections taken.
int length = Network.connections.Length;
Output ip,port per client by array subscript
for (int i=0; i<length; i++)
{
Guilayout.label ("client" +i);
Guilayout.label ("Client IP" +network.connections[i].ipaddress);
Guilayout.label ("Client port" +network.connections[i].port);
}
True when the user clicks on the button
if (Guilayout.button ("Disconnect server")) {
Network.disconnect ();
}
}
/* The method provided by the system, which executes only once.
Use this for initialization
void Start () {
}
Update is called once per frame
void Update () {
}
Copy Code
3, drag the code onto the camera.
4, run the program File---->build settings---->build and run, select Web Player
5, the server side is established to maintain the server side of the running state.
6, here you can create a new project to write client. Too much trouble can also like me, first the camera on the server script deleted, and then the following client script dragged up.
The client code is as follows:
Using Unityengine;
Using System.Collections;
public class Client:monobehaviour {
The server address to connect to
String IP = "127.0.0.1";
The port to connect to
int Port = 10000;
void Ongui () {
State of the end type
Switch (network.peertype) {
Prevent client connections from running, server not initialized
Case networkpeertype.disconnected:
Startconnect ();
Break
Run on the server side
Case Networkpeertype.server:
Break
Run on client
Case Networkpeertype.client:
Break
Attempting to connect to server
Case networkpeertype.connecting:
Break
}
}
void Startconnect () {
if (Guilayout.button ("Connection Server")) {
Networkconnectionerror error = Network.connect (Ip,port);
Debug.Log ("Connection state" +error);
}
}
Use this for initialization
void Start () {
}
Update is called once per frame
void Update () {
}