using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using UnityEngine;
// State object for receiving data from remote device.
using System.Collections;
public class StateObject {
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class SocketTest : MonoBehaviour {
// The port number for the remote device.
private const int port = 11000;
// The response from the remote device.
private static String response = String.Empty;
Socket client;
private void StartClient() {
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPAddress[] ipAddresses = Dns.GetHostAddresses( "10.0.0.10" );
IPAddress ipAddress = ipAddresses[ 0 ];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
client.ReceiveTimeout = 6000;
// Connect to the remote endpoint.
client.BeginConnect( remoteEP,
new AsyncCallback(ConnectCallback), client);
// Send test data to the remote device.
// Receive the response from the remote device.
// Write the response to the console.
//Console.WriteLine("Response received : {0}", response);
//// Release the socket.
//client.Shutdown(SocketShutdown.Both);
//client.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
private void ConnectCallback(IAsyncResult ar) {
try {
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Debug.Log("Socket connected to " + client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
Send(client, "This is a test");
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
private void Receive(Socket client) {
try {
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
private void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
Debug.Log( Encoding.ASCII.GetString(state.buffer,0,bytesRead) );
// Get the rest of the data.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback( ReceiveCallback ), state );
} else {
// All the data has arrived; put it in response.
// Signal that all bytes have been received.
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
private void Send(Socket client, String data) {
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
private void SendCallback(IAsyncResult ar) {
try {
// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Debug.Log( "Sent " + bytesSent + " bytes to server." );
// Signal that all bytes have been sent.
Receive(client);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
void Start() {
StartCoroutine( StartClientAsync() );
}
IEnumerator StartClientAsync() {
StartClient();
yield return new WaitForSeconds( 3 );
}
void OnDestroy() {
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
The code above is work fine on Mono2x, but when I upgrade to IL2CPP I get the exception:
System.Net.Sockets.SocketException: Operation on non-blocking socket would block
at System.Net.Sockets.Socket+SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in :0
at System.Net.Sockets.Socket.EndReceive (IAsyncResult asyncResult, SocketError& errorCode) [0x00000] in :0
at System.Net.Sockets.Socket.EndReceive (IAsyncResult result) [0x00000] in :0
at SocketTest.ReceiveCallback (IAsyncResult ar) [0x00000] in :0
at System.AsyncCallback.Invoke (IAsyncResult ar) [0x00000] in :0
at System.Net.Sockets.Socket+SocketAsyncResult.Complete () [0x00000] in :0
at System.Net.Sockets.Socket+Worker.Receive () [0x00000] in :0
This is the code for the server:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class StateObject {
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[ BufferSize ];
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener {
public static ManualResetEvent allDone = new ManualResetEvent( false );
public AsynchronousSocketListener() {
}
public static void StartListening() {
byte[] bytes = new byte[ 1024 ];
IPAddress[] ipAddress = Dns.GetHostAddresses( "10.0.0.10" );
Console.WriteLine( ipAddress[ 0 ].ToString() );
IPEndPoint localEndPoint = new IPEndPoint( ipAddress[ 0 ], 11000 );
Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
try {
listener.Bind( localEndPoint );
listener.Listen( 100 );
while ( true ) {
allDone.Reset();
Console.WriteLine( "Waiting for a connection..." );
listener.BeginAccept( new AsyncCallback( AcceptCallback ), listener );
allDone.WaitOne();
}
} catch ( Exception e ) {
Console.WriteLine( e.ToString() );
}
Console.WriteLine( "\nPress ENTER to continue..." );
Console.Read();
}
public static void AcceptCallback( IAsyncResult ar ) {
allDone.Set();
Socket listener = ( Socket )ar.AsyncState;
Socket handler = listener.EndAccept( ar );
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback( ReadCallback ), state );
}
public static void ReadCallback( IAsyncResult ar ) {
string content = string.Empty;
StateObject state = ( StateObject )ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive( ar );
if ( bytesRead > 0 ) {
state.sb.Append( Encoding.ASCII.GetString( state.buffer, 0, bytesRead ) );
content = state.sb.ToString();
if ( content.IndexOf( "" ) > -1 ) {
Console.WriteLine( "Read {0} bytes from socket. \n Data: {1}", content.Length, content );
int lastTickCount = Environment.TickCount;
while ( true ) {
if ( Environment.TickCount - lastTickCount > 6000 ) {
string contentTemp = DateTime.Now.ToLongTimeString().ToString() + content;
if ( handler.Connected ) {
Send( handler, contentTemp );
} else {
break;
}
lastTickCount = Environment.TickCount;
}
}
} else {
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback( ReadCallback ), state );
}
}
}
static void Send( Socket handler, string data ) {
byte[] byteData = Encoding.ASCII.GetBytes( data );
handler.BeginSend( byteData, 0, byteData.Length, 0, new AsyncCallback( SendCallback ), handler );
}
static void SendCallback( IAsyncResult ar ) {
try {
Socket handler = ( Socket )ar.AsyncState;
int bytesSent = handler.EndSend( ar );
Console.WriteLine( "Sent {0} bytes to client.", bytesSent );
//handler.Shutdown( SocketShutdown.Both );
//handler.Close();
} catch ( Exception e ) {
Console.WriteLine( e.ToString() );
}
}
public static int Main( string[] args ) {
StartListening();
return 0;
}
}
I have find the problem is the ReceiveTimeout property of the Socket.
Microsoft documentation say: this option applies to synchronous Receive calls only. If the time-out period is exceeded, the Receive method will throw a SocketException.
Reference: [link text][1]
[1]: https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivetimeout(v=vs.110).aspx
But I think Mono2x's Async Receive call is right do accord the Microsoft documentation, didn't effected by the ReceiveTimeout property. il2cpp async receive call do effected by the ReceiveTimeout property of Socket, which is wrong. So when the ReceiveTimeout reach, the Async Receive call complete with no data, then I get the Exception.
↧