Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Socket comunication problem

Socket comunication problem

Scheduled Pinned Locked Moved C#
helpsysadminquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rareseu
    wrote on last edited by
    #1

    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 :| , 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

    H 1 Reply Last reply
    0
    • R rareseu

      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 :| , 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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      rareseu wrote:

      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

      This is where your problem is likely to be. By declaring and instantiating the objects inside a code block (the try/catch block in this case) they are not visible to code outside of the block. You have already declared the streams outside the block, so you are declaring new ones inside the block. Just remove the bits, from inside the try block, struck out below and all should be well:

      System.IO.StreamWriter messageWriter =

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      R 1 Reply Last reply
      0
      • H Henry Minute

        rareseu wrote:

        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

        This is where your problem is likely to be. By declaring and instantiating the objects inside a code block (the try/catch block in this case) they are not visible to code outside of the block. You have already declared the streams outside the block, so you are declaring new ones inside the block. Just remove the bits, from inside the try block, struck out below and all should be well:

        System.IO.StreamWriter messageWriter =

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        R Offline
        R Offline
        rareseu
        wrote on last edited by
        #3

        Ah can't believe i missed that , thanks Henry ! i've got new problem now but i'l start a new thread if i can't figure it out

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups