I think you should implement socket communication by using thread.
I developed Android-PC socket communication project a year ago by using Unity 4.33f
.
First time, I implement communication logic in update so I can`t receive any message from server. I prefer you check your communication logic.
Here is main communication logic that I have implemented. I wish it helps your work.
//This function send packet according to the request
void SendPacket(byte[] msg,Request cmd)
{
switch (cmd)
{
case Request.LogInRequest:
try
{
//If socket exist,destroy it first.
if( GlobalInfo.NetworkActivity.currentSocket != null && GlobalInfo.NetworkActivity.currentSocket.Connected )
{
GlobalInfo.NetworkActivity.currentSocket.Shutdown( SocketShutdown.Both );
System.Threading.Thread.Sleep( 5 );
GlobalInfo.NetworkActivity.currentSocket.Close();
}
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(GlobalInfo.LoginUserInformation.serverIp),GlobalInfo.LoginUserInformation.serverPort);
GlobalInfo.NetworkActivity.currentSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
GlobalInfo.NetworkActivity.currentSocket.Blocking = false;
AsyncCallback onConnect = new AsyncCallback(OnConnect);
GlobalInfo.NetworkActivity.currentSocket.BeginConnect(endPoint,onConnect,GlobalInfo.NetworkActivity.currentSocket);
//Wait until connection established
connectDone.WaitOne();
}
catch(SocketException se)
{
GlobalInfo.NetworkActivity.isConnected = false;
print ("Socket Exception:" + se.ToString());
}
break;
case Request.LogOutRequest:
GlobalInfo.NetworkActivity.isLoggedFlag = false;
GlobalInfo.NetworkActivity.receivedThreadExitFlag = true;
break;
default:
break;
}
if (GlobalInfo.NetworkActivity.isConnected)
{
try{
AsyncCallback onSend = new AsyncCallback(OnSend);
GlobalInfo.NetworkActivity.currentSocket.BeginSend(msg,0,msg.Length,SocketFlags.None,onSend,0);
}catch(SocketException se){
print ("Socket Exception. Send Packet Failure." + se.ToString());
}
}
else
{
print ("Connection Problem. Send Packet Failure. Reconnect Please!");
GlobalInfo.NetworkActivity.currentSocket.Shutdown(SocketShutdown.Both);
GlobalInfo.NetworkActivity.currentSocket.Close();
}
}
//Receive Thread
void ReceiveThread()
{
... ... ...
bool bFlag = true;
while(bFlag){
if((GlobalInfo.NetworkActivity.isConnected == false) || GlobalInfo.NetworkActivity.receivedThreadExitFlag){
bFlag = false;
print ("Connection Problem! Receive Thread Exit!");
}else{
print ("Thread Ready!");
try{
AsyncCallback receiveData = new AsyncCallback(OnReceiveData);
GlobalInfo.NetworkActivity.currentSocket.BeginReceive(m_buffer,0,m_buffer.Length,SocketFlags.None,receiveData,GlobalInfo.NetworkActivity.currentSocket);
}catch(SocketException se){
bFlag = false;
print ("Receive Thread Error. Socket Exception:" + se.ToString());
}
}
}
}
//Receive Function
public void OnReceiveData(IAsyncResult ar)
{
Socket rec_sock = (Socket)ar.AsyncState;
try
{
int nBytesReceive = rec_sock.EndReceive(ar);
if(nBytesReceive >0)
{
byte[] receive = new byte[nBytesReceive];
for(int i = 0; i < nBytesReceive; i ++)
{
receive = m_buffer;
}
//Check Packet Type
DecompilePacket(receive);
// This is important command. Must deep think...ReceiveThread();
}
else
{
print ("Connection Problem. You have been disconnected.");
GlobalInfo.NetworkActivity.receivedThreadExitFlag = true;
GlobalInfo.NetworkActivity.currentSocket.Shutdown(SocketShutdown.Both);
GlobalInfo.NetworkActivity.currentSocket.Close();
}
}
catch(Exception e)
{
GlobalInfo.NetworkActivity.receivedThreadExitFlag = true;
print ("Receive Thread Exception:" + e.ToString());
}
}
//Send Function
void OnSend(IAsyncResult ar)
{
Socket rec_socket = (Socket)ar.AsyncState;
try{
rec_socket.EndSend(ar);
print ("Send Packet Successfully");
}catch(Exception se){
print("Send Packet Failure:" + se.ToString());
}
}
void SetPacket(Request cmd)
{
... .... ...
ModelSerializer mainMsgSerializer = new ModelSerializer ();
MemoryStream mainMsgMemoryStream = new MemoryStream ();
mainMsgSerializer.Serialize (mainMsgMemoryStream, qmdMessage);
//Do action according to the command
SendPacket(mainMsgMemoryStream.ToArray(),cmd);
}