我想监听某机子的某端口,代码如下
this.MyServer = new System.Net.IPEndPoint(this.MyIP,Convert.ToInt32(this.textBox2.Text));
this.Sock = new Socket.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
this.Sock.Bind(this.MyServer);
this.Sock.Listen(50);但是出现如下问题:
在其上下文中,请求的地址无效这是什么错误那

解决方案 »

  1.   

    private System.Net.IPAddress MyIP = System.Net.IPAddress.Parse("127.0.0.1");
      

  2.   

    我试着代码没问题,也许是你的系统网络设置的问题,你看看127.0.0.1能不能ping通,本机不存在这个网络地址才会出这个异常。
      

  3.   

    你用这个算了:
    this.MyServer = new System.Net.IPEndPoint(IPAddress.Any
      

  4.   

    现在我利用控制台做这个程序代码如下:
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;public class SynchronousSocketListener 
    {
        
    public static string data = null; public static void StartListening() 
    {
    byte[] bytes = new Byte[1024];

    IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());//主机名
    Console.WriteLine(ipHostInfo.HostName);
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    Console.WriteLine(ipAddress);
    IPEndPoint localEndPoint = new IPEndPoint(ipAddress,3389); Socket listener = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp ); try 
    {
    listener.Bind(localEndPoint);
    listener.Listen(10); while (true) 
    {
    Console.WriteLine("Waiting for a onnection...");
    Socket handler = listener.Accept();
    data = null; while (true) 
    {
    bytes = new byte[1024];
    string x ="aaaaa<EOF>";
    Console.WriteLine(x); bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(x.ToCharArray());
    foreach(byte y in bytes)
    {
    Console.WriteLine(y.ToString());
    } int bytesRec = handler.Receive(bytes);
    data += Encoding.ASCII.GetString(bytes,0,bytesRec);
    if (data.IndexOf("<EOF>") > -1) 
    {
    break;
    }
    } Console.WriteLine( "Text received : {0}", data); byte[] msg = Encoding.ASCII.GetBytes(data); handler.Send(msg);
    handler.Shutdown(SocketShutdown.Both);
    handler.Close();
    }
                

    catch (Exception e) 
    {
    Console.WriteLine(e.ToString());
    } Console.WriteLine("\nPress ENTER to continue...");
    Console.Read();
            
    } public static int Main(String[] args) 
    {
    StartListening();
    return 0;
    }
    }但是还是不可以,真奇怪
    起步可真困难啊
      

  5.   

    怎么就"soaringbird() "帮忙
    其他人怎么就没人帮忙了
    太简单了吗