Amigos, tengo un dilema. Encontré una clase la cual controla un proxy.
Lo que no logro saber es como usarla para enviar paquetes.
La sentencia sería PortListener.SendToServer(" BINARIO ");
Este dato me da el proxy como ejemplo de "dato" Message(3:2:05:01)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net.Sockets;
namespace AOPacketListener
{
class Program
{
static void Main(string[] args)
{
ExecuteCommand("X-Runner.cmd");
StartListening();
}
private static void ExecuteCommand(string command)
{
System.Diagnostics.ProcessStartInfo processInfo;
System.Diagnostics.Process process;
processInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = System.Diagnostics.Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
int exitCode = process.ExitCode;
Console.WriteLine("Error: " + error);
Console.WriteLine("Exit-Code: " + exitCode);
process.Close();
}
public static void StartListening()
{
try
{
//AllocConsole();
TcpListener r_Server = new TcpListener(IPAddress.Loopback, 1337);
r_Server.Start();
bool isActive = true;
while (isActive)
{
Console.Write("Waiting for a connection... ");
//!
//! Wait for the client to connect.
//!
TcpClient r_Client = r_Server.AcceptTcpClient();
Console.WriteLine("Connected!");
//!
//! Get the stream.
//!
NetworkStream r_IOStream = r_Client.GetStream();
//!
//! Keep executing while the client is active.
//!
while (r_Client.Connected)
{
using (var r_IOReader = new BinaryReader(r_IOStream))
{
while (r_IOReader != null)
{
if (r_IOStream.DataAvailable)
{
Byte r_ID = r_IOReader.ReadByte();
UInt16 r_Length = ReverseBytes(r_IOReader.ReadUInt16());
byte[] r_Message = r_IOReader.ReadBytes(r_Length);
Console.WriteLine("Message({0}:{1}:{2})", r_ID, r_Length, BitConverter.ToString(r_Message).Replace("-", ":"));
if (r_ID == 0x03)
Reply(r_IOStream, r_Message, r_Length, r_ID);
}
}
}
}
//!
//! Shutdown and end connection.
//!
r_Client.Close();
isActive = false;
}
}
catch (Exception exception)
{
Console.WriteLine("SocketException: {0}", exception);
}
}
public static UInt16 ReverseBytes(UInt16 value)
{
return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);
}
public static void SendToClient(NetworkStream pStream, String pMessage)
{
if (!string.IsNullOrEmpty(pMessage))
Send(pStream, pMessage, true);
}
public static void SendToServer(NetworkStream pStream, String pMessage)
{
if (!string.IsNullOrEmpty(pMessage))
Send(pStream, pMessage, false);
}
public static void Send(NetworkStream pStream, String pMessage, bool pForClient)
{
int len = (pMessage.Length + 1) * 2;
Byte[] r_Reply = Enumerable.Repeat((byte)0x00, 0x03 + len).ToArray();
r_Reply[0x00] = pForClient ? (byte)0x01 : (byte)0x02;
r_Reply[0x01] = (Byte)((len >> 0x08) & 0xFF);
r_Reply[0x02] = (Byte)(len & 0xFF);
System.Array.Copy(Encoding.Unicode.GetBytes(pMessage), 0, r_Reply, 0x03, len - 0x02);
pStream.Write(r_Reply, 0, r_Reply.Length);
pStream.Flush();
}
public static void Reply(NetworkStream pStream, byte[] pData, UInt16 pLength, Byte pID)
{
Byte[] r_Reply = Enumerable.Repeat((byte)0x00, 0x03 + pLength).ToArray();
r_Reply[0x00] = pID;
r_Reply[0x01] = (Byte)((pLength >> 0x08) & 0xFF);
r_Reply[0x02] = (Byte)(pLength & 0xFF);
System.Array.Copy(pData, 0, r_Reply, 0x03, pLength);
pStream.Write(r_Reply, 0, r_Reply.Length);
pStream.Flush();
}
}
}