编译的时候你需要用/r参数将名字空间System.Net.Sockets引用进来
csc query.cs /r:System.Net.Sockets

解决方案 »

  1.   

    C:\>csc query.cs /r:System.Net.Sockets
    Microsoft (R) Visual C# .NET Compiler version 7.00.9466
    for Microsoft (R) .NET Framework version 1.0.3705
    版权所有 (C) Microsoft Corporation 2001。保留所有权利。error CS0006: 未能找到元数据文件“System.Net.Sockets”还有,怎么样将服务器的返回信息显示出来?谢谢了~~
      

  2.   

    搞错了,原来是你的这一行出了问题:queryinfo=get http/1.0\r\n; 要改成queryinfo="get http/1.0\r\n"; 
      

  3.   

    改成这样
    using System;  
    using System.Net.Sockets;  
    using System.Text; 
    public class query 
    {     
         static void Main(String[] args)  
         { 
                 String queryinfo="get http/1.0\r\n"; 
                 TcpClient client=new TcpClient(args[0],80); 
                 NetworkStream ns=client.GetStream(); 
                 byte[] sendquery=Encoding.ASCII.GetBytes(queryinfo); 
                 ns.Write(sendquery,0,sendquery.Length); 
                 byte[] buffer=new byte[1024]; 
      int result=ns.Read(buffer,0,buffer.Length);
      Console.WriteLine(Encoding.ASCII.GetString(buffer,0,result));
                 Console.ReadLine();
         } 
    } 依然不对,郁闷……
    知道的说一下~~
    谢谢了
      

  4.   

    第一个例子是因你的main 是静态的,而你的变量不是静态的
    上面这个例子是正常的,但我在你这个代码中发现了很多导致问题的控制符0xa3 0xa0,你使用什么东西写的,建议你重要写一份
      

  5.   

    哈,终于写对了。
    但是出现异常的时候,有点不爽,会弹出窗口,这样如何解决?谢谢
    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    public class query
    {
           static void Main(string[] args)
           {
                  IPEndPoint point;
                  Socket socket;
                  string sendstr;
                  if(args.Length==0)Console.WriteLine("Usage:queryweb Ip Port.");
                  else if(args.Length==1)Console.WriteLine("not enough args.");
                  else if(args.Length==2)
                  {
                       IPAddress host=IPAddress.Parse(args[0]);
                       int port=Int32.Parse(args[1]);
                       point=new IPEndPoint(host,port);
                       socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                       Console.WriteLine("尝试连接主机"+host+"的"+port+"端口……");
                       try
                       {
                          socket.Connect(point);
                       }
                       catch(Exception e)
                       {
                           Console.WriteLine("无法连接主机"+host+"的"+port+"端口……");
                       }
                       sendstr="GET /http/1.0\r\n";
                       byte[] bytessendstr=new byte[1024];
                       bytessendstr=Encoding.ASCII.GetBytes(sendstr);
                       try
                       {
                           socket.Send(bytessendstr,bytessendstr.Length,0);
                       }
                       catch(Exception)
                       {
                           Console.WriteLine("发送查询信息失败……");
                       }
                       string recvstr="";
                       byte[] recvbytes=new byte[1024];
                       int bytes=0;
                       while(true)
                       {
                            bytes=socket.Receive(recvbytes,recvbytes.Length,0);
                            if(bytes<=0)
                            break;
                            recvstr+=Encoding.ASCII.GetString(recvbytes,0,bytes);
                       }
                       Console.WriteLine(recvstr);
                       socket.Shutdown(SocketShutdown.Both);
                       socket.Close();
                 }
          }
    }