G
Guest
Hi all,
How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.
My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;
public Server(int portNumber)
{
this.intPortNumber = portNumber;
}
public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];
socketListener.Bind( new IPEndPoint( ipAddHost, intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback( this.OnConnectionRequest ),socketListener );
}
private void OnConnectionRequest(Socket client)
{
clients ++;
}
public int GetClientCount()
{
return clients;
}
-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.
[SetUp]
public void init()
{
Server server = new Server(portNumber);
}
[Test]
public void Test()
{
server.StartListening();
Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);
socketClient.Connect(endPoint);
Assert.AreEqual(1,server.GetClientCount());
}
but test is failing... at "Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>
can any one suggest me how can we use NUnit in this situation.
Cheers,
Naveen Mukkelli.
How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.
My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;
public Server(int portNumber)
{
this.intPortNumber = portNumber;
}
public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];
socketListener.Bind( new IPEndPoint( ipAddHost, intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback( this.OnConnectionRequest ),socketListener );
}
private void OnConnectionRequest(Socket client)
{
clients ++;
}
public int GetClientCount()
{
return clients;
}
-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.
[SetUp]
public void init()
{
Server server = new Server(portNumber);
}
[Test]
public void Test()
{
server.StartListening();
Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);
socketClient.Connect(endPoint);
Assert.AreEqual(1,server.GetClientCount());
}
but test is failing... at "Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>
can any one suggest me how can we use NUnit in this situation.
Cheers,
Naveen Mukkelli.