Exemplo n.º 1
0
        private static string SocketSendReceive(string viewer, string owner, string gadget)
        {
            //  These keys need to match what you see in edu.ucsf.profiles.shindig.service.SecureTokenGeneratorService in Shindig
            string request = "c=default" + (viewer != null ? "&v=" + HttpUtility.UrlEncode(viewer) : "") +
                             (owner != null ? "&o=" + HttpUtility.UrlEncode(owner) : "") + "&u=" + HttpUtility.UrlEncode(gadget) + "\r\n";

            Byte[] bytesSent     = System.Text.Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];

            // Create a socket connection with the specified server and port.
            //Socket s = ConnectSocket(tokenService[0], Int32.Parse(tokenService[1]));

            // during startup we might fail a few times, so be will to retry
            string page = "";

            for (int i = 0; i < 3 && page.Length == 0; i++)
            {
                CustomSocket s = null;
                try
                {
                    s = sockets.GetSocket();

                    if (s == null)
                    {
                        return("Connection failed");
                    }

                    // Send request to the server.
                    DebugLogging.Log("Sending Bytes");
                    s.Send(bytesSent, bytesSent.Length, 0);

                    // Receive the server home page content.
                    int bytes = 0;

                    // The following will block until te page is transmitted.
                    do
                    {
                        DebugLogging.Log("Receiving Bytes");
                        bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                        page  = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
                        DebugLogging.Log("Socket Page=" + page + "|");
                    }while (page.Length == page.TrimEnd().Length&& bytes > 0);
                }
                catch (Exception ex)
                {
                    DebugLogging.Log("Socket Error :" + ex.Message);
                    page = "";
                }
                finally
                {
                    if (sockets != null)
                    {
                        sockets.PutSocket(s);
                    }
                }
            }
            return(page.TrimEnd());
        }