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


C# Socket.Receive方法代码示例

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


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

示例1:

// Build the buffers for the receive.
List<ArraySegment<byte>> recvBuffers = 
                         new List<ArraySegment<byte>>(2);

byte[] bigBuffer = new byte[1024];

// Specify the first buffer segment (2 bytes, starting 
// at the 4th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 20, 500));

int bytesReceived = mySocket.Receive(recvBuffers);

Console.WriteLine("{0}", ASCII.GetString(bigBuffer));
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:19,代码来源:Socket.Receive

示例2: SendReceiveTest4

// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, 
                                   SocketFlags.None);

        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:26,代码来源:Socket.Receive

示例3: while

// Receive the host home page content and loop until all the data is received.
     Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
     strRetPage = "Default HTML page on " + server + ":\r\n";
     strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

     while (bytes > 0)
     {
       bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
       strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
     }
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:10,代码来源:Socket.Receive

示例4: SendReceiveTest2

// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
public static int SendReceiveTest2(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None);
        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:24,代码来源:Socket.Receive

示例5: SendReceiveTest1

// Displays sending with a connected socket
// using the overload that takes a buffer.
public static int SendReceiveTest1(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server.Send(msg);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        i = server.Receive(bytes);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:23,代码来源:Socket.Receive

示例6: Socket.Receive(byte[] data)

//引入命名空间
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
   public static void Main()
   {
      IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);

      Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

      try
      {
         server.Connect(ip);
      } catch (SocketException e){
         Console.WriteLine("Unable to connect to server.");
         return;
      }

      byte[] data = new byte[1024];
      int receivedDataLength = server.Receive(data);
      string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
      Console.WriteLine(stringData);

      server.Shutdown(SocketShutdown.Both);
      server.Close();
   }
}
开发者ID:C#程序员,项目名称:System.Net.Sockets,代码行数:31,代码来源:Socket.Receive


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