Windows Phone Socket Programmierung

  • Ich möchte eine Verbindung zum http-Server unter Verwendung von Sockets unter Windows Phone 7.5 (MANGO) herstellen. Code sieht wie folgt aus:

     public class sockets
        {
            // Buffer for incoming data
            private byte[] _receiveBuffer;
    
            public void Demo()
            {
                String host = "209.85.148.106";
                int port = 80;
            String Message = "GET / HTTP/1.1 Host: google.pl User-Agent: Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8 Accept-Language: pl,en-us;q=0.7,en;q=0.3 Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n";
    
            // create endpoint
            var ipAddress = IPAddress.Parse(host);
            var endpoint = new IPEndPoint(ipAddress, port);
    
            // convert text to send (prefix with length)
            //var message = string.Format("{0};{1}", Message.Length, Message);
            var buffer = Encoding.UTF8.GetBytes(Message);
    
            // create event args
            var args = new SocketAsyncEventArgs();
            args.RemoteEndPoint = endpoint;
            args.Completed += SocketAsyncEventArgs_Completed;
            args.SetBuffer(buffer, 0, buffer.Length);
    
            // create a new socket
            var socket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);
    
            // connect socket
            bool completesAsynchronously = socket.ConnectAsync(args);
    
            // check if the completed event will be raised.
            // if not, invoke the handler manually.
            if (!completesAsynchronously)
            {
                SocketAsyncEventArgs_Completed(args.ConnectSocket, args);
            }
        }
    
        private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            // check for errors
            if (e.SocketError != SocketError.Success)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                return;
            }
    
            // check what has been executed
            switch (e.LastOperation)
            {
                case SocketAsyncOperation.Connect:
                    HandleConnect(e);
                    break;
                case SocketAsyncOperation.Send:
                    HandleSend(e);
                    break;
                case SocketAsyncOperation.Receive:
                    HandleReceive(e);
                    break;
                case SocketAsyncOperation.ReceiveFrom:
                    HandleReceive(e);
                    break;
            }
        }
    
        private void HandleConnect(SocketAsyncEventArgs e)
        {
            if (e.ConnectSocket != null)
            {
                // simply start sending
                bool completesAsynchronously = e.ConnectSocket.SendAsync(e);
    
                // check if the completed event will be raised.
                // if not, invoke the handler manually.
                if (!completesAsynchronously)
                {
                    SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
                }
            }
        }
    
        private void HandleSend(SocketAsyncEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("WYSLANO");
    
            // simply start sending
            bool completesAsynchronously = e.ConnectSocket.ReceiveAsync(e);
    
            // check if the completed event will be raised.
            // if not, invoke the handler manually.
            if (!completesAsynchronously)
            {
                SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
            }
        }
    
        private void HandleReceive(SocketAsyncEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("ODEBRANO");
    
    
        }
    }
     

    Einige dieser Codes stammen von manuellen usw. Der Versuch, sich mit http://google.pl Sendet Daten, empfängt jedoch nichts.

    24 August 2011
    James
1 answer
  • Ich sehe, Sie senden am Ende Ihrer Nachricht einen Zeilenumbruch / Zeilenvorschub. Sie müssen zwei senden, um zu signalisieren, dass Sie mit Ihrer Anfrage fertig sind.

    Eine bessere Option kann die Verwendung der WebClient-Klasse der HttpWebRequest-Klasse sein, da beide eine ordnungsgemäß erstellte HTTP-Anforderung für Sie senden.

    02 February 2012
    Joel