Problemw ith C# networking

Hi

I wrote my own network connection class in C#

public class ServerConnection {
	private string host;
	private int port;
	
	private object queueSyncObject = new object();
	
	private Socket mainSock;
	private byte [] buffer = new byte[2048];
	
	private Queue received = new Queue();
	
	private static ManualResetEvent connectDone = new ManualResetEvent(false);
	private static ManualResetEvent sendDone = new ManualResetEvent(false);
	private static ManualResetEvent receiveDone = new ManualResetEvent(false);
	
	public ServerConnection(string host, int port) {
		this.host = host;
		this.port = port;
	}
	
	private	void addMessageToQueue(string message)
	{
		lock(queueSyncObject)
		{
			received.Enqueue(message);
		}
	}
	
	public string getMessageFromQueue()
	{
		string toReturn = "";
		lock(queueSyncObject)
		{
			try {
				toReturn = (string)received.Dequeue();
				if (toReturn == null)
					toReturn = "";
			}
			catch(Exception)
			{
				toReturn = "";
			}
		}
		return toReturn;
	}
	
	public bool connect()
	{
		
		IPEndPoint server = null;
	
		try {
			mainSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			server = new IPEndPoint(IPAddress.Parse(host), port);
			
			mainSock.Blocking = false;
			AsyncCallback onConnect = new AsyncCallback( OnConnectCallback );
			mainSock.BeginConnect( server, onConnect, mainSock );
			
			connectDone.WaitOne();
		}
		catch(Exception ex) {
			Debug.Log(ex.Message);
			return false;
		}
		
		return true;
	}
	
	private void OnConnectCallback(IAsyncResult ar)
	{
		Socket sock = (Socket)ar.AsyncState;
		try {
			if (sock.Connected)
			{
				Debug.Log("Connected.");
				SetReceiveCallback( sock );
			}
			else
				Debug.Log("Can't connect!!!");
			connectDone.Set();
				
		}
		catch(Exception ex)
		{
			Debug.Log(ex.Message);
		}
	}
	
	private void SetReceiveCallback( Socket sock )
	{
		try {
			AsyncCallback receiveData = new AsyncCallback( OnReceiveDataCallback );
			sock.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, receiveData, sock);
			receiveDone.WaitOne();
		}
		catch(Exception ex)
		{
			Debug.Log(ex.Message);
		}
	}
	
	private void OnReceiveDataCallback(IAsyncResult ar)
	{
		Socket sock = (Socket)ar.AsyncState;
		try {
			int received = sock.EndReceive( ar );
			
			if (received > 0)
			{
				string message = Encoding.ASCII.GetString( buffer, 0, received);
				
				addMessageToQueue(message);
				
				Debug.Log("Received: " + message);
				SetReceiveCallback( sock );
				receiveDone.Set();
			}
			else
			{
				Debug.Log("Disconnected");
			}
		}
		catch(Exception ex)
		{
			Debug.Log(ex.Message);
		}
	}
	
	public void Send(string msg)
	{
		byte [] data = Encoding.ASCII.GetBytes(msg);
		
		mainSock.BeginSend( data, 0, data.Length, SocketFlags.None, new AsyncCallback( OnSendCallback), mainSock);
		sendDone.WaitOne();
	}
	
	private void OnSendCallback(IAsyncResult ar)
	{
		Socket sock = (Socket) ar.AsyncState;
		try {
			int sent = sock.EndSend(ar);
			Debug.Log("Sent " + sent + " bytes");
			sendDone.Set();
			
		}
		catch(Exception ex)
		{
			Debug.Log(ex.Message);
		}
	}

}

Problem: it only works when I have Mono attached to Unity (scripts in debug mode). When I try to run normally or built exe, it just hangs. I was waiting several minutes so it’s not timeout issue.
Does anyone have any idea about such behaviour?

Where does it stop? The player has a log. Did you check this?

Hm. I once had a different error, with C# and MySQL. I couldn’t run without MonoDevelop, and standalone builds didnt work.

I had to put two files in my project directory:

I18N.dll
I18N.West.dll

Found them at path: (Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\unity)

I’m think you have an error which also uses external libraries which are not used directly by Unity. (I’m not sure if these libraries will work for you, you might need different ones, and you might have a totally problem aswell, as far as I know, but it sounds like it is missing some .dll’s)

Player’s log didn’t show anything, but I used the secret method of ancient hackers (I manually appended lines to file) and found error in my script: in SetReceiveCallback shouldn’t be receiveDone.WaitOne();