當前位置: 首頁>>代碼示例>>C#>>正文


C# Socket.Send方法代碼示例

本文整理匯總了C#中System.Net.Sockets.Socket.Send方法的典型用法代碼示例。如果您正苦於以下問題:C# Socket.Send方法的具體用法?C# Socket.Send怎麽用?C# Socket.Send使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Sockets.Socket的用法示例。


在下文中一共展示了Socket.Send方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: 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.Send

示例2: 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.Send

示例3: SendReceiveTest3

// Displays sending with a connected socket
// using the overload that takes a buffer, message size, and socket flags.
public static int SendReceiveTest3(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, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        int byteCount = server.Receive(bytes, 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,代碼行數:25,代碼來源:Socket.Send

示例4: 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.Send

示例5: Socket.Send(String stringValue)

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

class MainClass
{
   public static void Main()
   {
      IPAddress host = IPAddress.Parse("192.168.1.1");
      IPEndPoint hostep = new IPEndPoint(host, 8000);
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      try
      {
         sock.Connect(hostep);
      } catch (SocketException e) {
         Console.WriteLine("Problem connecting to host");
         Console.WriteLine(e.ToString());
         sock.Close();
         return;
      }

      try
      {
         sock.Send(Encoding.ASCII.GetBytes("testing"));
      } catch (SocketException e) {
          Console.WriteLine("Problem sending data");
          Console.WriteLine( e.ToString());
          sock.Close();
          return;
      }
      sock.Close();
   }
}
開發者ID:C#程序員,項目名稱:System.Net.Sockets,代碼行數:36,代碼來源:Socket.Send

示例6: Socket.Send(data, data.Length, SocketFlags.None)

/*
C# Network Programming 
by Richard Blum

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

public class SelectTcpClient
{
   public static void Main()
   {
      Socket sock = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
      byte[] data = new byte[1024];
      string stringData;
      int recv;

      sock.Connect(iep);
      Console.WriteLine("Connected to server");
      recv = sock.Receive(data);
      stringData = Encoding.ASCII.GetString(data, 0, recv);
      Console.WriteLine("Received: {0}", stringData);


      while(true)
      {
         stringData = Console.ReadLine();
         if (stringData == "exit")
            break;
         data = Encoding.ASCII.GetBytes(stringData);
         sock.Send(data, data.Length, SocketFlags.None);
         data = new byte[1024];
         recv = sock.Receive(data);
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         Console.WriteLine("Received: {0}", stringData);
      }
      sock.Close();
   }
}
開發者ID:C#程序員,項目名稱:System.Net.Sockets,代碼行數:46,代碼來源:Socket.Send


注:本文中的System.Net.Sockets.Socket.Send方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。