Skip to content

Instantly share code, notes, and snippets.

@vittodevit
Last active October 7, 2020 18:11
Show Gist options
  • Save vittodevit/80dff8cbb375d621746e33821fa513fa to your computer and use it in GitHub Desktop.
Save vittodevit/80dff8cbb375d621746e33821fa513fa to your computer and use it in GitHub Desktop.
Example usage for Csmcrc library
/*THIS EXAMPLE CONNECTS TO A SERVER, SENDS A COMMAND AND DISCONNECTS*/
using System;
using csmcrc;
namespace CsmcrcExample
{
class CsmcrcExample
{
static void Main(string[] args)
{
Csmcrc minecraftserver = new Csmcrc; //initalizes an instance of Csmcrc
/* connects to the server and stores the result into connectionres
when connecting you should always a try-catch block to prevent exceptions (ex. Wrong ip or port makes the socket fail)*/
bool connectionres = minecraftserver.init("192.168.1.2", 25575, "password");
/* if connectionres is false the password is wrong*/
if(connectionres == false){
Console.WriteLine("Wrong password! Press a key to continue...");
Console.ReadKey(); //wait for a key press
System.Environment.Exit(0); //close the app
}else{
Console.WriteLine("Connected: " + minecraftserver.isConnected()); //using the isConnected method to print out the state of the connection
Console.WriteLine("IP: " + minecraftserver.getAddress()); //getAddress returns the ip address
Console.WriteLine("PORT: " + minecraftserver.getPort()); //getPort returns the ip port
}
//when sending commands you should always use a try-catch block to prevent exceptions (ex. You are not connected so the send fails)
string response = minecraftserver.send("time set 0"); //send to the minecraft server the command 'time set 0' and stores the response
Console.WriteLine("Response: " + response); //printing out the server response
//disconnecting
minecraftserver.disconnect();
//wait for a key press then close the app
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment