- Home /
how to use SocketAsyncEventArgs in unity3d
our game have to support wp8 platform, so i have to use SocketAsyncEventArgs rewrite my communication logic, but when i use SocketAsyncEventArgs in unity3d i can't get message exactly from server,here is my logic,please help me, Thanks!
public class NewBehaviourScript : MonoBehaviour
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
static IPAddress address = IPAddress.Parse("192.168.1.200");
static IPEndPoint hostEntry = new IPEndPoint(address, 3699);
//static IPAddress address = IPAddress.Parse("192.168.1.201");
//IPEndPoint hostEntry = new IPEndPoint(address, 7777);
public static Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Use this for initialization
void Start()
{
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 20), "click"))
Send();
}
static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case SocketAsyncOperation.Send:
ProcessSend(e);
break;
default:
throw new Exception("Invalid operation completed");
}
}
private static void ProcessConnect(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
// Successfully connected to the server
Debug.Log("connect success");
}
else
{
throw new SocketException((int)e.SocketError);
}
}
void Send()
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
byte[] buffer = Encoding.UTF8.GetBytes("Hello World");
socketEventArg.SetBuffer(buffer, 0, buffer.Length);
bool willRaiseEvent = sock.SendAsync(socketEventArg);
if (!willRaiseEvent)
{
ProcessSend(socketEventArg);
}
}
private static void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
// Received data from server
Debug.Log("get from server" + Encoding.UTF8.GetString(e.Buffer));
}
else
{
throw new SocketException((int)e.SocketError);
}
}
static void ProcessSend(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
//Read data sent from the server
Socket sock = e.UserToken as Socket;
bool willRaiseEvent = sock.ReceiveAsync(e);
if (!willRaiseEvent)
{
ProcessReceive(e);
}
}
else
{
throw new SocketException((int)e.SocketError);
}
}
}
Comment
Answer by dscroggi · Dec 31, 2014 at 02:17 AM
hard to tell with only the client code. looks ok overall just see one thing, setbuffer is not getting called before the receive
try [this] video series, it uses saea[1]
[1]: https://www.youtube.com/playlist?list=PLVrxpIRQe-GElmDh-apbTfViywsfHLGNj