A
Ankit Aneja
giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.
it is breaking on line:if(cl.status==true)
code for listen class
public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client[] cl=new client[5];
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Enter the listening loop.
// for(int i=0;i<5;i++)
// {
// cl.status=true;
// }
Boolean flag;
while(true)
{ flag=false;
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
for(int i=0;i<5;i++)
{
if(cl.status==true)
{
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();
flag=true;
break;
}
}
if(flag!=true)
{
//display error message
}
}
}
}
code for client class
public class client
{
TcpClient tcpClient;
public Boolean status;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client()
{ //
// TODO: Add constructor logic here
//
//status=true;
}
public client(TcpClient Client)
{
tcpClient =Client;
//
// TODO: Add constructor logic here
//
status=false;
}
public void getClient()
{
try
{
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
MessageBox.Show(se.ToString());
}
// Shutdown and end connection
tcpClient.Close();
status=true;
}
}
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.
it is breaking on line:if(cl.status==true)
code for listen class
public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client[] cl=new client[5];
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Enter the listening loop.
// for(int i=0;i<5;i++)
// {
// cl.status=true;
// }
Boolean flag;
while(true)
{ flag=false;
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
for(int i=0;i<5;i++)
{
if(cl.status==true)
{
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();
flag=true;
break;
}
}
if(flag!=true)
{
//display error message
}
}
}
}
code for client class
public class client
{
TcpClient tcpClient;
public Boolean status;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client()
{ //
// TODO: Add constructor logic here
//
//status=true;
}
public client(TcpClient Client)
{
tcpClient =Client;
//
// TODO: Add constructor logic here
//
status=false;
}
public void getClient()
{
try
{
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
MessageBox.Show(se.ToString());
}
// Shutdown and end connection
tcpClient.Close();
status=true;
}
}