# Programming TCP-protocol Server (MineCraft)

**URL:** <https://www.ruby-forum.com/t/programming-tcp-protocol-server-minecraft/191493>\
**Category:** Ruby\
**Created:** [July 13, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/programming-tcp-protocol-server-minecraft/191493 "2010-07-13T14:18:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hikayu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f0a364/32.png) [@hikayu](https://www.ruby-forum.com/u/hikayu)\
**Post date:** [July 13, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/programming-tcp-protocol-server-minecraft/191493/1 "2010-07-13T14:18:07Z")

</div>

There’s this game called Minecraft ([http://www.minecraft.net/](http://www.minecraft.net/))

Read this site first:

> **[Development resources](https://minecraft.fandom.com/wiki/Development_resources)**
>
> Minecraft is written in Java and C++, and although the class files are obfuscated, the community has figured out most of the inner workings of Minecraft. Notch himself gave notes on a common format used by Minecraft, the NBT format. From this, the...

What I want to do is to create a custom Minecraft Server in Ruby. There  
are several in other languages:

> **[Server software](https://minecraft.fandom.com/wiki/Server_software)**

But I’d like to make it in mine.

Basically all the server has to do is to

(Initialization):  
Send it’s name, max players, ip and port to the server  
([www.minecraft.net/heartbeat.jsp](http://www.minecraft.net/heartbeat.jsp))

E.g:  
(port=25565&max=32&name=name&public=True&version=7&salt=wo6kVAHjxoJcInKx&users=0)

And then receive each client and… do stuff \<\<- I can figure that out  
on my own.

But the crucial thing I DON’T know is how to SEND TCP packets and DECODE  
them after receiving them. May someone give me a pointer?

---

<div class="post-metadata">

**Author:** ![hikayu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f0a364/32.png) [@hikayu](https://www.ruby-forum.com/u/hikayu)\
**Post date:** [July 13, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/programming-tcp-protocol-server-minecraft/191493/2 "2010-07-13T23:01:35Z")

</div>

Zsdfhdfgasdf Gsfgsdgsdgsd wrote:

> What I want to do is to create a custom Minecraft Server in Ruby.

If your server wants to accept incoming TCP connections from clients,  
then you can use TCPServer and Socket#accept. gserver.rb in the standard  
library wraps this all up for you. Read the source code for  
documentation. It’s likely installed in your system somewhere like  
/usr/lib/ruby/1.8/gserver.rb

See also the chapter “Network and Web Libraries” in Programming Ruby:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

> But the crucial thing I DON’T know is how to SEND TCP packets and DECODE  
> them after receiving them. May someone give me a pointer?

Once you’ve got an open socket, you can just use gets() or read(n) to  
get data (the first reads up to a newline, the second reads n bytes),  
and puts() or write() to send data.

Note that each client will be handled in a separate thread, which means  
you may need to be careful about thread-safety if you are working with a  
shared data structure.

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![hikayu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f0a364/32.png) [@hikayu](https://www.ruby-forum.com/u/hikayu)\
**Post date:** [July 14, 2010, 10:19am UTC](https://www.ruby-forum.com/t/programming-tcp-protocol-server-minecraft/191493/3 "2010-07-14T10:19:09Z")

</div>

> Brian.

Thank you alot. I have begun writing the pseudocode and such.
