在发送自己的IP包时出错~~~~~~

sy_wcc 2005-07-11 03:05:38
在发送自己的一个IP包时,出现发下错误提示:

未处理的“System.Net.Sockets.SocketException”类型的异常出现在 system.dll 中。

其他信息: 一个封锁操作被对 WSACancelBlockingCall 的调用中断


有哪位大侠知道是什么原因引起的????
...全文
280 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sy_wcc 2005-08-24
  • 打赏
  • 举报
回复
现在已经解决了组成伪数据包的问题,但是有几个问题:

1 在数据包中"检查和"域的值能为0吗?如果为0,那目的主机会接收到该数据包吗?

2 如何知道目的主机是否已接收到该数据包?

等待中.......
sy_wcc 2005-08-23
  • 打赏
  • 举报
回复
有人会吗?????
sy_wcc 2005-07-28
  • 打赏
  • 举报
回复
upupupupup
kingnew 2005-07-15
  • 打赏
  • 举报
回复
up
sy_wcc 2005-07-15
  • 打赏
  • 举报
回复
可以的啊,我再试试吧
xiaopai20 2005-07-11
  • 打赏
  • 举报
回复
C#好象不能直接伪IP包, 用底层的语言能实现~
sy_wcc 2005-07-11
  • 打赏
  • 举报
回复
不过还是要感谢你kmblack(黑色)
sy_wcc 2005-07-11
  • 打赏
  • 举报
回复
我是想做一个伪IP包,然后发送到目的主机老大
kmblack 2005-07-11
  • 打赏
  • 举报
回复
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{

//客户端
IPAddress IP = IPAddress.Parse("192.168.0.105");
IPEndPoint ip = new IPEndPoint(IP,6000);
using( Socket socket = new Socket( ip.AddressFamily,SocketType.Stream,ProtocolType.Tcp ) )
{
if( !socket.Connected )
{
socket.Connect(ip);
}

byte[] SendMsg = System.Text.ASCIIEncoding.UTF8.GetBytes("Hello!");
socket.Send(SendMsg);
byte[] DataReceive = new byte[2048];
socket.Receive(DataReceive);
string msge = System.Text.ASCIIEncoding.UTF8.GetString(DataReceive);
Console.Write( msge.Trim() );


socket.Close();
Console.Write( "连接已断开" );

}// end using

}
}
kmblack 2005-07-11
  • 打赏
  • 举报
回复

//服务器端,只是一个简单的示例
public class AlarmDriver
{
private bool IsStop = false;
public static void Main (string[] args)
{
new AlarmDriver();
}

private AlarmDriver()
{
IPHostEntry ipHostInfo = Dns.Resolve( Dns.GetHostName() );
IPAddress ipAddress = ipHostInfo.AddressList[0];
int port = 6000;
TcpListener m_Listener = new TcpListener( ipAddress,port );
StartServer( m_Listener );
}

private void StartServer( TcpListener m_Listener )
{
m_Listener.Start();
Console.Write ("开始侦听......\r\n");
while ( !IsStop )
{
Socket mySocket = m_Listener.AcceptSocket();

Console.Write ( "Connection:" + mySocket.RemoteEndPoint.ToString() + "\r\n");
ReceiveData receive = new ReceiveData( mySocket );
Thread thread = new Thread( new System.Threading.ThreadStart( receive.SaveData ));
thread.Start();
}// end while
}// end void

public static void print( string msg )
{
Console.Write( msg + "\r\n");
}
} // end class

public class ReceiveData
{
/// <summary>
/// 设置数据缓冲区
/// </summary>
byte[] DataReceive = new byte[1024];
private Socket mySocket;
public ReceiveData( Socket mySocket )
{
this.mySocket = mySocket;
}// end

public void SaveData()
{
//处理数据
mySocket.Receive( DataReceive );
string msg = System.Text.ASCIIEncoding.UTF8.GetString(DataReceive);
AlarmDriver.print( msg.Trim() );
byte[] SendMsg = System.Text.ASCIIEncoding.UTF8.GetBytes("收到发送的消息");
mySocket.Send(SendMsg);
mySocket.Close();
}// end void
}// end class
kmblack 2005-07-11
  • 打赏
  • 举报
回复
汗,.net Socket编程用指针!待会我给个简单示例吧.服务器和客户机
sy_wcc 2005-07-11
  • 打赏
  • 举报
回复
但在s1.SendTo(buff,buff.Length,SocketFlags.None,iep);出现以上错误
sy_wcc 2005-07-11
  • 打赏
  • 举报
回复
谢谢你 kmblack(黑色),我的代码:

string aip1=t1.Text.Trim();
string aip2=t2.Text.Trim();
string aip3=t3.Text.Trim();
string aip4=t4.Text.Trim();

RawSocket s=new RawSocket();

uint ip1=s.ParseIPAddress(aip1);
uint ip2=s.ParseIPAddress(aip2);
uint ip3=s.ParseIPAddress(aip3);
uint ip4=s.ParseIPAddress(aip4);

byte[] buff=new byte[100];
Socket s1=new Socket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.Raw);
s1.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);

//生成伪IP头
fixed(byte * buff_a=buff)
{
IPHeader * ip_new = (IPHeader * ) buff_a;
ICMP_re *icmp=(ICMP_re *)(buff_a+20);
IPHeader *ip_old=(IPHeader *)(buff_a+28);
UDPhdr *udp=(UDPhdr *)(buff_a+48);

s.Make_udp_header(udp);
s.Make_ip_header(ip_old,ip2,ip4,(byte)(17),100);
s.Make_icmp5_header(icmp,ip3);
s.Make_ip_header(ip_new,ip1,ip2,(byte)(1),56);
}

IPEndPoint iep=new IPEndPoint(IPAddress.Parse(aip2),0);
EndPoint ep=(EndPoint) iep;
s1.SendTo(buff,buff.Length,SocketFlags.None,iep);

kmblack 2005-07-11
  • 打赏
  • 举报
回复
给你个例子参考一下吧
public class AlarmDriver
{
private bool IsStop = false;
public static void Main (string[] args)
{
new AlarmDriver();
}

private AlarmDriver()
{
IPHostEntry ipHostInfo = Dns.Resolve( Dns.GetHostName() );
IPAddress ipAddress = ipHostInfo.AddressList[0];
int port = 6000;
TcpListener m_Listener = new TcpListener( ipAddress,port );
StartServer( m_Listener );
}

private void StartServer( TcpListener m_Listener )
{
m_Listener.Start();
Console.Write ("开始侦听......\r\n");
while ( !IsStop )
{
Socket mySocket = m_Listener.AcceptSocket();
Console.Write ( "Connection:" + mySocket.RemoteEndPoint.ToString() + "\r\n");
ReceiveData receive = new ReceiveData( mySocket );
Thread thread = new Thread( new System.Threading.ThreadStart( receive.SaveData ));
thread.Start();
}// end while
}// end void
} // end class

public class ReceiveData
{
private Socket mySocket;
public ReceiveData( Socket mySocket )
{
this.mySocket = mySocket;
}// end

public void SaveData()
{
//处理数据
}// end void
}// end class
sy_wcc 2005-07-11
  • 打赏
  • 举报
回复
能说具体点吗?我初学SOCKET RAW编程
oyljerry 2005-07-11
  • 打赏
  • 举报
回复
断点,调试

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧