using System.Net.Sockets;下面的示例显示如何使用 Socket 类向 HTTP 服务器发送数据和接收响应public string DoSocketGet(string server) 
 {
    //Sets up variables and a 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;
 
    // IPAddress and IPEndPoint represent the endpoint that will
    //   receive the request.
    // Get the first IPAddress in the list using DNS.
    IPAddress hostadd = Dns.Resolve(server).AddressList[0];
    IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
 
    //Creates the Socket for sending data over TCP.
    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
       ProtocolType.Tcp );
 
    // Connects to the host using IPEndPoint.
    s.Connect(EPhost);
    if (!s.Connected)
    {
       strRetPage = "Unable to connect to host";
       return strRetPage;
    }
 
    // Sends the GET text to the host.
    s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
 
    // Receives the page, looping until all bytes are 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, SocketFlags.None);
       strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
    }
 
    return strRetPage;
 }

解决方案 »

  1.   

    using System.Net.Sockets;下面的示例显示如何使用 Socket 类向 HTTP 服务器发送数据和接收响应public string DoSocketGet(string server) 
     {
        //Sets up variables and a 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;
     
        // IPAddress and IPEndPoint represent the endpoint that will
        //   receive the request.
        // Get the first IPAddress in the list using DNS.
        IPAddress hostadd = Dns.Resolve(server).AddressList[0];
        IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
     
        //Creates the Socket for sending data over TCP.
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
           ProtocolType.Tcp );
     
        // Connects to the host using IPEndPoint.
        s.Connect(EPhost);
        if (!s.Connected)
        {
           strRetPage = "Unable to connect to host";
           return strRetPage;
        }
     
        // Sends the GET text to the host.
        s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
     
        // Receives the page, looping until all bytes are 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, SocketFlags.None);
           strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
        }
     
        return strRetPage;
     }
      

  2.   

    其实.NET Framework已经把Socket封装得很易用了,你只人使用System.Net.Socket里面的类就可以使用Socket了.
      

  3.   

    谢谢,inet_ntoa()在C#中如何实现的! 是类里的哪个函数啊?
      

  4.   

    socket成员ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfSystemNetSocketsSocketMembersTopic.htm
      

  5.   

    用C#如何把数字-1062731775转换为与其对应的IP地址串"192.168.0.1"????
    另送分!
      

  6.   

    ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemnetipaddressclassaddresstopic.htm看下面的备注 使用 IPAddress.ToString()
      

  7.   

    如果不想用socket类,可以使用wsock32.dll的函数。声明方法稍嫌麻烦:
    [DllImport ("wsock32.dll")]
    public static extern htonl(
     ……
    )