G120 Connect to Server via TCP

On my G120, I’m trying to connect to a TCP Server. The code below works great if the server is up and running. But I want don’t want to assume that the server is running and handle the inability to connect to the server gracefully. My first thought was to try and use a “non-blocking” connection attempt, but that does not seem to be supported. All I want to do right now is try to connect, if I can great, else try again later.

One other piece of information, the code right now just sits on the “connect” line, even IF I start the server. The code only works if the server is running before the G120 starts running.

        public static void Main()


            while (!connectToServer()) { Debug.Print("trying to connect"); Thread.Sleep(1000); }

            while (true)
                Debug.Print("Phone Home");
                if (!phoneHome())


            } // Main Control Loop

        } // End Main

        private static bool connectToServer()

            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipe = new IPEndPoint(srvrIP, 2055);
            Debug.Print("Waiting For Server");
                // *** This code causes an exception, but I don't think it is what I want anyway.    
                //client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 });
                // ****
                Debug.Print("Sever Found");
                return true;
                return false;     


By the way, have you seen this codeshare:


Isn’t this going to still have the same problem at the “connect” line. Won’t the “Connect” still be blocking and thus it will just sit there and wait? Or am I missing something?

            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           [b] clientSocket.Connect(serverEndPoint);[/b]
            Debug.Print("Connected to remote server");

I haven’t tried it. I don’t know.
I thought I would just mention that we have FTP client code on codeshare that you might find useful in general.

By the way there was a hot fix for G120 that solved some socket creation issues:


Make sure you have this loaded.

@ Dan - Have you looked into using socket Poll?

See this link: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx

Notice that calling Poll with a SelectWrite and a timeout set should do what you want.


true, if processing a Connect, and the connection has succeeded;


true if data can be sent;

otherwise, returns false.

@ Dan - I checked further and using Poll won’t work either. :frowning:

@ Dan - I have another possibly “dumb” idea…

How about calling socket Connect in a separate thread. In your main thread you can set a timer to timeout after say 3 seconds. At timer timeout, you could check to see if your socket is connected. If not, abort the thread.

Thanks Jasdev, that is probably how I will have to do it. Hate to do it that way was trying to keep the threads to a minimum and keep things simple. But I tossed and turned and could not think of anything better.

Architect, thanks for the pointer to the FTP code! It did give me some good ideas on how to communicate back and forth.


Instead of aborting the thread, I would try to close the socket and handle the error in the thread. The thread could then be terminated normally.

This might be cleaner.