Esempio n. 1
0
 /// <summary>
 /// Gets the server worker.
 /// </summary>
 /// <param name="input">The input.</param>
 /// <remarks></remarks>
 static void GetServerWorker(object input)
 {
     GetServerWorkerData data = (GetServerWorkerData)input;
     MinecraftServer response;
     try
     {
         response = GetServer(data.Hostname);
     }
     catch
     {
         response = new MinecraftServer();
         response.IsOnline = false;
     }
     if (data.AsyncCallback != null)
         data.AsyncCallback(response, data.AsyncState);
 }
Esempio n. 2
0
 private static MultiplayerClient Connect(string username, string password, string server)
 {
     p.UserName = username;
     p.Password = password;
     MultiplayerClient mpc = new MultiplayerClient();
     MinecraftServer mcs = new MinecraftServer();
     mcs.Hostname = server;
     mpc.Connect(mcs);
     mpc.LogIn(p);
     Server = mcs;
     return mpc;
 }
Esempio n. 3
0
        /// <summary>
        /// Gets server status based on the hostname.
        /// The hostname may include a port, like
        /// "127.0.0.1:port"
        /// </summary>
        /// <param name="Hostname">The hostname.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static MinecraftServer GetServer(string Hostname)
        {
            MinecraftServer server = new MinecraftServer();
            int port = 25565;

            if (Hostname.StartsWith("http://"))
                Hostname = Hostname.Substring("http://".Length);
            if (Hostname.Contains(":"))
            {
                port = int.Parse(Hostname.Substring(Hostname.IndexOf(":") + 1));
                Hostname = Hostname.Remove(Hostname.IndexOf(":"));
            }
            server.Port = port;
            server.IP = Hostname;

            byte[] resp;

            DateTime startTime = DateTime.Now;
            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.ReceiveTimeout = 1000;
                socket.SendTimeout = 1000;
                socket.Connect(Hostname, port);
                socket.Send(new byte[] { (byte)0xFE });
                resp = new byte[socket.ReceiveBufferSize];
                socket.Receive(resp);
                socket.Disconnect(false);
            }
            catch
            {
                server.IsOnline = false;
                return server;
            }
            server.IsOnline = true;
            TimeSpan ping = DateTime.Now - startTime;
            server.PingTime = (int)ping.TotalMilliseconds;

            if (resp[0] != (byte)0xFF) // Used to acknowledge
                return server;

            string Description = "";
            string numPlayers = "";
            string maxPlayers = "";
            // Read out description
            int i;
            for (i = 4; i < resp.Length; i += 2)
            {
                if (resp[i] == 0xA7) // End of string
                    break;
                Description += (char)resp[i];
            }
            i += 2;
            // Read out number of players
            for (; i < resp.Length; i += 2)
            {
                if (resp[i] == 0xA7) // End of string
                    break;
                numPlayers += (char)resp[i];
            }
            i += 2;
            // Read out max players
            for (; i < resp.Length; i += 2)
            {
                if (resp[i] == 0x00) // End of data
                    break;
                maxPlayers += (char)resp[i];
            }
            server.MotD = Description;
            server.MaxPlayers = int.Parse(maxPlayers);
            server.ConnectedPlayers = int.Parse(numPlayers);

            return server;
        }
Esempio n. 4
0
 /// <summary>
 /// Connects to the specified server (does not log in)
 /// </summary>
 /// <param name="server">The server.</param>
 /// <remarks></remarks>
 public void Connect(MinecraftServer server)
 {
     if (CurrentServer != null)
         throw new Exception("You cannot connect to another server before disconnecting.");
     try
     {
         CurrentServer = server;
         netSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         netSocket.Connect(server.IP, server.Port);
         netStream = new NetworkStream(netSocket);
     }
     catch (Exception e)
     {
         try
         {
             Disconnect();
         }
         catch { }
         finally
         {
             throw e;
         }
     }
 }