Visual C# Developer Center > Visual C# Forums > Visual C# General > How to keep a socket open once & send message multiple times?
Ask a questionAsk a question
 

QuestionHow to keep a socket open once & send message multiple times?

  • Tuesday, November 03, 2009 11:32 AMprogramatic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Trying to implement a single client & server application. I only need "1" client. And this client needs to send multiple message at any time a day. So how do i open a connection once (in the morning) and keep it alive until the night. - saves me making a connection everytime to server each time i got a message i need to send (as the server will deal this with multiple threads)

    here's the code of what i got so far:

     public class Client
        {
            private static TcpClient tcpclient = null;
            private static NetworkStream ns = null;
            private static StreamReader reader = null;
            private static StreamWriter str = null;
    
            private static Client client = new Client();
    
            private Client() {}
    
            //Singleton - only need 1 client
            public static Client getInstance()
            {
                return client;
            }
    
            //connect once here
            public void Connect()
            {
                try
                {
                    tcpclient = new TcpClient("127.0.0.1", 6013);
    
                    Console.WriteLine("Server invoked");
    
                    ns = tcpclient.GetStream();
    
                    str = new StreamWriter(ns);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
    
            //this method should get called whenever a message need to be sent
            public void sendMessage(String message)
            {
                str.WriteLine(message);
                str.Flush();
                         
            }
    
            public static void CloseConnection()
            {
                reader.Close();
                str.Close();
                ns.Close();
                tcpclient.Close();
            }
        }
    
    Code compiles... appears its getting to "sendmessage" method but my server is not responding. BUT if i change the structure of above code to just connect-send-close everytime, then it works (so its not the server fault). From that it looks like the client is not properly sending the message across despite getting into that "sendmessage" method.

    If you could help me out, that would be great

    thanks in advance.

All Replies

  • Tuesday, November 03, 2009 12:18 PMManju Sandhu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Just send a ping request after particular interval from client to server to keep socket alive.
    But, if you send data after long time from client to server than i prefer you to open socket instead of keeping it alive for whole day.


    Regards,
    Manju Sandhu
    Regards, Manju Sandhu
  • Tuesday, November 03, 2009 4:13 PMprogramatic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How will i do that?

    All i want to know is why my above code wont work, and simply keep the socket connection alive for as long as i want it to until i manually close it.

  • Tuesday, November 03, 2009 5:46 PMLuke_UK Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Did you make the server or is it someone else's application? I ask because it sounds like the server may be waiting for a termination character or the end of the stream before it reacts. Normally, "Flush" would suffice.

    You should design your code to open/close the connection as needed and handle this gracefully because you will have problems with disconnections due to anything (power cuts, network cable being unplugged, application being turned off, server restarted... anything).

  • Tuesday, November 03, 2009 6:05 PMprogramatic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Did you make the server or is it someone else's application? I ask because it sounds like the server may be waiting for a termination character or the end of the stream before it reacts. Normally, "Flush" would suffice.

    You should design your code to open/close the connection as needed and handle this gracefully because you will have problems with disconnections due to anything (power cuts, network cable being unplugged, application being turned off, server restarted... anything).

    My Server. Its fine. Just it reads one line at a time.

    Perhaps its good to open/close connection as needed but how will that impact on the design + performance. Is that good OO design?
    What about performance? If i needed to send 100 messages - i would need to ( open - send - close ) 100 times. Doesnt that mean its no difference from 100 clients? All i need is one client that be able to send more than one single message at any time.

    thanks
  • Wednesday, November 04, 2009 12:33 PMLuke_UK Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Are you using a stream reader with the "ReadLine()" method? If so it is waiting for Environment.NewLine at the end of your sent message.

    Opening & closing connections is absolutely fine with TCP but it depends on how often you are going to do it and how often you will use it.

    For example, I have made a chat server/client that is used by all the employees at my company. I connect from client to server and keep the connection open because the server can send messages to the client throughout the day. If the connection is interrupted (power problems, restarts etc..) the client will notify the user and automatically attempt to reconnect every 30 seconds.

    If you are only going to be sending a message to the server once every hour or another long amount of time it might just be best to open/close as needed. However, it really is up to you as it seems you're in control of all of the hardware/software there. Either way won't cause much of a problem for you but if it is going over the internet then definitely open/close.

  • Saturday, November 07, 2009 3:52 PMprogramatic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    No i'm not.

    My server just simply constantly read in lines using a loop. it will terminte if it detects no line or new line which is essentially no line. A line must contain contents.

    The problem is i do open/close very often hence i want to open once and leave it open.

    But what i think the problem is the logical structure in my code.
  • Saturday, November 14, 2009 3:41 PMprogramatic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This problem is still unsolved. But for my Sofware context i don't think i require anymore.