当前位置: 首页>>代码示例>>C#>>正文


C# Socket.ReceiveFrom方法代码示例

本文整理汇总了C#中System.Net.Sockets.Socket.ReceiveFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ReceiveFrom方法的具体用法?C# Socket.ReceiveFrom怎么用?C# Socket.ReceiveFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.Socket的用法示例。


在下文中一共展示了Socket.ReceiveFrom方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReceiveFrom1

public static void ReceiveFrom1()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    // Creates an IPEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    
    // This call blocks. 
    s.ReceiveFrom(msg, ref senderRemote);
    s.Close();
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:23,代码来源:Socket.ReceiveFrom

示例2: ReceiveFrom2

public static void ReceiveFrom2()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
    
    // Creates an IpEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks. 
    s.ReceiveFrom(msg, SocketFlags.None, ref senderRemote);
    s.Close();
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:22,代码来源:Socket.ReceiveFrom

示例3: ReceiveFrom3

public static void ReceiveFrom3()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    // Creates an IPEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks. 
    s.ReceiveFrom(msg, msg.Length, SocketFlags.None, ref senderRemote);
    s.Close();
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:22,代码来源:Socket.ReceiveFrom

示例4: ReceiveFrom4

public static void ReceiveFrom4()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
        
    // Creates an IpEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks.  
    s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
    s.Close();
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:21,代码来源:Socket.ReceiveFrom

示例5: Main

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SimpleUdpClient
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      string input, stringData;
      IPEndPoint ipep = new IPEndPoint(
                      IPAddress.Parse("127.0.0.1"), 9050);

      Socket server = new Socket(AddressFamily.InterNetwork,
                     SocketType.Dgram, ProtocolType.Udp);


      string welcome = "Hello, are you there?";
      data = Encoding.ASCII.GetBytes(welcome);
      server.SendTo(data, data.Length, SocketFlags.None, ipep);

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
      EndPoint Remote = (EndPoint)sender;

      data = new byte[1024];
      int recv = server.ReceiveFrom(data, ref Remote);

      Console.WriteLine("Message received from {0}:", Remote.ToString());
      Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

      while(true)
      {
         input = Console.ReadLine();
         if (input == "exit")
            break;
         server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
         data = new byte[1024];
         recv = server.ReceiveFrom(data, ref Remote);
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         Console.WriteLine(stringData);
      }
      Console.WriteLine("Stopping client");
      server.Close();
   }
}
开发者ID:C#程序员,项目名称:System.Net.Sockets,代码行数:53,代码来源:Socket.ReceiveFrom


注:本文中的System.Net.Sockets.Socket.ReceiveFrom方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。