Click here to Skip to main content
15,117,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a program that uses a socket that connects to a server, and waits for receiving data using the Socket.receive.

The question here is that, I need to send an message every 60 seconds to see if the server is still alive and then get back to receive state. I've been trying to use the ReceiveTimeout, but this, somehow, Closes the Socket (at least in debug i've seen that the Socket.Connected == false) and just like the MSDN says, it will throw an SocketExeption.HELP PLEASE!

C#
IPEndPoint EPhost = new IPEndPoint(hostadd, port);
           s.Connect(EPhost);
           Byte[] receive = new Byte[1024];
           s.ReceiveTimeout = 2000;
           while (true)
           {
               try
               {
                   Console.WriteLine("RECEIVING");
                   s.ReceiveTimeout = 2000;
                   s.Receive(receive);
                   
               }
               catch (SocketException sex)
               {
                  //I HAVE NO IDEIA WHAT I SHOULD DO HERE.

               }
           }
Posted

You don't need to stop your receive to do a send.

Of course, if the server sends a response to your ping, you'll have to expect that in your receive handler.

Nick
   
I am not very familiar with sockets, but would using something like the Socket.Poll method work for your purposes? http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx[^]
   
Comments
Nicholas Butler 17-Nov-10 16:12pm
   
The only way to test a TCP connection is to send a packet and see if it is acknowledged.
JOAT-MON 17-Nov-10 17:46pm
   
Interesting, so as I understand it then, the Socket.Poll() only checks the client's ability to connect to the server, but not the server's ability to listen and respond?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900