3

I try to implement my application with asynchronous socket communication. It perfectly connected and sent the request but I didn't receive any data from server (Java server). Socket connection is

client.BeginConnect(hostname, port,
            new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

  private static void ConnectCallback(IAsyncResult ar) {
    try {
        // Retrieve the socket from the state object.
        Socket client = (Socket) ar.AsyncState;

        // Complete the connection.
        client.EndConnect(ar);

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

        // Signal that the connection has been made.
        connectDone.Set();
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

private static void Receive(Socket client)
{
    try
    {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = client;

        // Begin receiving the data from the remote device.
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None,
            new AsyncCallback(ReceiveCallback), client);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

private static void ReceiveCallback(IAsyncResult ar)
{
    try
    {
        // Retrieve the state object and the client socket 
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket client = state.workSocket;

        // Read data from the remote device.
        int bytesRead = client.EndReceive(ar);
        Console.WriteLine(response);
        if (bytesRead > 0)
        {
            // There might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

            // Get the rest of the data.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        else
        {
            // All the data has arrived; put it in response.
            if (state.sb.Length > 1)
            {
                response = state.sb.ToString();
            }
            // Signal that all bytes have been received.
            receiveDone.Set();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

Any help would be appreciated.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
Mohana s
  • 47
  • 1
  • 9
  • 1
    well the `ConnectCallback` is missing - also: How do you know you are connected? Have you debugged this code (set a breakpoint in `ReceiveCallback`?) - Have you tried to connect via *telnet*? Of course if you start receiving and there was not data already there you might see `0` bytes and stop everything – Random Dev May 12 '15 at 14:59
  • connected = true; this property is seen in debug mode. And I set breakpoints in receivecallback method but it not executed.it was debugging until beginreceive after that it finish off from debug mode. And i didn't tried telnet. – Mohana s May 12 '15 at 16:47
  • You might consider checking this also http://stackoverflow.com/a/1388691/1537726 – csharpwinphonexaml May 13 '15 at 08:06

1 Answers1

0

Your code is almost ok, with one serious bug. In your BeginReceive, you put as the state object the client socket instance. In the ReceiveCallback function you cast it to a StateObject instead of a socket. This will cause an exception showing up at the console.

Nevertheless, also with this bug a breakpoint at the beginning of the ReceiveCallback function will trigger. Did you try this? The exception should have been thrown in any case.

In case that breakpoint doesn't trigger you should check independently whether something is really sent from the server. Of course all that assumed that the connection works, as you stated.

josh
  • 671
  • 5
  • 10