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


C# Socket類代碼示例

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


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

示例1: ConnectSocket

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

public class GetSocket
{
    private static Socket ConnectSocket(string server, int port)
    {
        Socket s = null;
        IPHostEntry hostEntry = null;
        
        // Get host related information.
        hostEntry = Dns.GetHostEntry(server);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        foreach(IPAddress address in hostEntry.AddressList)
        {
            IPEndPoint ipe = new IPEndPoint(address, port);
            Socket tempSocket = 
                new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            tempSocket.Connect(ipe);

            if(tempSocket.Connected)
            {
                s = tempSocket;
                break;
            }
            else
            {
                continue;
            }
        }
        return s;
    }

    // This method requests the home page content for the specified server.
    private static string SocketSendReceive(string server, int port) 
    {
        string request = "GET / HTTP/1.1\r\nHost: " + server + 
            "\r\nConnection: Close\r\n\r\n";
        Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
        Byte[] bytesReceived = new Byte[256];
        string page = "";
        
        // Create a socket connection with the specified server and port.
        using(Socket s = ConnectSocket(server, port)) {

            if (s == null)
                return ("Connection failed");
        
            // Send request to the server.
            s.Send(bytesSent, bytesSent.Length, 0);  
            
            // Receive the server home page content.
            int bytes = 0;
            page = "Default HTML page on " + server + ":\r\n";

            // The following will block until the page is transmitted.
            do {
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            }
            while (bytes > 0);
        }
        
        return page;
    }
    
    public static void Main(string[] args) 
    {
        string host;
        int port = 80;

        if (args.Length == 0)
            // If no server name is passed as argument to this program, 
            // use the current host name as the default.
            host = Dns.GetHostName();
        else
            host = args[0];

        string result = SocketSendReceive(host, port); 
        Console.WriteLine(result);
    }
}
開發者ID:.NET開發者,項目名稱:System.Net.Sockets,代碼行數:90,代碼來源:Socket

示例2: new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp )

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

class MainClass
{
  const int echoPort = 7;

  [STAThread]
  static void Main( string[] args )
  {
    Socket s = new Socket( AddressFamily.InterNetwork, 
      SocketType.Stream, 
      ProtocolType.Tcp );
    s.Connect( new IPEndPoint( IPAddress.Loopback, echoPort ) );

    UTF8Encoding enc = new UTF8Encoding();
    s.Send( enc.GetBytes( "test message" ) );
    Byte[] buff = new Byte[ 1024 ];
    s.Receive( buff );
    System.Console.WriteLine( enc.GetString( buff ) );
  }
}
開發者ID:C#程序員,項目名稱:System.Net.Sockets,代碼行數:25,代碼來源:Socket


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