I have writed a simple wrapper of Winsock in C for C#. When i use Socket.Accept() in some thread and client.Connect in main thread - i sometimes( receive DllNotFoundException.
System.DllNotFoundException: Exception of type 'System.DllNotFoundException' was thrown. at (wrapper managed-to-native) TcpWrapper:ES_ConnectClient (string,int) at TcpClientSocket.Connect (System.String address, Int32 port) [0x00000] in C:...\ESCore\TcpClientSocket.cs:21
TcpClientSocket.Connect calling
[DllImport("ESocket")]
public static extern int ES_ConnectClient(string ip, int port);
I don't know why that happening rarely.
Some code:
listener = new TcpListenerSocket(50231); //calling bind from library here
if (listener.Start()) //calling listen from library
{
thread = new Thread(new ThreadStart(Listen));
thread.Start();
client = new TcpClientSocket();
if(client.Connect("localhost", 50231)) //exception here!
{
...
client.Close();
}
}
Thread code:
void Listen()
{
while (m_Running)
{
if (listener.Pending()) //select from library
{
TcpClientSocket socket = listener.Accept(); //accept from library
if (socket != null)
{
...
socket.Close();
}
}
}
}
Listener is also in library.
Library code: http://pastie.org/private/hdgl9zqxfjt2arlkj11q
Update: That happens only in Unity3d. In mono project and Microsoft .NET no errors.