Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# - Reading a String from a Stream (IO)

Discussion in 'Scripting' started by Christian-Tucker, Oct 17, 2014.

  1. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376
    Note: I have finished converting the DataInputStream and DataOutputStream classes from Java to C# - You can get them here: https://bitbucket.org/CTucker1327/c-datastreams/

    This problem is solved.

    Okay, this isn't exactly "Unity Scripting" related and more of a C# relation; However it is be used in my current Unity3D Project, I'll also state that if you have no experience with programming or you're worried about getting started because you think it may be difficult and confusing, you should turn back now.

    ---------

    Alright, so basically I've been converting the DataInputStream and DataOutputStream classes from Java's networking package. The reason for this is because I have used java servers for quite awhile and already have a networking structure that I feel is quite capable and I wanted to use it for my game in Unity3D. The only problem lies in the ReadUTF method that rests in the DataInputStream class.

    The DataInputStream and DataOutputStream classes in Java use a custom UTF-8 encoding format when writing a string through a network stream, because of this you have to construct it yourself from the data that's sent, I'm not very experienced with C# and I believe the problem is that I'm using the BinaryReader class incorrectly, or misunderstanding something.

    Considering pasting this code here turns into Japanese, I'll be posting the code on paste-bin.

    ReadUTF Method: http://pastebin.com/jKLK9ntw
    ReadUnsignedShort method: http://pastebin.com/kk6FJ8Gf
    ReadFully Method: http://pastebin.com/dHsvnbhf

    For those of you who are curious as to how to String is constructed, here's a link to the method that builds the string: http://pastebin.com/hPnJwFX4

    The ClientOutput is an instance of BinaryWriter.


    Help me out guys, I really need to figure out how to read the strings, I've got the C# Client sending them to the server properly, however I can't get it to read them for some reason?
     
    Last edited: Oct 17, 2014
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Looking at google searches of people describing the modified format from UTF-8.

    Yeah... I'd just outright avoid using that for streaming. Personally.
     
  4. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376

    I managed to get it, here's a link to my repository if anyone is interested. https://bitbucket.org/CTucker1327/c-datastreams/
     
  5. ripper98

    ripper98

    Joined:
    Apr 22, 2015
    Posts:
    7
    please reupload the file, thanks:)
     
  6. sanketrane

    sanketrane

    Joined:
    Mar 27, 2021
    Posts:
    1