1

I have been learning some socket programming, i have created an asynchornous server which is only listen and return the data.

but i have a problem here, I use IPEndPoint on server with IPEndPoint(IPAddress.any,port), and I do the same with the client side. When running the server and client, I use try catch and the client side returning its exception like this

"The requested address is not valid in its context 0.0.0.0:port"

here below is the code:

Server.cs

      Public void Listen()
      {
        IPEndPoint IpEnd = new IPEndPoint(IPAddress.Any, 11000);

        Console.WriteLine("Create new socket");
        mainSocket = new Socket(AddressFamily.InterNetwork,
                       SocketType.Stream,ProtocolType.IP);

        Console.WriteLine("Bind the socket");
        mainSocket.Bind(IpEnd);

        Console.WriteLine("Listening to socket");
        mainSocket.Listen(10);

        Console.WriteLine("Waiting Connection");
        mainSocket.BeginAccept(new AsyncCallback(AcceptConnect), null);
      }
      protected void AcceptConnect(IAsyncResult ar)
      {
        try
        {
            Socket client = mainSocket.EndAccept(ar);
            SessionData session = new SessionData();
            Console.WriteLine("Connection Accepted, waiting for Data");
            Console.WriteLine("Waiting a new Connection");
            mainSocket.BeginAccept(new AsyncCallback(AcceptConnect), null);
            try{
                session.clientSocket = client;
                client.BeginReceive(session.buffer,0,SessionData.buffersize,0, 
                                    new AsyncCallback(ReceiveData), session);
            }catch(Exception e){
                client.Send(ASCIIEncoding.Default.GetBytes(e.Message));
                Console.WriteLine(e.Message);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
      }

Client.cs

    public void ClientConnect()
    {

        // Create a TCP/IP socket.
        Socket client = new Socket(AddressFamily.InterNetwork, 
                        SocketType.Stream, ProtocolType.Tcp);

        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 11000);

        // Connect to the remote endpoint.
        client.BeginConnect(remoteEP, new AsyncCallback(ConnectServer), client);
    }
    public void ConnectServer(IAsyncResult ar) 
    {
        try
        {
            Socket client = (Socket)ar.AsyncState;
            client.EndConnect(ar);

            Console.WriteLine("Socket connected to {0}", 
                             client.RemoteEndPoint.ToString());
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

my question is :

  1. Does client need to point at one IPAddress?
  2. Am I just using the wrong code on the server?
4

1 に答える 1

1

クライアントは特定の IP アドレス (つまり、192.168.1.101 など) を指す必要があります。IP 0.0.0.0 にパケットを送信することはできません。サーバーが IP.Any を受け入れることができる理由は、0.0.0.0 で表されるローカル マシン上のすべての IP アドレスをリッスンしているためです。

于 2013-03-20T03:39:09.160 に答える