C#.net XMLSocket 应用程序

发布于 2024-10-02 23:06:36 字数 2929 浏览 0 评论 0原文

我正在尝试使用 XMLSocket 协议将 C#.net 应用程序连接到服务器,我可以使用以下 VB.net 代码来完成此操作,但我不确定如何在 C# 中执行此操作,因为我刚刚学习它。

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

I'm trying to make a C#.net app connect to a server using the XMLSocket protocal, I can do it with the following VB.net code but i'm not sure how to do it in C# as i'm just now learning it.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-10-09 23:06:36

您的所有代码都可以轻松转换为 C#。

如果您需要帮助,可以尝试使用这个等自动转换器,或者提出更具体的问题。

提示:

  • Chr(0) 的 C# 等效项是 '\0'
  • params 是一个 C# 关键字;您需要重命名该变量。

All of your code can be trivially converted to C#.

If you need help, you can try an automated converter like this one or ask a more specific question.

Tips:

  • The C# equivalent of Chr(0) is '\0'.
  • params is a C# keyword; you'll need to rename that variable.
笑咖 2024-10-09 23:06:36
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);
    }
}

我用过: http://www.developerfusion.com/tools/convert/vb -to-csharp/

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);
    }
}

I used: http://www.developerfusion.com/tools/convert/vb-to-csharp/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文