Servus Leute,
braucht man bei einer Socket-Verbindung (mit Hilfe von TcpClient) immer Admin-Berechtigungen oder geht es auch ohne? Wenns ohne geht, was muss ich dann machen?
Mein Pinger lief jetzt zumindest nur unter Admin. Habe jetzt aber eine andere Version hier im Forum gefunden die ohne Adminrechte funktionieren sollte.
Danke
Steffen
Hi,
echt? Wo steht es geschrieben?
Ich benutze den Port 10001, sollte also funktionieren 👍
hatte zu schnell geantwortet. hatte da wohl linux im kopf.
was kommt den für eine fehlermeldung? access denied oder wie?
Error : Der Zugriff auf einen Socket war aufgrund der Zugriffsrechte des Sockets unzulÀssig
Stack Trace : bei System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
bei System.Net.Sockets.Socket.SendTo(Byte[] buffer, EndPoint remoteEP)
Wie gesagt dass war beim Pingen, dieser Code hat die Exception ausgelöst (socket.SentTo in der Send-Methode):
public class Pinger
{
protected Socket socket;
protected bool isOpen;
protected ManualResetEvent readComplete;
protected byte lastSequenceNr = 0;
protected byte[] pingCommand;
protected byte[] pingResult;
/// <summary>
/// Initializes a new instance of the <see cref="Pinger"/> class.
/// </summary>
public Pinger()
{
pingCommand = new byte[8];
pingCommand[0] = 8; // Type
pingCommand[1] = 0; // Subtype
pingCommand[2] = 0; // Checksum
pingCommand[3] = 0;
pingCommand[4] = 1; // Identifier
pingCommand[5] = 0;
pingCommand[6] = 0; // Sequence number
pingCommand[7] = 0;
pingResult = new byte[pingCommand.Length + 1000];
}
/// <summary>
/// Opens this instance.
/// </summary>
public void Open()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
isOpen = true;
readComplete = new ManualResetEvent(false);
}
/// <summary>
/// Closes this instance.
/// </summary>
public void Close()
{
isOpen = false;
socket.Close();
readComplete.Close();
}
/// <summary>
/// Sends the Ping to the specified address.
/// </summary>
/// <param name="address">The address.</param>
/// <param name="timeout">The timeout.</param>
/// <returns></returns>
public TimeSpan Send(string address, TimeSpan timeout)
{
while (socket.Available > 0)
socket.Receive(pingResult, Math.Min(socket.Available, pingResult.Length), SocketFlags.None);
readComplete.Reset();
DateTime timeSend = DateTime.Now;
pingCommand[6] = lastSequenceNr++;
SetChecksum(pingCommand);
int iSend = socket.SendTo(pingCommand, new IPEndPoint(IPAddress.Parse(address), 0));
socket.BeginReceive(pingResult, 0, pingResult.Length, SocketFlags.None, new AsyncCallback(CallBack), null);
if (readComplete.WaitOne(timeout, false))
{
if ((pingResult[20] == 0) && (pingCommand[4] == pingResult[24]) && (pingCommand[5] == pingResult[25]) &&
(pingCommand[6] == pingResult[26]) && (pingCommand[7] == pingResult[27]))
return DateTime.Now.Subtract(timeSend);
}
return TimeSpan.MaxValue;
}
/// <summary>
/// CallBack.
/// </summary>
/// <param name="result">The result.</param>
protected void CallBack(IAsyncResult result)
{
if (isOpen)
{
try
{
socket.EndReceive(result);
}
catch (Exception)
{
}
readComplete.Set();
}
}
/// <summary>
/// Sets the checksum.
/// </summary>
/// <param name="tel">The tel.</param>
private void SetChecksum(byte[] tel)
{
tel[2] = 0;
tel[3] = 0;
uint cs = 0;
for (int i = 0; i < pingCommand.Length; i = i + 2)
cs += BitConverter.ToUInt16(pingCommand, i);
cs = ~((cs & 0xffffu) + (cs >> 16));
tel[2] = (byte)cs;
tel[3] = (byte)(cs >> 8);
}
}
Nun habe ich den Code von dem Beitrag übernommen:
PING einer hosts ohne admin rechte
Ich warte gerade auf eine Rückmeldung vom Kollegen ob es funktioniert.
SocketType.Raw ist für Administratoren vorbehalten, und seit WinXP SP2 auch mit Limitierungen belegt.
We have removed support for TCP sends over RAW sockets in SP2.
We surveyed applications and found the only apps using this on XP were people writing attack tools.
Die normale Kommunikation über TcpClient funktioniert anscheinend. Es lag nur an der Ping-Klasse, nach der Modifikation läuft es nun auch unter "Nicht-Admin".