2013-11-27 65 views
0

我想從一個套接字編寫和讀取xml,但我不知道如何用.Net來做到這一點。一個SocketStream看起來像一個簡單的方法來做到這一點,但我沒有試圖建立一個Windows 8或Windows手機的應用程序。如果有人可以分享一些見解或者一些代碼瑕疵如何與.Net等價,我將不勝感激。SocketStream相當於.Net

 private const string MESSAGE_SERVER_URL = "xmpp.messenger.live.com"; 
     private const string MESSAGE_SERVER_PORT = "5222"; 

     /// <summary> 
     /// login into live message server 
     /// </summary> 

     private async void LoginXMPPServer(string access) 
     { 
      using (StreamSocket client = new StreamSocket()) 
      { 
       // connect to server 
       await client.ConnectAsync(new HostName(MESSAGE_SERVER_URL), MESSAGE_SERVER_PORT); 
       DataWriter writer = new DataWriter(client.OutputStream); 
       DataReader reader = new DataReader(client.InputStream); 
       reader.InputStreamOptions = InputStreamOptions.Partial; 

       //initialize a stream 
       string head = "<?xml version='1.0'?>" + 
        "<stream:stream " + 
        "to='messenger.live.com' " + 
        "xmlns='jabber:client' " + 
        "xmlns:stream='http://etherx.jabber.org/streams' " + 
        "version='1.0'>"; 
       await SendMessage(head, writer); 

       string reply = await ReadReplay(reader); 
       if (reply.Contains("stream:error")) 
        return; 
      } 
     } 
+0

採取了一個例子,我已經編輯您的提問和'[淨]'標籤取代了'[WPF]'標籤,因爲這並沒有真正與WPF有什麼關係。 –

回答

0

這是.net SendFile method

// Establish the local endpoint for the socket. 
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); 
IPAddress ipAddr = ipHost.AddressList[0]; 
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); 

// Create a TCP socket. 
Socket client = new Socket(AddressFamily.InterNetwork, 
     SocketType.Stream, ProtocolType.Tcp); 

// Connect the socket to the remote endpoint. 
client.Connect(ipEndPoint); 

// There is a text file test.txt located in the root directory. 
string fileName = "C:\\test.txt"; 

// Send file fileName to remote device 
Console.WriteLine("Sending {0} to the host.", fileName); 
client.SendFile(fileName); 

// Release the socket. 
client.Shutdown(SocketShutdown.Both); 
client.Close();