Question

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?
Was it helpful?

Solution

The client needs to point to a specific IP address (i.e. 192.168.1.101 or something similar), you can't send packets to IP 0.0.0.0. The reason that the server can accept IP.Any is that it's listening on all IP address on the local machine, which is represented at 0.0.0.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top