1

I'm trying to show the message in a string that was sent by the client. I coded a simple client application and then coded a server with DotNetty. The client app is just for testing.

Client:

var ipAddress = "127.0.0.1";
var port = 1232;

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try
{
    socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), port));
}
catch (SocketException se)
{

}

byte[] forwardMessage = Encoding.Default.GetBytes("lol");
socket.Send(forwardMessage);

Console.ReadLine();

Server decoder:

internal class Decoder : MessageToMessageDecoder<IByteBuffer>
{
    protected override async void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
    {
        Console.WriteLine("Socket says: " + input.ToString());
    }
}

When sending a message from the client to the server I get this.

Socket says: InstrumentedUnpooledHeapByteBuffer(ridx: 0, widx: 3, cap: 1024)
seegal
  • 75
  • 2

1 Answers1

3

input is a IByteBuffer, which is an abstraction around where that data is currently stored - in this case apparently a InstrumentedUnpooledHeapByteBuffer, but that doesn't really matter.

You can get the original data via a wide range of methods on IByteBuffer, but it looks like the simplest for you is simply:

var s = input.GetString(input.ReaderIndex, input.WriterIndex, yourEncoding);

(or something similar with a 0 and a 3 from one of input's properties)

where yourEncoding is the text encoding to use. Note that at the caller you used Encoding.Default, but this is a terrible choice, as this is machine-specific; I could guess what it might be for you, but I genuinely can't know what your Encoding.Default is. A better choice would be something like Encoding.UTF8 (at both ends).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • `input` doesn't have a method `GetString` so your answer is invalid. – seegal Apr 05 '18 at 13:04
  • @seegal https://github.com/Azure/DotNetty/blob/33b95cfe615bc000bebb247504473ed9d34d0938/src/DotNetty.Buffers/IByteBuffer.cs#L496 - if that isn't the version you're using, then you'll have to use whatever *is* available – Marc Gravell Apr 05 '18 at 15:27