Permalink
Browse files

Corrected mis-placed else for enter IP section - for when a setting i…

…s not set etc.

Changed Client to local variable for now, prevents an error caused by public variable and disconnections.
Added a brief sleep time to the code to allow for the tiny delay server side and actually getting the data sent back.
Remove the starting phrase on the protocol (????print)
Added while loop to cope with multiple packets  (currently fails to work properly not sure why, can't test without a full server.
Set the buffer size to precisely what we are going to be grabbing from the connection.
  • Loading branch information...
1 parent aa51e6f commit 99f6966dee7301042628e72c09a0a627417413b0 @jaddie committed Oct 10, 2010
Showing with 25 additions and 24 deletions.
  1. +25 −24 Program.cs
View
@@ -1,10 +1,11 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Settings = Cod4RconConsoleTool.Properties.Settings;
+using System.Threading;
namespace Cod4RconConsoleTool
{
class Program
@@ -13,7 +14,6 @@ class Program
public static int Port;
public static string RconPassword;
public static string RconCommand;
- public static Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
static void Main(string[] args)
{
try
@@ -48,19 +48,19 @@ static void Main(string[] args)
}
}
- else
- {
- Console.WriteLine("Enter IP");
- IP = IPAddress.Parse(Console.ReadLine());
- Console.WriteLine("Enter Port");
- Port = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter RCon Password");
- RconPassword = Console.ReadLine();
- Console.WriteLine("Enter RCon Command To Run");
- RconCommand = Console.ReadLine();
- Console.WriteLine("Connecting..");
- Console.WriteLine(Rcon(IP, Port, RconPassword, RconCommand));
- }
+ }
+ else
+ {
+ Console.WriteLine("Enter IP");
+ IP = IPAddress.Parse(Console.ReadLine());
+ Console.WriteLine("Enter Port");
+ Port = int.Parse(Console.ReadLine());
+ Console.WriteLine("Enter RCon Password");
+ RconPassword = Console.ReadLine();
+ Console.WriteLine("Enter RCon Command To Run");
+ RconCommand = Console.ReadLine();
+ Console.WriteLine("Connecting..");
+ Console.WriteLine(Rcon(IP, Port, RconPassword, RconCommand));
}
Console.WriteLine("Command Executed, you are now at an rcon terminal\n(in english you can now just type commands to be sent directly do not put rcon at the start)\nIt will be added automaticlly for you.\nTo close the program type exitrcon");
var input = "";
@@ -101,12 +101,9 @@ static void Main(string[] args)
public static string Rcon(IPAddress gameServerIP, int gameServerPort, string password, string rconCommand)
{
-
- // Connect to the server
- if(!Client.Connected)
+ Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Client.Connect(gameServerIP, gameServerPort);
-
string command = "rcon " + password + " " + rconCommand;
Byte[] bufferTemp = Encoding.ASCII.GetBytes(command);
Byte[] bufferSend = new Byte[bufferTemp.Length + 5];
@@ -130,11 +127,15 @@ public static string Rcon(IPAddress gameServerIP, int gameServerPort, string pas
//IPEndPoint remoteIpEndPoint IPEndPoint(IPAddress.Any, 0);
Client.Send(bufferSend, SocketFlags.None);
-
- // Use a large recieve buffer to make sure we can take the response
- Byte[] bufferRec = new Byte[64999];
- Client.Receive(bufferRec);
- String result = Encoding.ASCII.GetString(bufferRec).Replace("\0", ""); // Remove whitespace
+ string result = "";
+ Thread.Sleep(50);
+ while (Client.Available > 0)
+ {
+ // Use a large recieve buffer to make sure we can take the response
+ Byte[] bufferRec = new Byte[Client.Available];
+ Client.Receive(bufferRec);
+ result = result + Encoding.ASCII.GetString(bufferRec).Replace("????print","").Replace("\0", ""); // Remove whitespace
+ }
return result;
}

0 comments on commit 99f6966

Please sign in to comment.