none
socket 的 receive停住,什麼問題?? RRS feed

  • 問題

  • 各位達人好

    關於socket連webservice,走ssl+443,傳一個string給webservice,然後回傳一個string,以下是程式片段,會停在 receive, 請各位幫忙看哪裡有誤? 感謝!!

     

     

    static String DoSocketPost(String server, String searchpath,  String data)
        {
           
            // Sets up variables and a string to write to the server
            Encoding ASCII = Encoding.ASCII;
            String StrGet = "POST " + searchpath + " HTTP/1.1\r\n" ;
            StrGet += "Host: " + server+"\r\n";
            StrGet += "Content-type: application/x-www-form-urlencoded\r\n";
            StrGet += "Content-Length: "+data.Length+"\r\n";

            StrGet += "d = "+data;
           
            Byte[] ByteGet = ASCII.GetBytes(StrGet);

            Byte[] RecvBytes = new byte[256];
            String strRetPage="";

             //IPAddress and IPEndPoint represent the endpoint that will
             //  receive the request.
             //Gets the first IPAddress in the list using DNS.
            IPAddress hostadd = Dns.GetHostEntry(server).AddressList[0];//Dns.Resolve(server).AddressList(0);
            IPEndPoint EPhost = new IPEndPoint(hostadd, 443);

            //Creates the Socket for sending data over TCP.
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Socket(InterNetwork, Net.Sockets.SocketType.Stream, Tcp);
           

            // Connects to the host using IPEndPoint.
            s.Connect(EPhost);
            if (!s.Connected)
            {
                strRetPage = "Unable to connect to host";
                return strRetPage;
            }

            // Sends the GET text to the host
            int sendSucc = s.Send(ByteGet, ByteGet.Length, 0);  //這裡sendSucc有值,應該有傳出? 傳到哪裡??
            Int32 bytes;
            try
            {
                while (true)
                {
                    // Receives the page, looping until all bytes are received
                    bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);       //這裡停住 , 怎麼辦??
                    if (bytes != 0)
                        break;
                }
              
                strRetPage = "Default HTML page on " + server + ":\r\n";
           
                while (bytes > 0)
                {
                    bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
                    strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
                }

                strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
            }
            catch(Exception ee)
            {

            }
            Console.WriteLine(strRetPage);
            Console.ReadLine();
            //While (bytes > 0)
            //{
            //    bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)
            //    strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
            //}
            return strRetPage;
        }

    2008年1月31日 上午 09:36

所有回覆

  • HI,

     

    既然是WEB Service, 就直接呼叫Web Service就好了, 可以省去傳遞和接收資料的工作

     

    tihs

    2008年2月1日 上午 04:02
  • 謝謝this大抽空回應!!

     

    我知道可以用加入的方式,但專案需求,所以要這樣作,

     

     

    希望諸位達人多多指導,感謝您!!

     

    2008年2月1日 上午 04:49
  • 如果無資料可供讀取,Receive 方法會封鎖到有資料可用為止,除非使用 Socket.ReceiveTimeout 設定了逾時值。如果超過逾時值,則 Receive 呼叫會擲回 SocketException。如果您處於未封鎖模式,而且在通訊協定堆疊緩衝區中並沒有可用的資料,Receive 方法將會立即完成,並擲回 SocketException。您可以使用 Available 屬性,判斷資料是否可供讀取。當 Available 為非零值時,請重試接收作業。

     

     

    // Get reply from the server.
      int byteCount = server.Receive(bytes, server.Available, 
                                               SocketFlags.None);
    2008年2月1日 上午 05:27
  • 謝謝 cmf 詳細的回覆!!

     

    經查的確是沒資料,我在Webservice裡想要把接到的直寫到log,經查發現沒有,所以影該是沒連上WebService??

     

    所以,請問socket該怎麼連webservice??

    以下是我的測試片段,沒成功!!

    searchpath = "/service1.asmx/myService";

    server = "localhost";
    data = "2232";
    String msg = DoSocketPost(server, searchpath, data);

     

    謝謝!!

     

    抱歉!! 錯字!!

    2008年2月1日 上午 06:51
  • 存取 SSL 要配合 SslStream 類別,沒看到你在用...
    2008年2月2日 上午 08:34
  • 謝謝璉大的指導!!

     

    我參考了以下的文件後,

    http://technet.microsoft.com/en-us/library/system.net.security.sslstream.aspx

     

    成功連上了,傳值過去,回傳卻是 http 100   , 爬了文有段如下 :

     

    HTTP 100 continue is an optimization. It is sent by the server to tell
    the client to continue doing whatever it was going to do on that
    request. It is used for sending entity body, in the following manner:
    1. Client sends request without entity body to server
    2. Server responds with 100 continue
    3. Client sends entity body to server
    4. Server response with status code of processing the request

     

    摘自 http://www.eggheadcafe.com/software/aspnet/30279360/http-status-100.aspx

     

    因此,我的問題是 :

     

    在webservice的post方法是以下,我照著這串傳過去的,請問還要傳body?? 要傳什麼呢??

    HTTP POST

    下列是 HTTP POST 要求與回應的範例。預留位置顯示之處必須代入實際的值。

    POST /Service1.asmx/myService HTTP/1.1
    Host: xx.xx.com.tw
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length
    
    d=string

     

    感謝您!!

     
    2008年2月4日 上午 01:18