Anamay
Anamay

Reputation: 71

Client Socket not getting reponse bytes in .NET

I am creating and sending a message on server socket and not getting any response in return , I would like to know what could be possible reasons for the same. Below is my sample code for client socket

static void Main()
{
    int _DCPport = 9090;
    IPHostEntry ipHostInfo = Dns.GetHostEntry("HostName");
    Console.WriteLine("Got the IP Host Info" , ipHostInfo.ToString ());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    Console.WriteLine("Got the IP hadress Info");

    //End point of the host where the socket will be connected
    IPEndPoint remoteDAPEndPoint = new IPEndPoint(ipAddress, _DCPport);

    // Create a clientSocket that connects to host
    Socket clientDAP = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    clientDAP.Connect(remoteDAPEndPoint);
    Console.WriteLine("Client socket connected to remote end point");

    #region Input variables
    string requestString = "Hello";
    string responseString = "";

    byte[] requestByte = System.Text.Encoding.UTF8.GetBytes(requestString);
    byte[] responseByte = new byte[1024];

    StringBuilder messageBuilder = new StringBuilder();
    int byteResult = 0;
    #endregion

    try
    {
        //sending the string as bytes over the socket
        Console.WriteLine("Sending the input " + requestString);
        clientDAP.Send(requestByte);
    }
    catch (SocketException eX)
    {

        Console.WriteLine(eX.Message);
    }

    try
    {
        //recieving the response bytes
        byteResult = clientDAP.Receive(responseByte, clientDAP.Available, SocketFlags.None);
        Console.WriteLine("Recieved {0} bytes as response from remote end point" , byteResult);
        if (clientDAP.Connected)
        {
            responseString = Encoding.UTF8.GetString(responseByte);
            messageBuilder.Append(responseString);
        }
    }
    catch (SocketException eX)
    {

        Console.WriteLine(eX.Message);
    }

    clientDAP.Shutdown(SocketShutdown.Both);
    clientDAP.Disconnect(true);


    string sResult = messageBuilder.ToString();
    Console.WriteLine(sResult);


}

I am not getting any bytes when i call socket.Recieve() , Any reasons why such is happening ?

Upvotes: 1

Views: 282

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239664

From Socket.Send:

There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send

Which means that the data may not have even arrived at the server yet when your call to Send has completed. Let alone that the server has had time to process your data and return a response.

And yet your code charges straight ahead to try to receive the server's response. This isn't unreasonable, in and of itself. Your call to Receive would have blocked until it had received some data if you had specified the size of your buffer, rather than using Available, which will happily return 0 if there's no data yet available. So your Receive call is asking for up to 0 bytes and that's how many you're being given.

So change this line

byteResult = clientDAP.Receive(responseByte, 1024, SocketFlags.None);

(Or better yet, introduce a constant and/or use responseByte.Length or switch to an overload where you don't explicitly specify how many bytes you want)

Upvotes: 1

Related Questions