[Unity3d ARPG online game Programming practice]
Want to kill, Baidu has a half-day also do not know how to write local services, the results of the server in the resources can be used ~ ~ ~ (>_<) ~ ~ ~
Two methods are used
1 This method particularly depressed, own write server, own write client can communicate. But this server used in [Unity3d ARPG Network Game Programming practice] can not link success, fortunately, the resources with the start of local services program, no reason, do not understand the principle of server links
Version Xuanhusung Server client communication code
Server-side using unityengine;using System.collections;public class Localserver:monobehaviour {int port = 10110; void Ongui () {switch (network.peertype) {case networkpeertype.client://successfully connected to client Break Case networkpeertype.connecting://is attempting to connect break; Case networkpeertype.disconnected://is not started, this starts the network connection startserver (); Break Case networkpeertype.server://successfully connected server onserver (); Break }} void StartServer () {if (Guilayout.button ("Create local server")) {//Create local server allow 10 host connection, third parameter: whether or not to support NAT-Mode connection Networkconnectionerror error = Network.initializeserver (port, false); If the connection fails, print the error message Debug.Log ("Connection Status:" + error); }} void Onserver () {Guilayout.label ("server created, waiting for user to connect"); Get the number of users connected int length = Network.connections.Length; for (int i =0; i < length; i++) {Guilayout.label ("Connection Server client ID:" + i); Guilayout.label ("Connect Server client IP:" + network.connections[i].ipaddress); Guilayout.label ("Connection Server client port number:" + network.connections[i].port); } if (Guilayout.button ("Disconnect local Connection")) {network.disconnect (); }}} client code using unityengine;using System.collections;public class Clientserver:monobehaviour {//server address to connect//s Tring IP = "192.168.110.231"; String IP = "127.0.0.1"; The port to connect to int port = 10110; void Ongui () {///End Type status switch (network.peertype) {//Prevent client connection from running, server not initialized Case NetworkPeerType.Disconnected:StartConnect (); Break Run on server-side case NetworkPeerType.Server:break; Run on client case NetworkPeerType.Client:break; Trying to connect to server case networkpeertype.connecting: Break }} void Startconnect () {if (Guilayout.button ("Connection Server")) {Networkconnectionerror error = Network.connect (IP, Port); Debug.Log ("Connection status" + error); } } }
2 [Unity3d ARPG Network game Programming practice] Video Resources
Only client-side code (note that the NetworkSript1 class does not inherit from Monobehaviour and cannot be added directly to the camera, you can add Newscript script in the Camera component window)
Using unityengine;using system.collections;using system.net.sockets;public class networksript1{ private static Networkscript instance; private static socket socket; private static string IP = "127.0.0.1"; private static int port = 10100; public static Networkscript getinstance () { if (instance = = null) { instance = new Networkscript (); Init (); } return instance; } public static void Init () { try { socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket. Connect (IP, port); Debug.Log ("Server connection succeeded"); } Catch { Debug.Log ("Server Connection Failed"); } }}
Specific operation:
1 Remove camera, add widgets using Ngui
2 Creating a resource and adding a button to the scene
3 Adding a network connection script to the camera: the NetworkSript1 class does not inherit from the Monobehaviour and cannot be added directly to the camera, adding a script in the Camera Component window (add Newscript)
3.1 Socket Object (Inter network connection type, stream mode transfer, TCP protocol)
3.2 function Connect (ip,port)
4 Add the connection code in the callback function of the button
Summarize
Socket.beginreceive method (byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object)
Begins to receive data asynchronously from the connected Socket.
Public IAsyncResult BeginReceive (
byte[] Buffer,
int offset,
int size,
SocketFlags SocketFlags,
Out SocketError ErrorCode,
AsyncCallback Callback,
Object State
)
Parameters
Buffer
Type: system.byte[]
An array of type Byte, which is the location where the received data is stored.
Offset
Type: System.Int32
The location where the received data is stored in buffer.
Size
Type: System.Int32
The number of bytes to receive.
SocketFlags
Type: System.Net.Sockets.SocketFlags
A bitwise combination of the SocketFlags values.
ErrorCode
Type: System.Net.Sockets.SocketError
A SocketError object that stores socket errors.
Callback
Type: System.AsyncCallback
A AsyncCallback delegate that references the method to invoke when the operation is complete.
State
Type: System.Object
A user-defined object that contains information about the receive operation. When the operation is complete, this object is passed to the EndReceive delegate.
return value
Type: System.IAsyncResult
A IAsyncResult that references asynchronous reads.
[Unity3d ARPG Network Game Programming Practice]1--Unity Network programming