Im just having issues all over the place haha…
Anyways, I have a program that im trying to test its functionality based upon socket messages that it receives. My problem now is that we have connections between the sender and receiver (server and client what ever you like to refer to them as). We can see that the sending side is sending 24 or so bytes of data and the receiving end is supposed to receive the data into a byte array then i parse it out into a message. The problem that were running into is that although the binding of the EndPoint is good, It is actively listening, and accepting data, Listener.Available is always 0, and Listener.Receive(buf) doesn’t put any of the 24 bytes of data into the buf[] array.
private void StartListening()
{
IPHostEntry ipHostInfo = null;
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("192.168.1.50"), port);
if (ipAddress == string.Empty)
localEP = new IPEndPoint(ipHostInfo.AddressList[2], port);
else
localEP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEP);
Thread.Sleep(100);
listener.Listen(10);
while (!bShutdown)
{
listener.Accept();
byte[] buf = new byte[255];
int iLastLength = 0;
//wait for all data to come in before parsing it (200ms character timeout)
iLastLength = listener.Available;
do
{
iLastLength = listener.Available;
Thread.Sleep(1000);
} while (iLastLength != listener.Available);
//if (listener.Available > 0)
{
string sReceived = "";
//pull data from socket
listener.Receive(buf);
//convert buffer to string
foreach (byte b in buf)
sReceived += (char)b;
//listener.Close();
OnDataEvent(new DataEventArgs(sReceived, null, string.Empty, 0, false, false, true));
}
//bShutdown = true;
}
}
catch (Exception e)
{
//listener.Close();
//listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (!bShutdown)
OnDataEvent(new DataEventArgs(null, "Listener Waiting Error: " + e.Message, "", 0, true, false, false));
}
bShutdown = true;
}
We have it sleeping at random points because we were concerned about running too quickly and throwing exceptions. As far as myself and my co-worker are concerned the code should be working perfectly however that’s not the case. Any ideas?