Chris
Chris

Reputation: 1363

Reading Strings from Streams with C#

Right now I'm working on a project that requires my application (C#) to connect to a minecraft server and fetch information. I know how to do this with Java but not with C#. I have started to write a simple packet listener and such but I just want to find a way to do something like this in C#:

socket = new Socket();
socket.setSoTimeout(3000);
socket.setTcpNoDelay(true);
socket.setTrafficClass(18);
socket.connect(new InetSocketAddress(s1, j), 3000);
datainputstream = new DataInputStream(socket.getInputStream());
dataoutputstream = new DataOutputStream(socket.getOutputStream());
dataoutputstream.write(254);
String s4 = Packet.readString(datainputstream, 256);

The only part of this that I don't understand is how to read the string. This is the method Packet.readString()

short word0 = par0DataInputStream.readShort();    
StringBuilder stringbuilder = new StringBuilder();

for (int i = 0; i < word0; i++)
{
    stringbuilder.append(par0DataInputStream.readChar());
}

return stringbuilder.toString();

I know there is a method GetStream so could I use StreamReader and StreamWriter to write and read the strings?

Upvotes: 2

Views: 240

Answers (1)

nyxthulhu
nyxthulhu

Reputation: 9762

Have a peek at LibMinecraft why do all the heavy lifting when its already been done for you?

Using this library you can connect to and create your own server.

Upvotes: 2

Related Questions