2010-11-17 6 views
0

J'essaie de connecter une application C# .net à un serveur en utilisant le protocole XMLSocket, je peux le faire avec le code VB.net suivant mais je ne suis pas sûr de savoir comment le faire C# comme je l'apprends maintenant.C# .net Application XMLSocket

Public Sub connect(ByVal host As String, ByVal port As Integer) 
     Try 
      mobjClient = New TcpClient(host, port) 
      mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing) 
      DisplayText("Connected to " & host & " On port " & port) 
     Catch 
      MarkAsDisconnected("Connection error.") 
     End Try 
    End Sub 

    Public Sub Send(ByVal t As String, Optional ByVal disp As Boolean = True) 
     Try 
      Dim w As New IO.StreamWriter(mobjClient.GetStream) 
      w.Write(t & Chr(0)) 
      w.Flush() 
      If disp = True Then 
       'DisplayText(t) 
      End If 
     Catch 
      DisplayText("Error Sending!") 
     End Try 
    End Sub 

    Private Sub DoRead(ByVal ar As IAsyncResult) 
     Dim intCount As Integer 
     Try 
      intCount = mobjClient.GetStream.EndRead(ar) 
      If intCount < 1 Then 
       'MarkAsDisconnected("Error reading Stream!") 
       DisplayText("Error reading stream.") 
       'Exit Sub 
      End If 

      BuildString(marData, 0, intCount) 

      mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing) 
     Catch e As Exception 
      MarkAsDisconnected("Reconnecting...") 
      connect("example.com", 7777) 
      LogIn("lograinbows", "inthesky", "DRESSLOGCASINO") 
     End Try 
    End Sub 

    ''// This is important! Keep the Decoder and reuse it when you read this socket. 
    ''// If you don't, a char split across two reads will break. 
    Dim decoder As Decoder = Encoding.UTF8.GetDecoder() 



    Private Sub BuildString(ByVal bytes() As Byte, ByVal offset As Integer, ByVal byteCount As Integer) 
     Try 
      ''// Here's where the magic happens. The decoder converts bytes into chars. 
      ''// But it remembers the final byte(s), and doesn't convert them, 
      ''// until they form a complete char. 
      Dim chars(bytes.Length) As Char 
      Dim charCount As Integer = decoder.GetChars(bytes, offset, byteCount, chars, 0) 

      For i As Integer = 0 To charCount - 1 
       If chars(i) = Chr(0) Then   ''// The fix for bullet #2 
        mobjText.Append(vbLf) 

        Dim params() As Object = {mobjText.ToString} 
        Me.Invoke(New DisplayInvoker(AddressOf Me.HandleXML), params) 

        ''// You don't have to make a new StringBuilder, BTW -- just clear it. 
        mobjText.Length = 0 
       Else 
        mobjText.Append(chars(i)) 
       End If 
      Next 
     Catch e As Exception 
      DisplayText("Error: ", e.Message) 
     End Try 
    End Sub 
+1

je l'espère, pour vous, que ce n'est pas vos informations de connexion réelle dans le code. Si c'est le cas: Changez votre mot de passe dès que possible! – jgauffin

+0

Heh, il serait stupide d'utiliser un vrai mot de passe dedans. J'aime les faux. –

Répondre

2

Tout votre code peut être trivialement converti en C#.

Si vous avez besoin d'aide, vous pouvez essayer un convertisseur automatisé comme this one ou poser une question plus spécifique.

Conseils:

  • Le C# équivalent de Chr(0) est '\0'.
  • params est un mot-clé C#; vous devrez renommer cette variable.
0
public void connect(string host, int port) 
{ 
    try { 
     mobjClient = new TcpClient(host, port); 
     mobjClient.GetStream.BeginRead(marData, 0, 1024, DoRead, null); 
     DisplayText("Connected to " + host + " On port " + port); 
    } catch { 
     MarkAsDisconnected("Connection error."); 
    } 
} 

public void Send(string t, bool disp = true) 
{ 
    try { 
     System.IO.StreamWriter w = new System.IO.StreamWriter(mobjClient.GetStream); 
     w.Write(t + Strings.Chr(0)); 
     w.Flush(); 
     if (disp == true) { 
      //DisplayText(t) 
     } 
    } catch { 
     DisplayText("Error Sending!"); 
    } 
} 

private void DoRead(IAsyncResult ar) 
{ 
    int intCount = 0; 
    try { 
     intCount = mobjClient.GetStream.EndRead(ar); 
     if (intCount < 1) { 
      //MarkAsDisconnected("Error reading Stream!") 
      DisplayText("Error reading stream."); 
      //Exit Sub 
     } 

     BuildString(marData, 0, intCount); 

     mobjClient.GetStream.BeginRead(marData, 0, 1024, DoRead, null); 
    } catch (Exception e) { 
     MarkAsDisconnected("Reconnecting..."); 
     connect("xivio.com", 7777); 
     LogIn("lograinbows", "inthesky", "DRESSLOGCASINO"); 
    } 
} 

//'// This is important! Keep the Decoder and reuse it when you read this socket. 
//'// If you don't, a char split across two reads will break. 

Decoder decoder = Encoding.UTF8.GetDecoder(); 


private void BuildString(byte[] bytes, int offset, int byteCount) 
{ 
    try { 
     //'// Here's where the magic happens. The decoder converts bytes into chars. 
     //'// But it remembers the final byte(s), and doesn't convert them, 
     //'// until they form a complete char. 
     char[] chars = new char[bytes.Length + 1]; 
     int charCount = decoder.GetChars(bytes, offset, byteCount, chars, 0); 

     for (int i = 0; i <= charCount - 1; i++) { 
      //'// The fix for bullet #2 
      if (chars[i] == Strings.Chr(0)) { 
       mobjText.Append(Constants.vbLf); 

       object[] @params = { mobjText.ToString }; 
       this.Invoke(new DisplayInvoker(this.HandleXML), @params); 

       //'// You don't have to make a new StringBuilder, BTW -- just clear it. 
       mobjText.Length = 0; 
      } else { 
       mobjText.Append(chars[i]); 
      } 
     } 
    } catch (Exception e) { 
     DisplayText("Error: ", e.Message); 
    } 
} 

J'utilisé: http://www.developerfusion.com/tools/convert/vb-to-csharp/

Questions connexes