一个问题。怎么在点对点UDP中,得到对方IP和PORT

smartnose 2003-03-12 10:22:28
一个问题。怎么在点对点UDP中,得到对方IP和PORT
...全文
118 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
snewxf 2003-03-22
  • 打赏
  • 举报
回复
像飞刀兄说的这样你试试吧!
如QQ你临听8000端口。当你接收到消息时,总是得到一个socket吧.
socket有RemoteEndPoint或者LocalEndPoint属性,EndPoint的,转成IPEndPoint
SpyX 2003-03-19
  • 打赏
  • 举报
回复
ms-help://MS.NETFrameworkSDK.CHS/cpref/html/frlrfsystemnetsocketsudpclientclasstopic.htm

UdpClient 类 [C#]
提供用户数据文报 (UDP) 网络服务。

有关此类型所有成员的列表,请参阅 UdpClient 成员。

System.Object
System.Net.Sockets.UdpClient

线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。

备注
UdpClient 类基于 Socket 类提供更高理念级别的 UDP 服务。因为 UDP 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机连接。但您可以选择使用以下方法之一建立默认远程主机:

使用远程主机名和端口号作为参数创建 UdpClient 类的实例。
创建 UdpClient 类的实例,然后调用 Connect 方法。
可以使用在 UdpClient 中提供的任何一种发送方法将数据发送到远程设备。使用 Receive 方法从远程设备接收数据

注意 如果已指定了默认远程主机,则不要使用主机名或 IPEndPoint 调用 Send。如果您进行了调用,UdpClient 将引发异常。
UdpClient 方法使您还可以接收多路广播的数据文报。使用 JoinMulticastGroup 和 DropMulticastGroup 将 UdpClient 与多路广播组关联和解除它们之间的关联。

示例
下面的示例使用主机名 www.contoso.com 在端口 11000 上建立 UdpClient 连接。将很短的字符串消息发送到两个单独的远程主机。Receive 方法在接收消息前阻止执行。使用传递给 Receive 的 IPEndPoint 可以显示响应主机的标识。

[C#]
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
try{
udpClient.Connect("www.contoso.com", 11000);

// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

udpClient.Send(sendBytes, sendBytes.Length);

// Sends a message to a different host using optional hostname and port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);

//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());

udpClient.Close();
udpClientB.Close();

}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
SpyX 2003-03-19
  • 打赏
  • 举报
回复
看这里!这里有答案!
ms-help://MS.NETFrameworkSDK.CHS/cpref/html/frlrfsystemnetsocketsudpclientclasstopic.htm

aspcn 2003-03-19
  • 打赏
  • 举报
回复
不管是UDP还是TCP,接收时,总是得到一个socket吧.
socket有RemoteEndPoint或者LocalEndPoint属性,EndPoint的,转成IPEndPoint,你再查查IPEndPoint的属性吧,查了就知道了.
就不多讲了.
snewxf 2003-03-18
  • 打赏
  • 举报
回复
是呀!拿QQ来说。假如有人发消息!
要如何才能知道发消息的人的IP呢?用编程实现如何实现??
SpyX 2003-03-18
  • 打赏
  • 举报
回复
>>那么我在不知道对方IP和PORT的情况下,能够向对方发送信息吗?
肯定不能的!
>>我不知道接受方的IP和PORT,接受方也不知道发送方的IP和PORT.
不是的!当接受方收到发送方的信息后,就可以知道发送方的IP和Port了!
smartnose 2003-03-16
  • 打赏
  • 举报
回复
SpyX(Sy5Bu9) 如果如你所说,那么我在不知道对方IP和PORT的情况下,能够向对方发送信息吗?如果向你说的,我不知道接受方的IP和PORT,接受方也不知道发送方的IP和PORT,那么请问,双方怎么互传数据。
SpyX 2003-03-13
  • 打赏
  • 举报
回复
Re smartnose(smartnose):那是当然,首先发送方设置IP,和端口,关键是接收方必须知道发送方的IP和端口

UDP无连接的协议! 接受方不需要知道发送方的 IP/Port ,和TCP/IP协议不同!TCP/IP的发送是必须知道接受方的IP/Port才可以建立起连接!而Udp不是。
ttzzgg_80713 2003-03-13
  • 打赏
  • 举报
回复
accpet的时候不是可以知道吗?
starky 2003-03-13
  • 打赏
  • 举报
回复
要事先约定的,就好像你访问www的web网页,要知道对方ip,默认是80端口,如果你不知道,就要扫描每个端口。
smartnose 2003-03-13
  • 打赏
  • 举报
回复
那是当然,首先发送方设置IP,和端口,关键是接收方必须知道发送方的IP和端口
starky 2003-03-12
  • 打赏
  • 举报
回复
点对点应该先是有 对方IP和PORT,然后才用udp或是tcp的吧?
snewxf 2003-03-12
  • 打赏
  • 举报
回复
UP!

111,126

社区成员

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

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

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