socket是由操作系统自己实现的...

解决方案 »

  1.   

    using System;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;public class mySocket
    {
       private static Socket connectSocket(string server, int port)
       {
          Socket s = null;
          IPHostEntry iphe = null;
        
        
        try
          {
            // Get host related information.
          iphe = Dns.Resolve(server);
        
        
          // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
          // an exception to be thrown if the host IP Address is not compatible with the address family
          // (typical in the IPv6 case).
            foreach(IPAddress ipad in iphe.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(ipad, port);            Socket tmpS = 
                  new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);          tmpS.Connect(ipe);        if(tmpS.Connected)
                {
                  s = tmpS;
                  break;
                }
                else
                  continue;
            }
          }
        
          catch(SocketException e) 
          {
              Console.WriteLine("SocketException caught!!!");
              Console.WriteLine("Source : " + e.Source);
              Console.WriteLine("Message : " + e.Message);
          }
          catch(Exception e)
          {
              Console.WriteLine("Exception caught!!!");
              Console.WriteLine("Source : " + e.Source);
              Console.WriteLine("Message : " + e.Message);
          }
          return s;    }
      // This method requests the home page content for the passed server.
      // It displays the number of bytes received and the page content.
      private static string socketSendReceive(string server, int port) 
        {
          //Set up variables and String to write to the server.
          Encoding ASCII = Encoding.ASCII;
          string Get = "GET / HTTP/1.1\r\nHost: " + server + 
                     "\r\nConnection: Close\r\n\r\n";
          Byte[] ByteGet = ASCII.GetBytes(Get);
          Byte[] RecvBytes = new Byte[256];
          String strRetPage = null;      // Create a socket connection with the specified server and port.
          Socket s = connectSocket(server, port);      if (s == null)
            return ("Connection failed");
          
          // Send request to the server.
          s.Send(ByteGet, ByteGet.Length, 0);  
            
        
          // Receive the server  home page content.
          Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);    
       
          // Read the first 256 bytes.
          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);
          }
               
          return strRetPage;
        }
        
        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 default.
              host = Dns.GetHostName();
          else
              host = args[0];
          string result = socketSendReceive(host, port); 
           
          Console.WriteLine(result);
       }
      
    }
      

  2.   

    不算不算,你这个不算,看看我从。NET中反编译出来的库:[DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern IntPtr socket([In] AddressFamily addressFamily, [In] SocketType socketType, [In] ProtocolType protocolType); [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAAddressToString([In] byte[] socketAddress, [In] int socketAddressSize, [In] IntPtr lpProtocolInfo, [Out] StringBuilder addressString, [In, Out] ref int addressStringLength);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSACleanup();
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAEnumNetworkEvents([In] IntPtr socketHandle, [In] IntPtr Event, [In, Out] ref NetworkEvents networkEvents);
    [DllImport("ws2_32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    internal static extern int WSAEnumProtocols([In, MarshalAs(UnmanagedType.LPArray)] int[] lpiProtocols, [In] IntPtr lpProtocolBuffer, [In] ref uint lpdwBufferLength);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAEventSelect([In] IntPtr socketHandle, [In] IntPtr Event, [In] AsyncEventBits NetworkEvents);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern uint WSAGetLastError();
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern bool WSAGetOverlappedResult([In] IntPtr socketHandle, [In] IntPtr overlapped, out uint bytesTransferred, [In] bool wait, [In] IntPtr ignored);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAIoctl([In] IntPtr socketHandle, [In] int ioControlCode, [In] byte[] inBuffer, [In] int inBufferSize, [Out] byte[] outBuffer, [In] int outBufferSize, out int bytesTransferred, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSARecv([In] IntPtr socketHandle, [In, Out] ref WSABuffer Buffer, [In] int BufferCount, [In] IntPtr bytesTransferred, [In, Out] ref SocketFlags socketFlags, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSARecvFrom([In] IntPtr socketHandle, [In, Out] ref WSABuffer Buffer, [In] int BufferCount, [In] IntPtr bytesTransferred, [In, Out] ref SocketFlags socketFlags, [In] IntPtr socketAddressPointer, [In] IntPtr socketAddressSizePointer, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSASend([In] IntPtr socketHandle, [In] WSABuffer[] BufferArray, [In] int BufferCount, [In] IntPtr bytesTransferred, [In] SocketFlags socketFlags, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSASend([In] IntPtr socketHandle, [In] ref WSABuffer Buffer, [In] int BufferCount, [In] IntPtr bytesTransferred, [In] SocketFlags socketFlags, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSASendTo([In] IntPtr socketHandle, [In] ref WSABuffer Buffer, [In] int BufferCount, [In] IntPtr BytesWritten, [In] SocketFlags socketFlags, [In] byte[] socketAddress, [In] int socketAddressSize, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern IntPtr WSASocket([In] AddressFamily addressFamily, [In] SocketType socketType, [In] ProtocolType protocolType, [In] IntPtr protocolInfo, [In] uint group, [In] SocketConstructorFlags flags);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAStartup([In] short wVersionRequested, out WSAData lpWSAData);
    [DllImport("ws2_32.dll", CharSet=CharSet.Ansi, SetLastError=true)]
    internal static extern int WSAStringToAddress([In] string addressString, [In] AddressFamily addressFamily, [In] IntPtr lpProtocolInfo, [Out] byte[] socketAddress, [In, Out] ref int socketAddressSize);