Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- namespace Minecraft_Client
- {
- class Minecraft
- {
- public void Update(TcpClient c)
- {
- byte[] data = new byte[5];
- data[0] = (byte)0;
- byte[] id = new byte[1];
- c.Client.Receive(id, 0, 1, SocketFlags.None);
- if (id[0] == (byte)0)
- {
- //Ping/alive package (send back!)
- Console.WriteLine("Client receiver keep-alive-package.");
- c.Client.Receive(data, 1, 4, SocketFlags.None);
- int kai = BitConverter.ToInt16(data, 1);
- Console.WriteLine("Sending keep-alive-package back server (KAI:" + kai.ToString() + ")");
- c.Client.Send(data);
- }
- else if (id[0] == (byte)3)
- {
- short len;
- byte[] buffer = new byte[2];
- c.Client.Receive(buffer, 1, 3, SocketFlags.None);
- len = (short)BitConverter.ToInt16(buffer, 0);
- Console.WriteLine("Received chatmsg (len:" + len.ToString());
- }
- }
- public bool Handshake(string username,TcpClient c)
- {
- //array
- byte[] data = new byte[3 + (username.Length *2)];
- //packet id
- data[0] = (byte)2;
- //short len
- byte[] sh = new byte[2];
- sh = BitConverter.GetBytes((short)username.Length);
- Array.Reverse(sh);
- sh.CopyTo(data, 1);
- //username
- byte[] name = Encoding.Unicode.GetBytes(username);
- name.CopyTo(data, 3);
- c.Client.Send(data);
- byte[] pid = new byte[1];
- c.Client.Receive(pid, 0, 1, SocketFlags.None);
- if (pid[0] == (byte)2)
- {
- return true;
- }
- else return false;
- }
- public bool RequestLogin(string username,TcpClient c)
- {
- //array
- byte id = (byte)1;
- byte[] protecol = new byte[4]; protecol = BitConverter.GetBytes((int)22); Array.Reverse(protecol);//v1.0
- byte[] len = new byte[2]; len = BitConverter.GetBytes((username.Length * 2)); Array.Reverse(len);
- byte[] usrname = new byte[username.Length * 2]; usrname = Encoding.Unicode.GetBytes(username);
- byte[] lon = new byte[8]; lon = BitConverter.GetBytes((long)0); Array.Reverse(lon);
- byte[] intt = new byte[4]; intt = BitConverter.GetBytes((int)0); Array.Reverse(intt);
- byte empty = (byte)0;
- //Creating byte array
- byte[] data = new byte[23 + (username.Length * 2)];
- //adding id
- data[0] = id;
- //adding protecol
- protecol.CopyTo(data, 1);
- //adding username length short
- len.CopyTo(data, 5);
- //adding username
- usrname.CopyTo(data, 7);
- //new index calculation
- int index = (7 + (username.Length * 2));
- //adding long
- lon.CopyTo(data, index);
- index = index + 8;
- //adding int
- intt.CopyTo(data, index);
- index = index + 4;
- //adding bytes
- data[index] = empty;
- data[index +1] = empty;
- data[index +2] = empty;
- data[index +3] = empty;
- c.Client.Send(data);
- try
- {
- byte[] pid = new byte[1];
- try
- {
- if (c.Connected)
- {
- c.Client.Receive(pid, 0, 1, SocketFlags.None);
- }
- else return false; //kick
- }
- catch
- {
- //server disconnected
- return false;
- }
- //Got package back ?
- return true;
- }
- catch
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Server closed connection.");
- while (true) { }
- }
- }
- public void SendChatMessage(string message, TcpClient c)
- {
- byte[] chat = new byte[3 + (message.Length * 2)];
- chat[0] = (byte)3;
- byte[] msglen = new byte[2];
- msglen = BitConverter.GetBytes(message.Length);
- Array.Reverse(msglen);
- msglen.CopyTo(chat, 1);
- Encoding.Unicode.GetBytes(message).CopyTo(chat, 3);
- c.Client.Send(chat);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement