Coder Social home page Coder Social logo

unicode string decoded error about clrzmq4 HOT 18 CLOSED

zeromq avatar zeromq commented on June 5, 2024
unicode string decoded error

from clrzmq4.

Comments (18)

goldenbull avatar goldenbull commented on June 5, 2024

ZFrame is a Stream, string reading/writing functions are in StreamReader/StreamWriter class, so I think maybe it's not necessary to encode/decode string byte-by-byte again.

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

Awesome, you are actually using Unicode! 16bit or 32bit?

You should use

using System;
using System.Text;
using ZeroMQ;

private static void Main(string[] args)
{
    var push = new ZSocket(ZSocketType.PUSH);
    push.Bind("tcp://127.0.0.1:12345");
    var pull = new ZSocket(ZSocketType.PULL);
    pull.Connect("tcp://127.0.0.1:12345");

    string str = "中文字符串";
    Console.WriteLine("str send: {0}", str);
    push.SendFrame(new ZFrame(str, Encoding.Unicode));

    using (ZFrame frame = pull.ReceiveFrame())
        Console.WriteLine("str recv: {0}", frame.ReadString(Encoding.Unicode));
}

This is because ZContext.Encoding is Encoding.UTF8... Please, try this one!

The problem with StreamReader/StreamWriter classes is, they are closing the stream after their use - and I don't want a ZFrame being closed before I do so...

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

Encoding.Unicode is fine.
StreamReader and StreamWriter constructor has a leaveOpen parameter since .net 4.5, maybe that will help:
https://msdn.microsoft.com/en-us/library/gg712853(v=vs.110).aspx

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

".net 4.5" - I don't want to say they could have done this in ".net 2.0"

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

For .net 2.0, we can derive a custom class from StreamReader/StreamWriter and override the Dispose method not to close underlying stream.

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

Yes, this is what ZFrame is basically all about - Dismiss, Dispose and not going to close the stream, when someone is reading from or writing on it.

I also thought about a ZFrameReader and ZFrameWriter. But I don't like the creation of additional objects, just to read and to write. This is why I made these functions directly into the stream, the ZFrame.

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

Agree. Still I think it's better if we can use StreamReader/Writer with some simple work-around, and not to re-invent the wheels 😄

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

However, you can do so already... You just need someone initializing your ZFrame on the length of the string.

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

In fact I think we do not need to support net 2.0 any more

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

Not .NET 2.0, but I do like .NET 4.0 because it runs on WinXP. .NET 4.5 doesn't.

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

So what's your plan of this bug? If users are required to use Encoding.Unicode, the API should not have an encoding parameter.

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

I can try to make a PR of a custom derived StreamReader/Writer if you are OK with this kind of work-around

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

This is actually a really hard PR, because you need to write a new ZFrame constructor...

The Encoding is there to actually read/write a string on a ZFrame. This may be removed, if you have a ZFrameReader and ZFrameWriter.

You may try...

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

I will try to make it as simple as possible

from clrzmq4.

metadings avatar metadings commented on June 5, 2024

The new ZFrame constructor will be you biggest problem, because you need someone initializing the ZFrame to a specific Length. The other changes are in ReadString, ReadLine and WriteString, WriteLine... And you need to make ZFrameReader/Writer classes.

Let's see, now I'm curious about your PR :-) (Please try using TAB instead of SPACEs for intendation!)

from clrzmq4.

sigiesec avatar sigiesec commented on June 5, 2024

I am a bit lost here... Was this really a bug? Or was clrzmq4 used in a wrong way? If it was a bug, was is solved by #57? Is there any reason to keep this issue open?

from clrzmq4.

goldenbull avatar goldenbull commented on June 5, 2024

IMO, if you do not care much about the performance hit of memory copy, you can just treat ZFrame as a byte stream.

from clrzmq4.

sigiesec avatar sigiesec commented on June 5, 2024

@goldenbull Sure you can do this.

Is there any reason to keep this issue open from your point of view?

from clrzmq4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.