المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Socket comunication problem



C# Programming
05-01-2009, 03:00 PM
Hy guys ! i've been trying to write up classes for a server and a client using sockets, i'm having a bit of a problem with the server : whenever i try to read incoming messages or send messages i get "Object reference not set to an instance of an object" error, this is because my streamWriter/Reader is null. My problem is that i can't figure out why they are null!? when i instance them there are no problems, but when i invoke their mothods they're null http://www.barakasoft.com/script/Forums/Images/smiley_line.gif , does anybody have any ideeas ?

some code snippets might help to clear this up :

the declarations :


class Server
{
private TcpListener listner;

private Socket socketForClients;

private System.IO.StreamReader messageReader;
private System.IO.StreamWriter messageWriter;


the instantianion :



public void startSever()
{

// listenning for incoming connections
listner.Start();

//create the socket for the clients
socketForClients = listner.AcceptSocket();

if (socketPentruClienti.Connected == true)
{
try
{
NetworkStream networkStream = new NetworkStream(socketForClients);
System.IO.StreamWriter messageWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader messageReader = new System.IO.StreamReader(networkStream);
}
catch (Exception eNetworkStream)
{
System.Windows.Forms.MessageBox.Show("EROARE : eroare la network stream\nEXCEPTIE : "+eNetworkStream.Message);
}// try/catch

}// if connected

}//startServer


the invoking :


public void write(string message)
{
try
{
messageWriter.WriteLine(message);
messageWriter.Flush();
}
catch (Exception eScriere)
{
System.Windows.Forms.MessageBox.Show("EROARE : serverul nu a putut trimite mesaj\nEXCEPTIE : " + eScriere.Message);
return;
}
}//write


PS : some of the text is in romanian but it's nothing important