När jag kör denna kod får jag meddelandet: Problem med egentillverkad ping
"A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself. "
Men bufferten är inte för liten, har jag fel i koden eller fel rättigheter?
Mina ögon blöder, snälla hjälp!
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
namespace Ifd
{
/// <summary>
/// Summary description for ping.
/// </summary>
public class ping
{
int pingBytes = 32;
Socket s;
IPHostEntry pingTarget, pingSource;
IPEndPoint ipServer,ipLocal;
EndPoint epServer,epLocal;
Random randId = new Random();
ushort id = (ushort)1;//randId.Next(ushort.MaxValue);
ushort seqNr=120;
byte [] sendBuf;
byte [] recBuf;
public ping(String server)
{
pingTarget = Dns.GetHostByName(server);
ipServer = new IPEndPoint(pingTarget.AddressList[0],7);
epServer = (EndPoint)(ipServer);
pingSource = Dns.GetHostByName(Dns.GetHostName());
ipLocal = new IPEndPoint(pingSource.AddressList[0],7);
epLocal = (EndPoint)(ipLocal);
s = new Socket(AddressFamily.InterNetwork,
socketType.Raw, ProtocolType.Icmp);
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout,1000);
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,1000);
ICMP_pack packet = new ICMP_pack();
fillICMP(packet, id, seqNr, pingBytes);
sendBuf = new byte[pingBytes+8];
recBuf = new byte[65536];//Kan ej vara för liten
classToByte(packet,sendBuf);
packet.checksum = cSum(sendBuf);
classToByte(packet,sendBuf);
}
public int send()
{
return s.SendTo(sendBuf,pingBytes+8,0,epServer);
}
public int rec()
{
int t =s.ReceiveFrom(recBuf,pingBytes+8,0,ref epLocal);
s.Close();
return t;
}
public byte[] getAr()
{
return sendBuf;
}
private void classToByte(ICMP_pack p, byte[] b)
{
b[0] = p.Type;
b[1] = p.SubCode;
byte [] cs = BitConverter.GetBytes(p.checksum);
byte [] id = BitConverter.GetBytes(p.id);
byte [] sn = BitConverter.GetBytes(p.seqNr);
for(int i = 0 ; i < p.Data.Length ; i++)
p.Data[i]=(byte)'1';
Array.Copy(cs,0,b,2*1,cs.Length);
Array.Copy(id,0,b,2*1+cs.Length,id.Length);
Array.Copy(sn,0,b,2*1+cs.Length+id.Length,
sn.Length);
Array.Copy(p.Data,0,b,
2*1+cs.Length+id.Length+sn.Length,
p.Data.Length);
}
private void fillICMP(ICMP_pack p, ushort id, ushort seqNr,
int pingBytes)
{
p.id=id;
p.seqNr=seqNr;
p.Data = new byte[32];
}
private ushort cSum(byte[] arBytes)
{
ulong chksum = 0;
int nSize = arBytes.Length;
int i = 0;
while(nSize > 1)
{
chksum += (ulong)((((ushort)arBytes
[i+1]) << 8) + (ushort)arBytes[i]);
nSize--;
nSize--;
i++;
i++;
}
if(nSize > 0)
{
chksum += arBytes[i];
}
chksum = (chksum >> 16) + (chksum & 0xffff);
chksum += (chksum >> 16);
ushort result = (ushort)(~chksum);
return result;
}
}
public class ICMP_pack
{
public byte Type = 8;
public byte SubCode = 0;
public ushort checksum;
public ushort id;
public ushort seqNr;
public byte[] Data;
}
}